Skip to content

Commit 56370ee

Browse files
authored
fix: make sure to include areas with disputed regions (#74)
2 parents 2fac721 + 535070c commit 56370ee

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

pygadm/__init__.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -229,18 +229,32 @@ def _items(self, name: str = "", admin: str = "", content_level: int = -1) -> gp
229229
)
230230

231231
level_gdf = gpd.GeoDataFrame.from_features(data)
232-
level_gdf.rename(columns={"COUNTRY": "NAME_0"}, inplace=True)
232+
level_gdf = level_gdf.rename(columns={"COUNTRY": "NAME_0"})
233+
234+
# countries can embed multiple iso codes as some places are disputed so we need to gather them
235+
# from the geojson file
236+
isos = level_gdf.GID_0.dropna().unique()
233237

234238
# workaround for the wrong naming convention in the geojson files
235239
# https://gis.stackexchange.com/questions/467848/how-to-get-back-spaces-in-administrative-names-in-gadm-4-1
236240
# it should disappear in the next version of GADM
237241
# we are forced to retrieve all the names from the df (sourced from.gpkg) to replace the one from
238-
# the geojson that are all in camelCase
239-
complete_df = Names(admin=iso_3, content_level=content_level, complete=True)
242+
# the geojson that are all in camelCase.
243+
df_list = [Names(admin=iso, content_level=content_level, complete=True) for iso in isos]
244+
complete_df = pd.concat(df_list)
240245
for i in range(int(content_level) + 1):
241246
level_gdf.loc[:, f"NAME_{i}"] = complete_df[f"NAME_{i}"].values
242247

243-
gdf = level_gdf[level_gdf[column.format(level)].str.fullmatch(id, case=False)]
248+
df_list = [Names(admin=iso, content_level=content_level, complete=True) for iso in isos]
249+
complete_df = pd.concat(df_list)
250+
# GID columns to merge on; they (should) match exactly
251+
shared_cols = [f"GID_{i}" for i in range(int(content_level) + 1)]
252+
# Camel-case columns to drop
253+
drop_cols = [f"NAME_{i}" for i in range(int(content_level) + 1)]
254+
gdf = pd.merge(level_gdf.drop(drop_cols, axis=1), complete_df, how="inner", on=shared_cols)
255+
256+
# now we can filter this dataframe with the appropriate name or admin code
257+
gdf = gdf[gdf[column.format(level)].str.fullmatch(id, case=False)]
244258

245259
return gdf
246260

0 commit comments

Comments
 (0)