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

Introduction of configuration option to simplify GADM tolerance #1147

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
161 changes: 77 additions & 84 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -191,17 +191,17 @@ The documentation is available here: [documentation](https://pypsa-earth.readthe
<table>
<tr>
<td align="center">
<a href="https://github.com/FabianHofmann">
<img src="https://avatars.githubusercontent.com/u/19226431?v=4" width="100;" alt="FabianHofmann"/>
<a href="https://github.com/SermishaNarayana">
<img src="https://avatars.githubusercontent.com/u/156903227?v=4" width="100;" alt="SermishaNarayana"/>
<br />
<sub><b>Fabian Hofmann</b></sub>
<sub><b>Null</b></sub>
</a>
</td>
<td align="center">
<a href="https://github.com/fneum">
<img src="https://avatars.githubusercontent.com/u/29101152?v=4" width="100;" alt="fneum"/>
<a href="https://github.com/davide-f">
<img src="https://avatars.githubusercontent.com/u/67809479?v=4" width="100;" alt="davide-f"/>
<br />
<sub><b>Fabian Neumann</b></sub>
<sub><b>Davide-f</b></sub>
</a>
</td>
<td align="center">
Expand All @@ -212,60 +212,39 @@ The documentation is available here: [documentation](https://pypsa-earth.readthe
</a>
</td>
<td align="center">
<a href="https://github.com/euronion">
<img src="https://avatars.githubusercontent.com/u/42553970?v=4" width="100;" alt="euronion"/>
<a href="https://github.com/pz-max">
<img src="https://avatars.githubusercontent.com/u/61968949?v=4" width="100;" alt="pz-max"/>
<br />
<sub><b>Euronion</b></sub>
<sub><b>Max Parzen</b></sub>
</a>
</td>
<td align="center">
<a href="https://github.com/Justus-coded">
<img src="https://avatars.githubusercontent.com/u/44394641?v=4" width="100;" alt="Justus-coded"/>
<a href="https://github.com/DeniseGiub">
<img src="https://avatars.githubusercontent.com/u/113139589?v=4" width="100;" alt="DeniseGiub"/>
<br />
<sub><b>Justus Ilemobayo</b></sub>
<sub><b>DeniseGiub</b></sub>
</a>
</td>
<td align="center">
<a href="https://github.com/mnm-matin">
<img src="https://avatars.githubusercontent.com/u/45293386?v=4" width="100;" alt="mnm-matin"/>
<a href="https://github.com/yerbol-akhmetov">
<img src="https://avatars.githubusercontent.com/u/113768325?v=4" width="100;" alt="yerbol-akhmetov"/>
<br />
<sub><b>Mnm-matin</b></sub>
<sub><b>Yerbol Akhmetov</b></sub>
</a>
</td></tr>
<tr>
<td align="center">
<a href="https://github.com/martacki">
<img src="https://avatars.githubusercontent.com/u/53824825?v=4" width="100;" alt="martacki"/>
<br />
<sub><b>Martha Frysztacki</b></sub>
</a>
</td>
<td align="center">
<a href="https://github.com/LukasFrankenQ">
<img src="https://avatars.githubusercontent.com/u/55196140?v=4" width="100;" alt="LukasFrankenQ"/>
<br />
<sub><b>Lukas Franken</b></sub>
</a>
</td>
<td align="center">
<a href="https://github.com/pz-max">
<img src="https://avatars.githubusercontent.com/u/61968949?v=4" width="100;" alt="pz-max"/>
<br />
<sub><b>Max Parzen</b></sub>
</a>
</td>
<td align="center">
<a href="https://github.com/davide-f">
<img src="https://avatars.githubusercontent.com/u/67809479?v=4" width="100;" alt="davide-f"/>
<a href="https://github.com/GbotemiB">
<img src="https://avatars.githubusercontent.com/u/48842684?v=4" width="100;" alt="GbotemiB"/>
<br />
<sub><b>Davide-f</b></sub>
<sub><b>Emmanuel Bolarinwa</b></sub>
</a>
</td>
<td align="center">
<a href="https://github.com/koen-vg">
<img src="https://avatars.githubusercontent.com/u/74298901?v=4" width="100;" alt="koen-vg"/>
<a href="https://github.com/mnm-matin">
<img src="https://avatars.githubusercontent.com/u/45293386?v=4" width="100;" alt="mnm-matin"/>
<br />
<sub><b>Koen Van Greevenbroek</b></sub>
<sub><b>Mnm-matin</b></sub>
</a>
</td>
<td align="center">
Expand All @@ -274,57 +253,35 @@ The documentation is available here: [documentation](https://pypsa-earth.readthe
<br />
<sub><b>Hazem</b></sub>
</a>
</td></tr>
<tr>
</td>
<td align="center">
<a href="https://github.com/energyLS">
<img src="https://avatars.githubusercontent.com/u/89515385?v=4" width="100;" alt="energyLS"/>
<br />
<sub><b>EnergyLS</b></sub>
</a>
</td>
<td align="center">
<a href="https://github.com/AnasAlgarei">
<img src="https://avatars.githubusercontent.com/u/101210563?v=4" width="100;" alt="AnasAlgarei"/>
<br />
<sub><b>AnasAlgarei</b></sub>
</a>
</td>
<td align="center">
<a href="https://github.com/yerbol-akhmetov">
<img src="https://avatars.githubusercontent.com/u/113768325?v=4" width="100;" alt="yerbol-akhmetov"/>
<br />
<sub><b>Yerbol Akhmetov</b></sub>
</a>
</td>
<td align="center">
<a href="https://github.com/DeniseGiub">
<img src="https://avatars.githubusercontent.com/u/113139589?v=4" width="100;" alt="DeniseGiub"/>
<br />
<sub><b>DeniseGiub</b></sub>
</a>
</td>
<td align="center">
<a href="https://github.com/GbotemiB">
<img src="https://avatars.githubusercontent.com/u/48842684?v=4" width="100;" alt="GbotemiB"/>
<br />
<sub><b>Emmanuel Bolarinwa</b></sub>
</a>
</td>
<td align="center">
<a href="https://github.com/Tomkourou">
<img src="https://avatars.githubusercontent.com/u/5240283?v=4" width="100;" alt="Tomkourou"/>
<br />
<sub><b>Thomas Kouroughli</b></sub>
</a>
</td></tr>
<tr>
</td>
<td align="center">
<a href="https://github.com/GridGrapher">
<img src="https://avatars.githubusercontent.com/u/127969728?v=4" width="100;" alt="GridGrapher"/>
<br />
<sub><b>GridGrapher</b></sub>
</a>
</td></tr>
<tr>
<td align="center">
<a href="https://github.com/martacki">
<img src="https://avatars.githubusercontent.com/u/53824825?v=4" width="100;" alt="martacki"/>
<br />
<sub><b>Martha Frysztacki</b></sub>
</a>
</td>
<td align="center">
<a href="https://github.com/finozzifa">
Expand Down Expand Up @@ -369,6 +326,27 @@ The documentation is available here: [documentation](https://pypsa-earth.readthe
<sub><b>Cschau</b></sub>
</a>
</td>
<td align="center">
<a href="https://github.com/euronion">
<img src="https://avatars.githubusercontent.com/u/42553970?v=4" width="100;" alt="euronion"/>
<br />
<sub><b>Euronion</b></sub>
</a>
</td>
<td align="center">
<a href="https://github.com/AnasAlgarei">
<img src="https://avatars.githubusercontent.com/u/101210563?v=4" width="100;" alt="AnasAlgarei"/>
<br />
<sub><b>AnasAlgarei</b></sub>
</a>
</td>
<td align="center">
<a href="https://github.com/LukasFrankenQ">
<img src="https://avatars.githubusercontent.com/u/55196140?v=4" width="100;" alt="LukasFrankenQ"/>
<br />
<sub><b>Lukas Franken</b></sub>
</a>
</td>
<td align="center">
<a href="https://github.com/Tooblippe">
<img src="https://avatars.githubusercontent.com/u/805313?v=4" width="100;" alt="Tooblippe"/>
Expand All @@ -382,14 +360,22 @@ The documentation is available here: [documentation](https://pypsa-earth.readthe
<br />
<sub><b>Anton Achhammer</b></sub>
</a>
</td>
</td></tr>
<tr>
<td align="center">
<a href="https://github.com/asolavi">
<img src="https://avatars.githubusercontent.com/u/131155817?v=4" width="100;" alt="asolavi"/>
<br />
<sub><b>Null</b></sub>
</a>
</td>
<td align="center">
<a href="https://github.com/koen-vg">
<img src="https://avatars.githubusercontent.com/u/74298901?v=4" width="100;" alt="koen-vg"/>
<br />
<sub><b>Koen Van Greevenbroek</b></sub>
</a>
</td>
<td align="center">
<a href="https://github.com/carlosfv92">
<img src="https://avatars.githubusercontent.com/u/103258059?v=4" width="100;" alt="carlosfv92"/>
Expand All @@ -403,8 +389,7 @@ The documentation is available here: [documentation](https://pypsa-earth.readthe
<br />
<sub><b>Stephen J Lee</b></sub>
</a>
</td></tr>
<tr>
</td>
<td align="center">
<a href="https://github.com/rsparks3">
<img src="https://avatars.githubusercontent.com/u/30065966?v=4" width="100;" alt="rsparks3"/>
Expand All @@ -418,7 +403,8 @@ The documentation is available here: [documentation](https://pypsa-earth.readthe
<br />
<sub><b>Null</b></sub>
</a>
</td>
</td></tr>
<tr>
<td align="center">
<a href="https://github.com/juli-a-ko">
<img src="https://avatars.githubusercontent.com/u/126512394?v=4" width="100;" alt="juli-a-ko"/>
Expand Down Expand Up @@ -446,8 +432,7 @@ The documentation is available here: [documentation](https://pypsa-earth.readthe
<br />
<sub><b>Pietro Monticone</b></sub>
</a>
</td></tr>
<tr>
</td>
<td align="center">
<a href="https://github.com/Netotse">
<img src="https://avatars.githubusercontent.com/u/89367243?v=4" width="100;" alt="Netotse"/>
Expand All @@ -461,7 +446,8 @@ The documentation is available here: [documentation](https://pypsa-earth.readthe
<br />
<sub><b>Katherine M. Antonio</b></sub>
</a>
</td>
</td></tr>
<tr>
<td align="center">
<a href="https://github.com/jessLryan">
<img src="https://avatars.githubusercontent.com/u/122939887?v=4" width="100;" alt="jessLryan"/>
Expand All @@ -483,21 +469,28 @@ The documentation is available here: [documentation](https://pypsa-earth.readthe
<sub><b>HanaElattar</b></sub>
</a>
</td>
<td align="center">
<a href="https://github.com/FabianHofmann">
<img src="https://avatars.githubusercontent.com/u/19226431?v=4" width="100;" alt="FabianHofmann"/>
<br />
<sub><b>Fabian Hofmann</b></sub>
</a>
</td>
<td align="center">
<a href="https://github.com/EmreYorat">
<img src="https://avatars.githubusercontent.com/u/93644024?v=4" width="100;" alt="EmreYorat"/>
<br />
<sub><b>EmreYorat</b></sub>
</a>
</td></tr>
<tr>
</td>
<td align="center">
<a href="https://github.com/AndreCNF">
<img src="https://avatars.githubusercontent.com/u/19359510?v=4" width="100;" alt="AndreCNF"/>
<br />
<sub><b>André Cristóvão Neves Ferreira</b></sub>
</a>
</td>
</td></tr>
<tr>
<td align="center">
<a href="https://github.com/AlexanderMeisinger">
<img src="https://avatars.githubusercontent.com/u/91368938?v=4" width="100;" alt="AlexanderMeisinger"/>
Expand Down
1 change: 1 addition & 0 deletions config.default.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ cluster_options:

build_shape_options:
gadm_layer_id: 1 # GADM level area used for the gadm_shapes. Codes are country-dependent but roughly: 0: country, 1: region/county-like, 2: municipality-like
simplify_tolerance: 0.01 # Default value is 0.01, higher the value more is the simplification of the GADM shapes
update_file: false # When true, all the input files are downloaded again and replace the existing files
out_logging: true # When true, logging is printed to console
year: 2020 # reference year used to derive shapes, info on population and info on GDP
Expand Down
19 changes: 10 additions & 9 deletions doc/configtables/build_shape_options.csv
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
,Unit,Values,Description
gadm_layer_id,, "{0, 1, 2}", "GADM level area used for the gadm_shapes. Codes are country-dependent but roughly: 0: country, 1: region/county-like, 2: municipality-like."
update_file, bool, "{True, False}", "True: all input files are downloaded again and replace the existing files."
out_logging, bool, "{True, False}", "True: Logging is printed in the console."
year,, "past year; e.g. YYYY", "Reference year used to derive shapes, info on population and info on GDP."
nprocesses, int,, "Number of processes to be used in build_shapes."
worldpop_method,, "{""standard"", ""api"", false}", "Specifies how population is added to every shape: ""standard"" pulls from web 1kmx1km raster; ""api"" pulls from API 100mx100m raster; false (not ""false"") no population addition to shape. This is useful when generating only cutout."
gdp_method,, "{""standard"", false}", "Specifies how GDP is added to every shape: ""standard"" pulls from web 1x1km raster; false (not ""false"") no gdp addition to shape. This is useful when generating only cutout."
contended_flag,, "{""set_by_country"", ""drop""}", "Specifies what to do with contended countries: ""set_by_country"" assigns the contended areas to the countries according to the GADM database; ""drop"" drops the contended areas from the model."
,Unit,Values,Description,,,,
gadm_layer_id,,"""{0",1,"2}""","""GADM level area used for the gadm_shapes. Codes are country-dependent but roughly: 0: country",1: region/county-like,"2: municipality-like."""
simplify_tolerance,float,>= 0,Specifies tolerance limit when simplifying GADM shapes. A higher value indicates more simplification,,,,
update_file, bool," ""{True"," False}"""," ""True: all input files are downloaded again and replace the existing files.""",,,
out_logging, bool," ""{True"," False}"""," ""True: Logging is printed in the console.""",,,
year,," ""past year; e.g. YYYY"""," ""Reference year used to derive shapes"," info on population and info on GDP.""",,,
nprocesses, int,," ""Number of processes to be used in build_shapes.""",,,,
worldpop_method,," ""{""standard"""," ""api"""," false}""","""Specifies how population is added to every shape: ""standard"" pulls from web 1kmx1km raster; ""api"" pulls from API 100mx100m raster; false (not ""false"") no population addition to shape. This is useful when generating only cutout.""",,
gdp_method,," ""{""standard"""," false}""","""Specifies how GDP is added to every shape: ""standard"" pulls from web 1x1km raster; false (not ""false"") no gdp addition to shape. This is useful when generating only cutout.""",,,
contended_flag,," ""{""set_by_country"""," ""drop""}"""," ""Specifies what to do with contended countries: ""set_by_country"" assigns the contended areas to the countries according to the GADM database; ""drop"" drops the contended areas from the model.""",,,
3 changes: 3 additions & 0 deletions doc/release_notes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ E.g. if a new rule becomes available describe how to use it `make test` and in o

* Enable configfile specification for mock_snakemake `PR #1135 <https://github.com/pypsa-meets-earth/pypsa-earth/pull/1135>`_

* Included a configuration option to quantitavely set the tolerance levels for simplifying GADM shapes `PR #1147 <https://github.com/pypsa-meets-earth/pypsa-earth/pull/1147/commits>`__


PyPSA-Earth 0.4.1
=================

Expand Down
13 changes: 9 additions & 4 deletions scripts/build_shapes.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ def _simplify_polys(polys, minarea=0.01, tolerance=0.01, filterremote=False):
return polys.simplify(tolerance=tolerance)


def countries(countries, geo_crs, contended_flag, update=False, out_logging=False):
def countries(countries, geo_crs, contended_flag, update=False, out_logging=False, tolerance=0.01):
"Create country shapes"

if out_logging:
Expand All @@ -282,7 +282,8 @@ def countries(countries, geo_crs, contended_flag, update=False, out_logging=Fals
df_countries.rename(columns={"GID_0": "name"}, inplace=True)

# set index and simplify polygons
ret_df = df_countries.set_index("name")["geometry"].map(_simplify_polys)
ret_df = df_countries.set_index("name")["geometry"].map(lambda x: _simplify_polys(x,tolerance=tolerance))

# there may be "holes" in the countries geometry which cause troubles along the workflow
# e.g. that is the case for enclaves like Dahagram–Angarpota for IN/BD
ret_df = ret_df.make_valid()
Expand Down Expand Up @@ -1256,6 +1257,7 @@ def gadm(
out_logging=False,
year=2020,
nprocesses=None,
tolerance=0.01,
):
if out_logging:
logger.info("Stage 3 of 5: Creation GADM GeoDataFrame")
Expand Down Expand Up @@ -1305,7 +1307,7 @@ def gadm(
lambda x: x if x.find(".") == 0 else "." + x
)
df_gadm.set_index("GADM_ID", inplace=True)
df_gadm["geometry"] = df_gadm["geometry"].map(_simplify_polys)
df_gadm["geometry"] = df_gadm["geometry"].map(lambda x: _simplify_polys(x,tolerance=tolerance))
df_gadm.geometry = df_gadm.geometry.apply(
lambda r: make_valid(r) if not r.is_valid else r
)
Expand Down Expand Up @@ -1338,18 +1340,20 @@ def gadm(
contended_flag = snakemake.params.build_shape_options["contended_flag"]
worldpop_method = snakemake.params.build_shape_options["worldpop_method"]
gdp_method = snakemake.params.build_shape_options["gdp_method"]
tolerance = snakemake.params.build_shape_options['simplify_tolerance']

country_shapes = countries(
countries_list,
geo_crs,
contended_flag,
update,
out_logging,
tolerance=tolerance,
)
country_shapes.to_file(snakemake.output.country_shapes)

offshore_shapes = eez(
countries_list, geo_crs, country_shapes, EEZ_gpkg, out_logging
countries_list, geo_crs, country_shapes, EEZ_gpkg, out_logging, tolerance=tolerance
)

offshore_shapes.reset_index().to_file(snakemake.output.offshore_shapes)
Expand All @@ -1371,5 +1375,6 @@ def gadm(
out_logging,
year,
nprocesses=nprocesses,
tolerance=tolerance,
)
save_to_geojson(gadm_shapes, out.gadm_shapes)
Loading