diff --git a/pygadm/__init__.py b/pygadm/__init__.py index f2ea6e9..1ec8d04 100644 --- a/pygadm/__init__.py +++ b/pygadm/__init__.py @@ -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 diff --git a/tests/test_items.py b/tests/test_items.py index beae5c4..7a0310d 100644 --- a/tests/test_items.py +++ b/tests/test_items.py @@ -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"]]) diff --git a/tests/test_items/test_sub_admin.csv b/tests/test_items/test_sub_admin.csv new file mode 100644 index 0000000..22a6d20 --- /dev/null +++ b/tests/test_items/test_sub_admin.csv @@ -0,0 +1,2 @@ +,GID_1,NAME_1,GID_0,NAME_0 +0,SGP.1_1,Central,SGP,Singapore