@@ -229,18 +229,32 @@ def _items(self, name: str = "", admin: str = "", content_level: int = -1) -> gp
229
229
)
230
230
231
231
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 ()
233
237
234
238
# workaround for the wrong naming convention in the geojson files
235
239
# https://gis.stackexchange.com/questions/467848/how-to-get-back-spaces-in-administrative-names-in-gadm-4-1
236
240
# it should disappear in the next version of GADM
237
241
# 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 )
240
245
for i in range (int (content_level ) + 1 ):
241
246
level_gdf .loc [:, f"NAME_{ i } " ] = complete_df [f"NAME_{ i } " ].values
242
247
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 )]
244
258
245
259
return gdf
246
260
0 commit comments