Skip to content

Commit

Permalink
Unify save_to_geojson definition
Browse files Browse the repository at this point in the history
  • Loading branch information
davide-f committed Jan 2, 2025
1 parent da1cfab commit d463de7
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 25 deletions.
18 changes: 1 addition & 17 deletions scripts/build_shapes.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
BASE_DIR,
configure_logging,
create_logger,
save_to_geojson,
three_2_two_digits_country,
two_2_three_digits_country,
two_digits_2_name_country,
Expand Down Expand Up @@ -305,23 +306,6 @@ def country_cover(country_shapes, eez_shapes=None, out_logging=False, distance=0
return africa_shape


def save_to_geojson(df, fn):
if os.path.exists(fn):
os.unlink(fn) # remove file if it exists
if not isinstance(df, gpd.GeoDataFrame):
df = gpd.GeoDataFrame(dict(geometry=df))

# save file if the GeoDataFrame is non-empty
if df.shape[0] > 0:
df = df.reset_index()
schema = {**gpd.io.file.infer_schema(df), "geometry": "Unknown"}
df.to_file(fn, driver="GeoJSON", schema=schema, engine="fiona")
else:
# create empty file to avoid issues with snakemake
with open(fn, "w") as fp:
pass


def load_EEZ(countries_codes, geo_crs, EEZ_gpkg="./data/eez/eez_v11.gpkg"):
"""
Function to load the database of the Exclusive Economic Zones.
Expand Down
8 changes: 0 additions & 8 deletions scripts/cluster_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -634,14 +634,6 @@ def clustering_for_n_clusters(
return clustering


def save_to_geojson(s, fn):
if os.path.exists(fn):
os.unlink(fn)
df = s.reset_index()
schema = {**gpd.io.file.infer_schema(df), "geometry": "Unknown"}
df.to_file(fn, driver="GeoJSON", schema=schema, engine="fiona")


def cluster_regions(busmaps, inputs, output):
busmap = reduce(lambda x, y: x.map(y), busmaps[1:], busmaps[0])

Expand Down

0 comments on commit d463de7

Please sign in to comment.