EEZ: Update EEZ to v12, auto-download and remove from databundle (#1188)

* eez: update to version 12, autodownload, remove pycountry

* eez: do not simplify as it distorts topology

* remove missed merge conflicts
This commit is contained in:
Fabian Neumann 2024-08-07 15:28:55 +02:00 committed by GitHub
parent c907d59253
commit fb41016c60
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 45 additions and 29 deletions

View File

@ -53,7 +53,6 @@ extensions = [
autodoc_mock_imports = [ autodoc_mock_imports = [
"atlite", "atlite",
"snakemake", "snakemake",
"pycountry",
"rioxarray", "rioxarray",
"country_converter", "country_converter",
"tabula", "tabula",

View File

@ -17,7 +17,6 @@ tabula-py
# cartopy # cartopy
scikit-learn scikit-learn
pycountry
pyyaml pyyaml
seaborn seaborn
memory_profiler memory_profiler

View File

@ -18,7 +18,6 @@ dependencies:
# Dependencies of the workflow itself # Dependencies of the workflow itself
- xlrd - xlrd
- openpyxl!=3.1.1 - openpyxl!=3.1.1
- pycountry
- seaborn - seaborn
- snakemake-minimal>=8.14 - snakemake-minimal>=8.14
- memory_profiler - memory_profiler

View File

@ -92,7 +92,7 @@ rule build_shapes:
countries=config_provider("countries"), countries=config_provider("countries"),
input: input:
naturalearth=ancient("data/naturalearth/ne_10m_admin_0_countries_deu.shp"), 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"), nuts3=ancient("data/bundle/NUTS_2013_60M_SH/data/NUTS_RG_60M_2013.shp"),
nuts3pop=ancient("data/bundle/nama_10r_3popgdp.tsv.gz"), nuts3pop=ancient("data/bundle/nama_10r_3popgdp.tsv.gz"),
nuts3gdp=ancient("data/bundle/nama_10r_3gdp.tsv.gz"), nuts3gdp=ancient("data/bundle/nama_10r_3gdp.tsv.gz"),

View File

@ -16,7 +16,6 @@ if config["enable"]["retrieve"] is False:
if config["enable"]["retrieve"] and config["enable"].get("retrieve_databundle", True): if config["enable"]["retrieve"] and config["enable"].get("retrieve_databundle", True):
datafiles = [ datafiles = [
"je-e-21.03.02.xls", "je-e-21.03.02.xls",
"eez/World_EEZ_v8_2014.shp",
"NUTS_2013_60M_SH/data/NUTS_RG_60M_2013.shp", "NUTS_2013_60M_SH/data/NUTS_RG_60M_2013.shp",
"nama_10r_3popgdp.tsv.gz", "nama_10r_3popgdp.tsv.gz",
"nama_10r_3gdp.tsv.gz", "nama_10r_3gdp.tsv.gz",
@ -215,6 +214,41 @@ if config["enable"]["retrieve"]:
move(input[0], output[0]) 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"]: if config["enable"]["retrieve"]:
# Download directly from naciscdn.org which is a redirect from naturalearth.com # Download directly from naciscdn.org which is a redirect from naturalearth.com

View File

@ -26,7 +26,7 @@ Inputs
.. image:: img/countries.png .. image:: img/countries.png
:scale: 33 % :scale: 33 %
- ``data/bundle/eez/World_EEZ_v8_2014.shp``: World `exclusive economic zones <https://en.wikipedia.org/wiki/Exclusive_economic_zone>`_ (EEZ) - ``data/eez/World_EEZ_v12_20231025_gpkg/eez_v12.gpkg ``: World `exclusive economic zones <https://en.wikipedia.org/wiki/Exclusive_economic_zone>`_ (EEZ)
.. image:: img/eez.png .. image:: img/eez.png
:scale: 33 % :scale: 33 %
@ -76,19 +76,13 @@ from operator import attrgetter
import geopandas as gpd import geopandas as gpd
import numpy as np import numpy as np
import pandas as pd import pandas as pd
import pycountry as pyc import country_converter as coco
from _helpers import configure_logging, set_scenario_config from _helpers import configure_logging, set_scenario_config
from shapely.geometry import MultiPolygon, Polygon from shapely.geometry import MultiPolygon, Polygon
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
cc = coco.CountryConverter()
def _get_country(target, **keys):
assert len(keys) == 1
try:
return getattr(pyc.countries.get(**keys), target)
except (KeyError, AttributeError):
return np.nan
def _simplify_polys(polys, minarea=0.1, tolerance=None, filterremote=True): def _simplify_polys(polys, minarea=0.1, tolerance=None, filterremote=True):
@ -135,22 +129,15 @@ def countries(naturalearth, country_list):
return s return s
def eez(country_shapes, eez, country_list): def eez(eez, country_list):
df = gpd.read_file(eez) df = gpd.read_file(eez)
df = df.loc[ iso3_list = cc.convert(country_list, src="ISO2", to="ISO3")
df["ISO_3digit"].isin( df = df.query("ISO_TER1 in @iso3_list and POL_TYPE == '200NM'").copy()
[_get_country("alpha_3", alpha_2=c) for c in country_list] df["name"] = cc.convert(df.ISO_TER1, src="ISO3", to="ISO2")
)
]
df["name"] = df["ISO_3digit"].map(lambda c: _get_country("alpha_2", alpha_3=c))
s = df.set_index("name").geometry.map( s = df.set_index("name").geometry.map(
lambda s: _simplify_polys(s, filterremote=False) lambda s: _simplify_polys(s, filterremote=False)
) )
s = gpd.GeoSeries( s = s.to_frame("geometry").set_crs(df.crs)
{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.index.name = "name" s.index.name = "name"
return s return s
@ -262,9 +249,7 @@ if __name__ == "__main__":
country_shapes = countries(snakemake.input.naturalearth, snakemake.params.countries) country_shapes = countries(snakemake.input.naturalearth, snakemake.params.countries)
country_shapes.reset_index().to_file(snakemake.output.country_shapes) country_shapes.reset_index().to_file(snakemake.output.country_shapes)
offshore_shapes = eez( offshore_shapes = eez(snakemake.input.eez, snakemake.params.countries)
country_shapes, snakemake.input.eez, snakemake.params.countries
)
offshore_shapes.reset_index().to_file(snakemake.output.offshore_shapes) offshore_shapes.reset_index().to_file(snakemake.output.offshore_shapes)
europe_shape = gpd.GeoDataFrame( europe_shape = gpd.GeoDataFrame(