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

Revise deprecations build_shapes #315

Merged
merged 3 commits into from
Mar 29, 2022
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions doc/release_notes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ Upcoming Release

* Solved the issue of "overpassing nodes" and restyling osm_build_network: `PR #294 <https://github.com/pypsa-meets-africa/pypsa-africa/pull/294>`__

* Revise deprecations in build_shape: `PR #315 <https://github.com/pypsa-meets-africa/pypsa-africa/pull/315>`__

PyPSA-Africa 0.0.1 (24th December 2021)
=====================================
Expand Down
22 changes: 16 additions & 6 deletions scripts/build_shapes.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import fiona
import geopandas as gpd
import numpy as np
import pandas as pd
import rasterio
import requests
import rioxarray as rx
Expand Down Expand Up @@ -108,7 +109,7 @@ def get_GADM_layer(country_list, layer_id, update=False, outlogging=False):
"""
# initialization of the geoDataFrame
geodf_GADM = gpd.GeoDataFrame()
geodf_list = []

for country_code in country_list:
# download file gpkg
Expand Down Expand Up @@ -139,9 +140,10 @@ def get_GADM_layer(country_list, layer_id, update=False, outlogging=False):
geodf_temp["GADM_ID"] = geodf_temp[f"GID_{code_layer}"]

# append geodataframes
geodf_GADM = geodf_GADM.append(geodf_temp)
geodf_list.append(geodf_temp)

geodf_GADM.reset_index(drop=True, inplace=True)
geodf_GADM = gpd.GeoDataFrame(pd.concat(geodf_list, ignore_index=True))
geodf_GADM.set_crs(geodf_list[0].crs, inplace=True)

return geodf_GADM

Expand Down Expand Up @@ -443,6 +445,8 @@ def generalized_mask(src, geom, **kwargs):
"Generalize mask function to account for Polygon and MultiPolygon"
if geom.geom_type == "Polygon":
return mask(src, [geom], **kwargs)
elif geom.geom_type == "MultiPolygon":
return mask(src, geom.geoms, **kwargs)
else:
return mask(src, geom, **kwargs)

Expand Down Expand Up @@ -556,8 +560,10 @@ def _process_func_pop(c_code):

with rasterio.open(WorldPop_inputfile) as src:

for i, row in country_rows.iterrows():
country_rows.loc[i, "pop"] = _sum_raster_over_mask(row.geometry, src)
for i in country_rows.index:
country_rows.loc[i, "pop"] = _sum_raster_over_mask(
country_rows.geometry.loc[i], src
)

return country_rows

Expand Down Expand Up @@ -656,7 +662,11 @@ def gadm(
df_gadm.rename(columns={"GID_0": "country"}, inplace=True)

# drop useless columns
df_gadm = df_gadm[["country", "GADM_ID", "geometry"]]
df_gadm.drop(
df_gadm.columns.difference(["country", "GADM_ID", "geometry"]),
axis=1,
inplace=True,
)

# add the population data to the dataset
add_population_data(
Expand Down