Skip to content

Commit

Permalink
feature: Feature to add a tolerance value to simplify GADM shapes
Browse files Browse the repository at this point in the history
  • Loading branch information
SermishaNarayana committed Oct 17, 2024
1 parent dca319d commit 9ed4189
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 13 deletions.
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.""",,,
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)

0 comments on commit 9ed4189

Please sign in to comment.