From 4993072991be24b4031b513d969aad2f89609e56 Mon Sep 17 00:00:00 2001 From: Sermisha Date: Thu, 10 Oct 2024 15:17:05 +0530 Subject: [PATCH] feature: Introducing configurable option to simplify GADM shapes --- config.default.yaml | 1 + scripts/build_shapes.py | 32 ++++++++++++++++++++------------ 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/config.default.yaml b/config.default.yaml index 431812b94..ac2c6b68d 100644 --- a/config.default.yaml +++ b/config.default.yaml @@ -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 diff --git a/scripts/build_shapes.py b/scripts/build_shapes.py index 22e6b68cf..0cadbb8d9 100644 --- a/scripts/build_shapes.py +++ b/scripts/build_shapes.py @@ -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 @@ -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() @@ -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") @@ -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 ) @@ -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, @@ -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) @@ -1371,5 +1378,6 @@ def gadm( out_logging, year, nprocesses=nprocesses, + simplify_gadm=simplify_gadm, ) save_to_geojson(gadm_shapes, out.gadm_shapes)