Skip to content

Commit

Permalink
EEZ: Update EEZ to v12, auto-download and remove from databundle (PyP…
Browse files Browse the repository at this point in the history
…SA#1188)

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

* eez: do not simplify as it distorts topology

* remove missed merge conflicts
  • Loading branch information
fneum authored Aug 7, 2024
1 parent c907d59 commit fb41016
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 29 deletions.
1 change: 0 additions & 1 deletion doc/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@
autodoc_mock_imports = [
"atlite",
"snakemake",
"pycountry",
"rioxarray",
"country_converter",
"tabula",
Expand Down
1 change: 0 additions & 1 deletion doc/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ tabula-py

# cartopy
scikit-learn
pycountry
pyyaml
seaborn
memory_profiler
Expand Down
1 change: 0 additions & 1 deletion envs/environment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ dependencies:
# Dependencies of the workflow itself
- xlrd
- openpyxl!=3.1.1
- pycountry
- seaborn
- snakemake-minimal>=8.14
- memory_profiler
Expand Down
2 changes: 1 addition & 1 deletion rules/build_electricity.smk
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down
36 changes: 35 additions & 1 deletion rules/retrieve.smk
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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
Expand Down
33 changes: 9 additions & 24 deletions scripts/build_shapes.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
.. image:: img/countries.png
: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
:scale: 33 %
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit fb41016

Please sign in to comment.