Skip to content

Commit

Permalink
feature: Introducing configurable option to simplify GADM shapes
Browse files Browse the repository at this point in the history
  • Loading branch information
SermishaNarayana committed Oct 10, 2024
1 parent e9ac465 commit 4993072
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 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_gadm: false # When true, shape polygons are simplified else no
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
32 changes: 20 additions & 12 deletions scripts/build_shapes.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,7 @@ def eez(
distance=0.01,
minarea=0.01,
tolerance=0.01,
simplify_gadm=True,
):
"""
Creates offshore shapes by buffer smooth countryshape (=offset country
Expand All @@ -386,22 +387,24 @@ def eez(
}
).set_index("name")

ret_df = ret_df.geometry.map(
lambda x: _simplify_polys(x, minarea=minarea, tolerance=tolerance)
)
if simplify_gadm:
ret_df = ret_df.geometry.map(
lambda x: _simplify_polys(x, minarea=minarea, tolerance=tolerance)
)

ret_df = ret_df.apply(lambda x: make_valid(x))
ret_df = ret_df.apply(lambda x: make_valid(x))

country_shapes_with_buffer = country_shapes.buffer(distance)
ret_df_new = ret_df.difference(country_shapes_with_buffer)

# repeat to simplify after the buffer correction
ret_df_new = ret_df_new.map(
lambda x: (
x if x is None else _simplify_polys(x, minarea=minarea, tolerance=tolerance)
if simplify_gadm:
# repeat to simplify after the buffer correction
ret_df_new = ret_df_new.map(
lambda x: (
x if x is None else _simplify_polys(x, minarea=minarea, tolerance=tolerance)
)
)
)
ret_df_new = ret_df_new.apply(lambda x: x if x is None else make_valid(x))
ret_df_new = ret_df_new.apply(lambda x: x if x is None else make_valid(x))

# Drops empty geometry
ret_df = ret_df_new.dropna()
Expand Down Expand Up @@ -1256,6 +1259,7 @@ def gadm(
out_logging=False,
year=2020,
nprocesses=None,
simplify_gadm=True,
):
if out_logging:
logger.info("Stage 3 of 5: Creation GADM GeoDataFrame")
Expand Down Expand Up @@ -1305,7 +1309,9 @@ 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)

if simplify_gadm:
df_gadm["geometry"] = df_gadm["geometry"].map(_simplify_polys)
df_gadm.geometry = df_gadm.geometry.apply(
lambda r: make_valid(r) if not r.is_valid else r
)
Expand Down Expand Up @@ -1338,6 +1344,7 @@ 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"]
simplify_gadm = snakemake.params.build_shape_options['simplify_gadm']

country_shapes = countries(
countries_list,
Expand All @@ -1349,7 +1356,7 @@ def gadm(
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, simplify_gadm
)

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

0 comments on commit 4993072

Please sign in to comment.