-
Notifications
You must be signed in to change notification settings - Fork 0
feat: use the new version of FAO GAUL (GAUL 2024) #58
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
814bd42
feat: use the new version of FAO GAUL (GAUL 2024)
12rambau e5a119c
build: add the environment correct variables
12rambau 3b2176b
tests: init test from a service account
12rambau 6e5ea0c
fix: add the docstring to the function
12rambau 21eae8f
fix: drop support for 3.8 and add for 3.12
12rambau 65e9aa8
test: fix the tes twith the new datasets
12rambau d4bdf99
docs: use an earthengine authentication
12rambau ecefab7
fix: make sure geetools is loaded
12rambau 217ec7b
docs: make documentation depend on geetools
12rambau 76c8bd6
docs: use 2025 code convention in usage
12rambau File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,7 +3,6 @@ | |
|
|
||
| This lib provides access to FAO GAUL 2015 datasets from a Python script. it is the best boundary dataset available for GEE at this point. We provide access to The current version (2015) administrative areas till level 2. | ||
| """ | ||
| import json | ||
| import warnings | ||
| from difflib import get_close_matches | ||
| from functools import lru_cache | ||
|
|
@@ -21,8 +20,7 @@ | |
| __email__ = "pierrick.rambaud49@gmail.com" | ||
|
|
||
| __gaul_data__ = Path(__file__).parent / "data" / "gaul_database.parquet" | ||
| __gaul_continent__ = Path(__file__).parent / "data" / "gaul_continent.json" | ||
| __gaul_asset__ = "FAO/GAUL/2015/level{}" | ||
| __gaul_asset__ = "projects/sat-io/open-datasets/FAO/GAUL/GAUL_2024_L{}" | ||
|
|
||
|
|
||
| @lru_cache(maxsize=1) | ||
|
|
@@ -63,7 +61,7 @@ def __init__( | |
| id = name if name else admin | ||
|
|
||
| # read the data and find if the element exist | ||
| column = "ADM{}_NAME" if is_name else "ADM{}_CODE" | ||
| column = "gaul{}_name" if is_name else "gaul{}_code" | ||
| is_in = ( | ||
| df.filter([column.format(i) for i in range(3)]) | ||
| .apply(lambda col: col.str.lower()) | ||
|
|
@@ -82,18 +80,18 @@ def __init__( | |
| else: | ||
| close_ids = [i.upper() for i in close_ids] | ||
| raise ValueError( | ||
| f'The requested "{id}" is not part of FAO GAUL 2015. The closest ' | ||
| f'The requested "{id}" is not part of FAO GAUL 2024. The closest ' | ||
| f'matches are: {", ".join(close_ids)}.' | ||
| ) | ||
|
|
||
| # Get the code of the associated country of the identifed area and the associated level | ||
| line = is_in[~((~is_in).all(axis=1))].idxmax(1) | ||
| level = line.iloc[0][3] | ||
| level = line.iloc[0][4] | ||
|
|
||
| # load the max_level available in the requested area | ||
| sub_df = df[df[column.format(level)].str.fullmatch(id, case=False)] | ||
| max_level = next( | ||
| i for i in reversed(range(3)) if (sub_df[f"ADM{i}_NAME"] != "").any() | ||
| i for i in reversed(range(3)) if (sub_df[f"gaul{i}_name"] != "").any() | ||
| ) | ||
|
|
||
| # get the request level from user | ||
|
|
@@ -119,7 +117,7 @@ def __init__( | |
| content_level = 0 if content_level == -1 else content_level | ||
|
|
||
| # get the columns name corresponding to the requested level | ||
| columns = [f"ADM{content_level}_NAME", f"ADM{content_level}_CODE"] | ||
| columns = [f"gaul{content_level}_name", f"gaul{content_level}_code"] | ||
|
|
||
| # the list will contain duplicate as all the smaller admin level will be included | ||
| sub_df = sub_df.drop_duplicates(subset=columns, ignore_index=True) | ||
|
|
@@ -160,10 +158,10 @@ def __init__( | |
| if names == [""] == admins: | ||
| raise ValueError('at least "name" or "admin" need to be set.') | ||
|
|
||
| # special parsing for continents. They are saved as admins to avoid any duplication | ||
| continents = json.loads(__gaul_continent__.read_text()) | ||
| if len(names) == 1 and names[0].lower() in continents: | ||
| admins = [c for c in continents[names[0].lower()]] | ||
| # special parsing for continents. They are associated to the countries by FAO. | ||
| continents = _df().continent.unique() | ||
| if len(names) == 1 and (c := names[0].lower()) in continents: | ||
| admins = [a for a in _df()[_df().continent == c].gaul0_code.unique()] | ||
|
12rambau marked this conversation as resolved.
|
||
| names = [""] | ||
|
|
||
| # use itertools, normally one of them is empty so it will raise an error | ||
|
|
@@ -201,20 +199,20 @@ def _items( | |
| f"If you don't know the GAUL code, use the following code, " | ||
| f'it will return the GAUL codes as well:\n`Names(name="{name}")`' | ||
| ) | ||
| df.columns[0][3] | ||
| df.columns[0][4] | ||
|
|
||
| # now load the useful one to get content_level | ||
| df = Names(name, admin, content_level) | ||
| content_level = df.columns[1][3] | ||
| content_level = df.columns[1][4] | ||
|
||
|
|
||
| # checks have already been performed in Names and there should | ||
| # be one single result | ||
| ids = [int(v) for v in df[f"ADM{content_level}_CODE"].to_list()] | ||
| ids = [int(v) for v in df[f"gaul{content_level}_code"].to_list()] | ||
|
|
||
| # read the accurate dataset | ||
| feature_collection = ee.FeatureCollection( | ||
| __gaul_asset__.format(content_level) | ||
| ).filter(ee.Filter.inList(f"ADM{content_level}_CODE", ids)) | ||
| ).filter(ee.Filter.inList(f"gaul{content_level}_code", ids)) | ||
|
|
||
| return feature_collection | ||
|
|
||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.