diff --git a/doc/conf.py b/doc/conf.py index efce867ed..f0d1ca377 100644 --- a/doc/conf.py +++ b/doc/conf.py @@ -53,7 +53,6 @@ autodoc_mock_imports = [ "atlite", "snakemake", - "pycountry", "rioxarray", "country_converter", "tabula", diff --git a/doc/requirements.txt b/doc/requirements.txt index a1cd0a5c4..dca414fcb 100644 --- a/doc/requirements.txt +++ b/doc/requirements.txt @@ -17,7 +17,6 @@ tabula-py # cartopy scikit-learn -pycountry pyyaml seaborn memory_profiler diff --git a/envs/environment.yaml b/envs/environment.yaml index febd6ea2e..c8d8a6336 100644 --- a/envs/environment.yaml +++ b/envs/environment.yaml @@ -18,7 +18,6 @@ dependencies: # Dependencies of the workflow itself - xlrd - openpyxl!=3.1.1 -- pycountry - seaborn - snakemake-minimal>=8.14 - memory_profiler diff --git a/rules/build_electricity.smk b/rules/build_electricity.smk index 64bd85a1d..34472f27d 100644 --- a/rules/build_electricity.smk +++ b/rules/build_electricity.smk @@ -92,7 +92,7 @@ rule build_shapes: countries=config_provider("countries"), input: naturalearth=ancient("data/naturalearth/ne_10m_admin_0_countries_deu.shp"), - eez=ancient("data/bundle/eez/World_EEZ_v8_2014.shp"), + eez=ancient("data/eez/World_EEZ_v12_20231025_gpkg/eez_v12.gpkg"), nuts3=ancient("data/bundle/NUTS_2013_60M_SH/data/NUTS_RG_60M_2013.shp"), nuts3pop=ancient("data/bundle/nama_10r_3popgdp.tsv.gz"), nuts3gdp=ancient("data/bundle/nama_10r_3gdp.tsv.gz"), diff --git a/rules/retrieve.smk b/rules/retrieve.smk index aa4452156..ffb44baef 100644 --- a/rules/retrieve.smk +++ b/rules/retrieve.smk @@ -16,7 +16,6 @@ if config["enable"]["retrieve"] is False: if config["enable"]["retrieve"] and config["enable"].get("retrieve_databundle", True): datafiles = [ "je-e-21.03.02.xls", - "eez/World_EEZ_v8_2014.shp", "NUTS_2013_60M_SH/data/NUTS_RG_60M_2013.shp", "nama_10r_3popgdp.tsv.gz", "nama_10r_3gdp.tsv.gz", @@ -215,6 +214,41 @@ if config["enable"]["retrieve"]: move(input[0], output[0]) +if config["enable"]["retrieve"]: + + rule retrieve_eez: + params: + zip="data/eez/World_EEZ_v12_20231025_gpkg.zip", + output: + gpkg="data/eez/World_EEZ_v12_20231025_gpkg/eez_v12.gpkg", + run: + import os + import requests + from uuid import uuid4 + + name = str(uuid4())[:8] + org = str(uuid4())[:8] + + response = requests.post( + "https://www.marineregions.org/download_file.php", + params={"name": "World_EEZ_v12_20231025_gpkg.zip"}, + data={ + "name": name, + "organisation": org, + "email": f"{name}@{org}.org", + "country": "Germany", + "user_category": "academia", + "purpose_category": "Research", + "agree": "1", + }, + ) + + with open(params["zip"], "wb") as f: + f.write(response.content) + output_folder = Path(params["zip"]).parent + unpack_archive(params["zip"], output_folder) + os.remove(params["zip"]) + if config["enable"]["retrieve"]: # Download directly from naciscdn.org which is a redirect from naturalearth.com diff --git a/scripts/build_shapes.py b/scripts/build_shapes.py index 93a738582..c2fe7ce63 100644 --- a/scripts/build_shapes.py +++ b/scripts/build_shapes.py @@ -26,7 +26,7 @@ .. image:: img/countries.png :scale: 33 % -- ``data/bundle/eez/World_EEZ_v8_2014.shp``: World `exclusive economic zones `_ (EEZ) +- ``data/eez/World_EEZ_v12_20231025_gpkg/eez_v12.gpkg ``: World `exclusive economic zones `_ (EEZ) .. image:: img/eez.png :scale: 33 % @@ -76,19 +76,13 @@ import geopandas as gpd import numpy as np import pandas as pd -import pycountry as pyc +import country_converter as coco from _helpers import configure_logging, set_scenario_config from shapely.geometry import MultiPolygon, Polygon logger = logging.getLogger(__name__) - -def _get_country(target, **keys): - assert len(keys) == 1 - try: - return getattr(pyc.countries.get(**keys), target) - except (KeyError, AttributeError): - return np.nan +cc = coco.CountryConverter() def _simplify_polys(polys, minarea=0.1, tolerance=None, filterremote=True): @@ -135,22 +129,15 @@ def countries(naturalearth, country_list): return s -def eez(country_shapes, eez, country_list): +def eez(eez, country_list): df = gpd.read_file(eez) - df = df.loc[ - df["ISO_3digit"].isin( - [_get_country("alpha_3", alpha_2=c) for c in country_list] - ) - ] - df["name"] = df["ISO_3digit"].map(lambda c: _get_country("alpha_2", alpha_3=c)) + iso3_list = cc.convert(country_list, src="ISO2", to="ISO3") + df = df.query("ISO_TER1 in @iso3_list and POL_TYPE == '200NM'").copy() + df["name"] = cc.convert(df.ISO_TER1, src="ISO3", to="ISO2") s = df.set_index("name").geometry.map( lambda s: _simplify_polys(s, filterremote=False) ) - s = gpd.GeoSeries( - {k: v for k, v in s.items() if v.distance(country_shapes[k]) < 1e-3}, - crs=df.crs, - ) - s = s.to_frame("geometry") + s = s.to_frame("geometry").set_crs(df.crs) s.index.name = "name" return s @@ -262,9 +249,7 @@ def nuts3(country_shapes, nuts3, nuts3pop, nuts3gdp, ch_cantons, ch_popgdp): country_shapes = countries(snakemake.input.naturalearth, snakemake.params.countries) country_shapes.reset_index().to_file(snakemake.output.country_shapes) - offshore_shapes = eez( - country_shapes, snakemake.input.eez, snakemake.params.countries - ) + offshore_shapes = eez(snakemake.input.eez, snakemake.params.countries) offshore_shapes.reset_index().to_file(snakemake.output.offshore_shapes) europe_shape = gpd.GeoDataFrame(