Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: make sure to include areas with disputed regions #74

Merged
merged 6 commits into from
Jan 3, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions pygadm/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,18 +229,32 @@ 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)
level_gdf = level_gdf.rename(columns={"COUNTRY": "NAME_0"})

# countries can embed multiple iso codes as some places are disputed so we need to gather them
# from the geojson file
isos = level_gdf.GID_0.dropna().unique()

# 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(admin=iso_3, content_level=content_level, complete=True)
# the geojson that are all in camelCase.
df_list = [Names(admin=iso, content_level=content_level, complete=True) for iso in isos]
complete_df = pd.concat(df_list)
for i in range(int(content_level) + 1):
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)]
df_list = [Names(admin=iso, content_level=content_level, complete=True) for iso in isos]
complete_df = pd.concat(df_list)
# GID columns to merge on; they (should) match exactly
shared_cols = [f"GID_{i}" for i in range(int(content_level) + 1)]
# Camel-case columns to drop
drop_cols = [f"NAME_{i}" for i in range(int(content_level) + 1)]
gdf = pd.merge(level_gdf.drop(drop_cols, axis=1), complete_df, how="inner", on=shared_cols)

# now we can filter this dataframe with the appropriate name or admin code
gdf = gdf[gdf[column.format(level)].str.fullmatch(id, case=False)]

return gdf

Expand Down
Loading