Skip to content

Commit

Permalink
code: Option to simplify offshore GADM shapes introduced
Browse files Browse the repository at this point in the history
  • Loading branch information
SermishaNarayana committed Oct 8, 2024
1 parent ffe9c0f commit fa584c7
Showing 1 changed file with 14 additions and 13 deletions.
27 changes: 14 additions & 13 deletions scripts/build_shapes.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def get_countries_shapes(

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

# 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 @@ -178,6 +178,7 @@ def get_eez(
distance=0.01,
minarea=0.01,
tolerance=0.01,
simplify_gadm=True,
):
"""
Creates offshore shapes by buffer smooth countryshape (=offset country
Expand All @@ -202,23 +203,23 @@ def get_eez(
}
).set_index("name")


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))
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))

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:
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 @@ -1182,7 +1183,7 @@ def get_gadm_shapes(
country_shapes_df.to_file(snakemake.output.country_shapes)

offshore_shapes = get_eez(
countries_list, geo_crs, country_shapes_df, EEZ_gpkg, out_logging,
countries_list, geo_crs, country_shapes_df, EEZ_gpkg, out_logging, simplify_gadm=simplify_gadm,
)

offshore_shapes.reset_index().to_file(snakemake.output.offshore_shapes)
Expand Down

0 comments on commit fa584c7

Please sign in to comment.