2022-09-16 13:04:04 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2023-02-16 10:50:55 +00:00
|
|
|
# SPDX-FileCopyrightText: : 2017-2023 The PyPSA-Eur Authors
|
2022-06-27 17:00:41 +00:00
|
|
|
#
|
|
|
|
# SPDX-License-Identifier: MIT
|
|
|
|
"""
|
2022-09-16 13:20:10 +00:00
|
|
|
Rasters the vector data of the `Natura 2000.
|
|
|
|
|
2022-09-16 13:04:04 +00:00
|
|
|
<https://en.wikipedia.org/wiki/Natura_2000>`_ natural protection areas onto all
|
|
|
|
cutout regions.
|
2022-06-27 17:00:41 +00:00
|
|
|
|
|
|
|
Relevant Settings
|
|
|
|
-----------------
|
|
|
|
|
|
|
|
.. code:: yaml
|
|
|
|
|
|
|
|
renewable:
|
|
|
|
{technology}:
|
|
|
|
cutout:
|
|
|
|
|
|
|
|
.. seealso::
|
2023-04-21 08:41:44 +00:00
|
|
|
Documentation of the configuration file ``config/config.yaml`` at
|
2022-06-27 17:00:41 +00:00
|
|
|
:ref:`renewable_cf`
|
|
|
|
|
|
|
|
Inputs
|
|
|
|
------
|
|
|
|
|
|
|
|
- ``data/bundle/natura/Natura2000_end2015.shp``: `Natura 2000 <https://en.wikipedia.org/wiki/Natura_2000>`_ natural protection areas.
|
|
|
|
|
2023-03-09 12:28:42 +00:00
|
|
|
.. image:: img/natura.png
|
2022-06-27 17:00:41 +00:00
|
|
|
:scale: 33 %
|
|
|
|
|
|
|
|
Outputs
|
|
|
|
-------
|
|
|
|
|
|
|
|
- ``resources/natura.tiff``: Rasterized version of `Natura 2000 <https://en.wikipedia.org/wiki/Natura_2000>`_ natural protection areas to reduce computation times.
|
|
|
|
|
2023-03-09 12:28:42 +00:00
|
|
|
.. image:: img/natura.png
|
2022-06-27 17:00:41 +00:00
|
|
|
:scale: 33 %
|
|
|
|
|
|
|
|
Description
|
|
|
|
-----------
|
|
|
|
"""
|
|
|
|
|
|
|
|
import logging
|
|
|
|
|
|
|
|
import atlite
|
|
|
|
import geopandas as gpd
|
|
|
|
import rasterio as rio
|
2023-08-15 13:02:41 +00:00
|
|
|
from _helpers import configure_logging, set_scenario_config
|
2022-06-27 17:00:41 +00:00
|
|
|
from rasterio.features import geometry_mask
|
|
|
|
from rasterio.warp import transform_bounds
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
def determine_cutout_xXyY(cutout_name):
|
2023-04-26 13:03:34 +00:00
|
|
|
"""
|
|
|
|
Determine the full extent of a cutout.
|
|
|
|
|
|
|
|
Since the coordinates of the cutout data are given as the
|
|
|
|
center of the grid cells, the extent of the cutout is
|
|
|
|
calculated by adding/subtracting half of the grid cell size.
|
|
|
|
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
cutout_name : str
|
|
|
|
Path to the cutout.
|
|
|
|
|
|
|
|
Returns
|
|
|
|
-------
|
|
|
|
A list of extent coordinates in the order [x, X, y, Y].
|
|
|
|
"""
|
2022-06-27 17:00:41 +00:00
|
|
|
cutout = atlite.Cutout(cutout_name)
|
|
|
|
assert cutout.crs.to_epsg() == 4326
|
|
|
|
x, X, y, Y = cutout.extent
|
|
|
|
dx, dy = cutout.dx, cutout.dy
|
2022-09-16 13:04:04 +00:00
|
|
|
return [x - dx / 2.0, X + dx / 2.0, y - dy / 2.0, Y + dy / 2.0]
|
2022-06-27 17:00:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
def get_transform_and_shape(bounds, res):
|
2022-09-16 13:04:04 +00:00
|
|
|
left, bottom = [(b // res) * res for b in bounds[:2]]
|
2022-06-27 17:00:41 +00:00
|
|
|
right, top = [(b // res + 1) * res for b in bounds[2:]]
|
|
|
|
shape = int((top - bottom) // res), int((right - left) / res)
|
|
|
|
transform = rio.Affine(res, 0, left, 0, -res, top)
|
|
|
|
return transform, shape
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2022-09-16 13:04:04 +00:00
|
|
|
if "snakemake" not in globals():
|
2022-06-27 17:00:41 +00:00
|
|
|
from _helpers import mock_snakemake
|
2022-09-16 13:04:04 +00:00
|
|
|
|
|
|
|
snakemake = mock_snakemake("build_natura_raster")
|
2022-06-27 17:00:41 +00:00
|
|
|
configure_logging(snakemake)
|
2023-08-15 13:02:41 +00:00
|
|
|
set_scenario_config(snakemake)
|
2022-06-27 17:00:41 +00:00
|
|
|
|
|
|
|
cutouts = snakemake.input.cutouts
|
|
|
|
xs, Xs, ys, Ys = zip(*(determine_cutout_xXyY(cutout) for cutout in cutouts))
|
|
|
|
bounds = transform_bounds(4326, 3035, min(xs), min(ys), max(Xs), max(Ys))
|
|
|
|
transform, out_shape = get_transform_and_shape(bounds, res=100)
|
|
|
|
|
|
|
|
# adjusted boundaries
|
|
|
|
shapes = gpd.read_file(snakemake.input.natura).to_crs(3035)
|
2022-10-31 10:47:21 +00:00
|
|
|
raster = ~geometry_mask(shapes.geometry, out_shape, transform)
|
2022-06-27 17:00:41 +00:00
|
|
|
raster = raster.astype(rio.uint8)
|
|
|
|
|
2022-09-16 13:04:04 +00:00
|
|
|
with rio.open(
|
|
|
|
snakemake.output[0],
|
|
|
|
"w",
|
|
|
|
driver="GTiff",
|
|
|
|
dtype=rio.uint8,
|
|
|
|
count=1,
|
|
|
|
transform=transform,
|
|
|
|
crs=3035,
|
|
|
|
compress="lzw",
|
|
|
|
width=raster.shape[1],
|
|
|
|
height=raster.shape[0],
|
|
|
|
) as dst:
|
2022-06-27 17:00:41 +00:00
|
|
|
dst.write(raster, indexes=1)
|