Skip to content

Commit

Permalink
fix: manage multi word names (#70)
Browse files Browse the repository at this point in the history
  • Loading branch information
12rambau authored May 31, 2024
2 parents bcc3466 + 50fc6e2 commit 29008f8
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 3 deletions.
7 changes: 4 additions & 3 deletions pygadm/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,16 +230,17 @@ 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)
gdf = level_gdf[level_gdf[column.format(level)].str.fullmatch(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
12 changes: 12 additions & 0 deletions tests/test_items.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ def test_too_high(data_regression):
data_regression.check(gdf.GID_1.tolist())


def test_multi_word_name(dataframe_regression):
"""Request a multi-word area."""
gdf = pygadm.Items(name="United States")
dataframe_regression.check(gdf[["NAME_0", "GID_0"]])


def test_too_low(data_regression):
"""Request a sublevel lower than available in the area."""
# request a level too low
Expand Down Expand Up @@ -109,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_multi_word_name.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
,NAME_0,GID_0
0,United States,USA
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 29008f8

Please sign in to comment.