pypsa-eur/scripts/build_bus_regions.py

98 lines
2.8 KiB
Python
Raw Normal View History

"""
2019-08-11 20:34:18 +00:00
Creates Voronoi shapes for each bus representing both onshore and offshore regions.
2019-08-11 09:40:47 +00:00
Relevant Settings
-----------------
2019-08-11 11:17:36 +00:00
.. code:: yaml
countries:
2019-11-14 16:50:24 +00:00
.. seealso::
Documentation of the configuration file ``config.yaml`` at
:ref:`toplevel_cf`
2019-08-11 11:17:36 +00:00
2019-08-11 09:40:47 +00:00
Inputs
------
2019-08-11 20:34:18 +00:00
- ``resources/country_shapes.geojson``: confer :ref:`shapes`
- ``resources/offshore_shapes.geojson``: confer :ref:`shapes`
- ``networks/base.nc``: confer :ref:`base`
2019-08-11 09:40:47 +00:00
Outputs
-------
2019-08-11 20:34:18 +00:00
- ``resources/regions_onshore.geojson``:
.. image:: ../img/regions_onshore.png
2019-08-11 20:34:18 +00:00
:scale: 33 %
- ``resources/regions_offshore.geojson``:
.. image:: ../img/regions_offshore.png
2019-08-11 20:34:18 +00:00
:scale: 33 %
2019-08-11 09:40:47 +00:00
Description
-----------
"""
2019-11-14 16:50:24 +00:00
from vresutils.graph import voronoi_partition_pts
import os
import pandas as pd
import geopandas as gpd
import pypsa
import logging
if __name__ == "__main__":
logging.basicConfig(level=snakemake.config["logging_level"])
countries = snakemake.config['countries']
n = pypsa.Network(snakemake.input.base_network)
country_shapes = gpd.read_file(snakemake.input.country_shapes).set_index('name')['geometry']
offshore_shapes = gpd.read_file(snakemake.input.offshore_shapes).set_index('name')['geometry']
onshore_regions = []
offshore_regions = []
for country in countries:
c_b = n.buses.country == country
onshore_shape = country_shapes[country]
onshore_locs = n.buses.loc[c_b & n.buses.substation_lv, ["x", "y"]]
onshore_regions.append(gpd.GeoDataFrame({
2019-10-24 14:22:57 +00:00
'name': onshore_locs.index,
'x': onshore_locs['x'],
'y': onshore_locs['y'],
'geometry': voronoi_partition_pts(onshore_locs.values, onshore_shape),
'country': country
2019-10-24 14:22:57 +00:00
}))
if country not in offshore_shapes.index: continue
offshore_shape = offshore_shapes[country]
offshore_locs = n.buses.loc[c_b & n.buses.substation_off, ["x", "y"]]
offshore_regions_c = gpd.GeoDataFrame({
2019-10-24 14:22:57 +00:00
'name': offshore_locs.index,
'x': offshore_locs['x'],
'y': offshore_locs['y'],
'geometry': voronoi_partition_pts(offshore_locs.values, offshore_shape),
'country': country
}, index=offshore_locs.index)
offshore_regions_c = offshore_regions_c.loc[offshore_regions_c.area > 1e-2]
offshore_regions.append(offshore_regions_c)
def save_to_geojson(s, fn):
if os.path.exists(fn):
os.unlink(fn)
2019-10-24 14:22:57 +00:00
schema = {**gpd.io.file.infer_schema(s), 'geometry': 'Unknown'}
s.to_file(fn, driver='GeoJSON', schema=schema)
save_to_geojson(pd.concat(onshore_regions), snakemake.output.regions_onshore)
save_to_geojson(pd.concat(offshore_regions), snakemake.output.regions_offshore)