Skip to content

Commit

Permalink
fix: work with all country structures
Browse files Browse the repository at this point in the history
  • Loading branch information
12rambau committed May 31, 2024
1 parent 7032876 commit 50fc6e2
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 8 deletions.
12 changes: 4 additions & 8 deletions pygadm/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,20 +231,16 @@ def _items(self, name: str = "", admin: str = "", content_level: int = -1) -> gp
level_gdf = gpd.GeoDataFrame.from_features(data)
level_gdf.rename(columns={"COUNTRY": "NAME_0"}, inplace=True)

# as explained in the next comment data from the geojson are completely broken
# to make sure the fullmatch works we need to replace the name with full camelcas alternative.
# should be removed once the next version of GADM is released.
corrected_id = id.title().replace(" ", "")
gdf = level_gdf[level_gdf[column.format(level)].str.fullmatch(corrected_id, case=False)]

# workaround for the wrong naming convention in the geojson files
# https://gis.stackexchange.com/questions/467848/how-to-get-back-spaces-in-administrative-names-in-gadm-4-1
# it should disappear in the next version of GADM
# we are forced to retrieve all the names from the df (sourced from.gpkg) to replace the one from
# the geojson that are all in camelCase
complete_df = Names(name, admin, content_level=content_level, complete=True)
complete_df = Names(admin=iso_3, content_level=content_level, complete=True)
for i in range(int(content_level) + 1):
gdf.loc[:, (f"NAME_{i}")] = complete_df[f"NAME_{i}"].values
level_gdf.loc[:, f"NAME_{i}"] = complete_df[f"NAME_{i}"].values

gdf = level_gdf[level_gdf[column.format(level)].str.fullmatch(id, case=False)]

return gdf

Expand Down
6 changes: 6 additions & 0 deletions tests/test_items.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,9 @@ def test_adm_items():
with pytest.warns(DeprecationWarning):
gdf2 = pygadm.AdmItems(name="Singapore")
assert gdf1.equals(gdf2)


def test_sub_admin(dataframe_regression):
"""Request a sublevel."""
gdf = pygadm.Items(admin="SGP.1_1")
dataframe_regression.check(gdf[["GID_1", "NAME_1", "GID_0", "NAME_0"]])
2 changes: 2 additions & 0 deletions tests/test_items/test_sub_admin.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
,GID_1,NAME_1,GID_0,NAME_0
0,SGP.1_1,Central,SGP,Singapore

0 comments on commit 50fc6e2

Please sign in to comment.