Skip to content

Commit

Permalink
Revise deprecations build_shapes (#315)
Browse files Browse the repository at this point in the history
* Revise deprecations build_shapes

* Modify release_notes
  • Loading branch information
davide-f authored Mar 29, 2022
1 parent d355d82 commit 2cb3a2a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
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

0 comments on commit 2cb3a2a

Please sign in to comment.