2023-03-06 08:27:45 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2024-02-19 15:21:48 +00:00
|
|
|
# SPDX-FileCopyrightText: : 2020-2024 The PyPSA-Eur Authors
|
2023-03-06 17:49:23 +00:00
|
|
|
#
|
|
|
|
# SPDX-License-Identifier: MIT
|
2023-03-06 08:27:45 +00:00
|
|
|
"""
|
2023-03-09 11:45:43 +00:00
|
|
|
Build spatial distribution of industries from Hotmaps database.
|
2024-06-05 13:02:44 +00:00
|
|
|
|
|
|
|
Inputs
|
|
|
|
-------
|
|
|
|
|
|
|
|
- ``resources/regions_onshore_elec_s{simpl}_{clusters}.geojson``
|
|
|
|
- ``resources/pop_layout_elec_s{simpl}_{clusters}.csv``
|
|
|
|
|
|
|
|
Outputs
|
|
|
|
-------
|
|
|
|
|
|
|
|
- ``resources/industrial_distribution_key_elec_s{simpl}_{clusters}.csv``
|
|
|
|
|
|
|
|
Description
|
|
|
|
-------
|
|
|
|
|
|
|
|
This rule uses the `Hotmaps database <https://gitlab.com/hotmaps/industrial_sites/industrial_sites_Industrial_Database>`. After removing entries without valid locations, it assigns each industrial site to a bus region based on its location.
|
|
|
|
Then, it calculates the nodal distribution key for each sector based on the emissions of the industrial sites in each region. This leads to a distribution key of 1 if there is only one bus per country and <1 if there are multiple buses per country. The sum over buses of one country is 1.
|
|
|
|
|
|
|
|
The following subcategories of industry are considered:
|
|
|
|
- Iron and steel
|
|
|
|
- Cement
|
|
|
|
- Refineries
|
|
|
|
- Paper and printing
|
|
|
|
- Chemical industry
|
|
|
|
- Glass
|
|
|
|
- Non-ferrous metals
|
|
|
|
- Non-metallic mineral products
|
|
|
|
- Other non-classified
|
|
|
|
Furthermore, the population distribution is added
|
|
|
|
- Population
|
2023-03-06 08:27:45 +00:00
|
|
|
"""
|
2020-10-12 10:07:49 +00:00
|
|
|
|
2023-02-23 09:30:32 +00:00
|
|
|
import logging
|
2021-07-01 18:09:04 +00:00
|
|
|
import uuid
|
|
|
|
from itertools import product
|
2023-03-06 08:27:45 +00:00
|
|
|
|
2023-08-24 09:12:05 +00:00
|
|
|
import country_converter as coco
|
2023-03-06 08:27:45 +00:00
|
|
|
import geopandas as gpd
|
|
|
|
import pandas as pd
|
2024-02-12 10:53:20 +00:00
|
|
|
from _helpers import configure_logging, set_scenario_config
|
|
|
|
|
2024-01-19 09:47:58 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
2023-08-22 12:29:00 +00:00
|
|
|
cc = coco.CountryConverter()
|
2020-10-12 10:07:49 +00:00
|
|
|
|
|
|
|
|
2021-07-01 18:09:04 +00:00
|
|
|
def locate_missing_industrial_sites(df):
|
|
|
|
"""
|
2023-03-06 08:27:45 +00:00
|
|
|
Locate industrial sites without valid locations based on city and
|
|
|
|
countries.
|
|
|
|
|
|
|
|
Should only be used if the model's spatial resolution is coarser
|
|
|
|
than individual cities.
|
2021-07-01 18:09:04 +00:00
|
|
|
"""
|
|
|
|
try:
|
|
|
|
from geopy.extra.rate_limiter import RateLimiter
|
2023-03-06 08:27:45 +00:00
|
|
|
from geopy.geocoders import Nominatim
|
2024-01-19 11:16:07 +00:00
|
|
|
except ImportError:
|
2023-03-06 08:27:45 +00:00
|
|
|
raise ModuleNotFoundError(
|
|
|
|
"Optional dependency 'geopy' not found."
|
|
|
|
"Install via 'conda install -c conda-forge geopy'"
|
|
|
|
"or set 'industry: hotmaps_locate_missing: false'."
|
|
|
|
)
|
2020-10-12 10:07:49 +00:00
|
|
|
|
2021-07-01 18:09:04 +00:00
|
|
|
locator = Nominatim(user_agent=str(uuid.uuid4()))
|
|
|
|
geocode = RateLimiter(locator.geocode, min_delay_seconds=2)
|
2020-10-12 10:07:49 +00:00
|
|
|
|
2021-07-01 18:09:04 +00:00
|
|
|
def locate_missing(s):
|
|
|
|
if pd.isna(s.City) or s.City == "CONFIDENTIAL":
|
|
|
|
return None
|
2020-10-12 10:07:49 +00:00
|
|
|
|
2023-03-06 08:27:45 +00:00
|
|
|
loc = geocode([s.City, s.Country], geometry="wkt")
|
2021-07-01 18:09:04 +00:00
|
|
|
if loc is not None:
|
2023-02-23 09:30:32 +00:00
|
|
|
logger.debug(f"Found:\t{loc}\nFor:\t{s['City']}, {s['Country']}\n")
|
2021-07-01 18:09:04 +00:00
|
|
|
return f"POINT({loc.longitude} {loc.latitude})"
|
2020-10-12 10:07:49 +00:00
|
|
|
else:
|
2021-07-01 18:09:04 +00:00
|
|
|
return None
|
|
|
|
|
|
|
|
missing = df.index[df.geom.isna()]
|
2023-03-06 08:27:45 +00:00
|
|
|
df.loc[missing, "coordinates"] = df.loc[missing].apply(locate_missing, axis=1)
|
2021-07-01 18:09:04 +00:00
|
|
|
|
|
|
|
# report stats
|
|
|
|
num_still_missing = df.coordinates.isna().sum()
|
|
|
|
num_found = len(missing) - num_still_missing
|
|
|
|
share_missing = len(missing) / len(df) * 100
|
|
|
|
share_still_missing = num_still_missing / len(df) * 100
|
2023-03-06 08:27:45 +00:00
|
|
|
logger.warning(
|
|
|
|
f"Found {num_found} missing locations. \nShare of missing locations reduced from {share_missing:.2f}% to {share_still_missing:.2f}%."
|
|
|
|
)
|
2021-07-01 18:09:04 +00:00
|
|
|
|
|
|
|
return df
|
|
|
|
|
|
|
|
|
|
|
|
def prepare_hotmaps_database(regions):
|
|
|
|
"""
|
|
|
|
Load hotmaps database of industrial sites and map onto bus regions.
|
|
|
|
"""
|
|
|
|
df = pd.read_csv(snakemake.input.hotmaps_industrial_database, sep=";", index_col=0)
|
|
|
|
|
2023-03-06 08:27:45 +00:00
|
|
|
df[["srid", "coordinates"]] = df.geom.str.split(";", expand=True)
|
2021-07-01 18:09:04 +00:00
|
|
|
|
2023-06-15 16:52:25 +00:00
|
|
|
if snakemake.params.hotmaps_locate_missing:
|
2021-07-01 18:09:04 +00:00
|
|
|
df = locate_missing_industrial_sites(df)
|
|
|
|
|
|
|
|
# remove those sites without valid locations
|
|
|
|
df.drop(df.index[df.coordinates.isna()], inplace=True)
|
|
|
|
|
2023-03-06 08:27:45 +00:00
|
|
|
df["coordinates"] = gpd.GeoSeries.from_wkt(df["coordinates"])
|
2021-07-01 18:09:04 +00:00
|
|
|
|
2023-03-06 08:27:45 +00:00
|
|
|
gdf = gpd.GeoDataFrame(df, geometry="coordinates", crs="EPSG:4326")
|
2021-07-01 18:09:04 +00:00
|
|
|
|
2024-02-09 12:59:15 +00:00
|
|
|
gdf = gpd.sjoin(gdf, regions, how="inner", predicate="within")
|
2021-07-01 18:09:04 +00:00
|
|
|
|
|
|
|
gdf.rename(columns={"index_right": "bus"}, inplace=True)
|
|
|
|
gdf["country"] = gdf.bus.str[:2]
|
2023-08-30 10:05:16 +00:00
|
|
|
|
2023-08-22 12:22:25 +00:00
|
|
|
# the .sjoin can lead to duplicates if a geom is in two overlapping regions
|
2023-08-21 14:16:53 +00:00
|
|
|
if gdf.index.duplicated().any():
|
|
|
|
# get all duplicated entries
|
|
|
|
duplicated_i = gdf.index[gdf.index.duplicated()]
|
|
|
|
# convert from raw data country name to iso-2-code
|
2024-01-19 11:16:07 +00:00
|
|
|
code = cc.convert(gdf.loc[duplicated_i, "Country"], to="iso2") # noqa: F841
|
2023-08-22 12:22:25 +00:00
|
|
|
# screen out malformed country allocation
|
|
|
|
gdf_filtered = gdf.loc[duplicated_i].query("country == @code")
|
2023-08-21 14:16:53 +00:00
|
|
|
# concat not duplicated and filtered gdf
|
2023-08-22 12:22:25 +00:00
|
|
|
gdf = pd.concat([gdf.drop(duplicated_i), gdf_filtered])
|
2023-08-21 14:16:53 +00:00
|
|
|
|
2021-07-01 18:09:04 +00:00
|
|
|
return gdf
|
|
|
|
|
|
|
|
|
2023-03-09 10:04:41 +00:00
|
|
|
def build_nodal_distribution_key(hotmaps, regions, countries):
|
2023-03-06 08:27:45 +00:00
|
|
|
"""
|
|
|
|
Build nodal distribution keys for each sector.
|
|
|
|
"""
|
2021-07-01 18:09:04 +00:00
|
|
|
sectors = hotmaps.Subsector.unique()
|
|
|
|
|
|
|
|
keys = pd.DataFrame(index=regions.index, columns=sectors, dtype=float)
|
|
|
|
|
|
|
|
pop = pd.read_csv(snakemake.input.clustered_pop_layout, index_col=0)
|
2023-03-06 08:27:45 +00:00
|
|
|
pop["country"] = pop.index.str[:2]
|
|
|
|
ct_total = pop.total.groupby(pop["country"]).sum()
|
|
|
|
keys["population"] = pop.total / pop.country.map(ct_total)
|
2021-07-01 18:09:04 +00:00
|
|
|
|
|
|
|
for sector, country in product(sectors, countries):
|
|
|
|
regions_ct = regions.index[regions.index.str.contains(country)]
|
|
|
|
|
|
|
|
facilities = hotmaps.query("country == @country and Subsector == @sector")
|
|
|
|
|
|
|
|
if not facilities.empty:
|
2023-08-14 10:40:04 +00:00
|
|
|
emissions = facilities["Emissions_ETS_2014"].fillna(
|
2024-01-04 18:17:49 +00:00
|
|
|
hotmaps["Emissions_EPRTR_2014"].dropna()
|
2023-08-14 10:40:04 +00:00
|
|
|
)
|
2021-07-01 18:09:04 +00:00
|
|
|
if emissions.sum() == 0:
|
|
|
|
key = pd.Series(1 / len(facilities), facilities.index)
|
2020-10-12 10:07:49 +00:00
|
|
|
else:
|
2023-03-06 08:27:45 +00:00
|
|
|
# BEWARE: this is a strong assumption
|
2021-07-01 18:09:04 +00:00
|
|
|
emissions = emissions.fillna(emissions.mean())
|
|
|
|
key = emissions / emissions.sum()
|
2023-03-06 08:27:45 +00:00
|
|
|
key = key.groupby(facilities.bus).sum().reindex(regions_ct, fill_value=0.0)
|
2021-07-01 18:09:04 +00:00
|
|
|
else:
|
2023-03-06 08:27:45 +00:00
|
|
|
key = keys.loc[regions_ct, "population"]
|
2020-10-12 10:07:49 +00:00
|
|
|
|
2021-07-01 18:09:04 +00:00
|
|
|
keys.loc[regions_ct, sector] = key
|
2020-10-12 10:07:49 +00:00
|
|
|
|
2021-07-01 18:09:04 +00:00
|
|
|
return keys
|
2020-10-12 10:07:49 +00:00
|
|
|
|
2023-08-30 10:05:16 +00:00
|
|
|
|
2020-10-12 10:07:49 +00:00
|
|
|
if __name__ == "__main__":
|
2023-03-06 08:27:45 +00:00
|
|
|
if "snakemake" not in globals():
|
2023-03-06 18:09:45 +00:00
|
|
|
from _helpers import mock_snakemake
|
2023-03-06 08:27:45 +00:00
|
|
|
|
2021-07-01 18:09:04 +00:00
|
|
|
snakemake = mock_snakemake(
|
2023-03-06 08:27:45 +00:00
|
|
|
"build_industrial_distribution_key",
|
|
|
|
simpl="",
|
2023-08-22 08:21:42 +00:00
|
|
|
clusters=128,
|
2021-07-01 18:09:04 +00:00
|
|
|
)
|
2024-02-12 10:53:20 +00:00
|
|
|
configure_logging(snakemake)
|
|
|
|
set_scenario_config(snakemake)
|
2023-02-23 09:30:32 +00:00
|
|
|
|
2023-06-15 16:52:25 +00:00
|
|
|
countries = snakemake.params.countries
|
2023-03-09 10:04:41 +00:00
|
|
|
|
2023-03-06 08:27:45 +00:00
|
|
|
regions = gpd.read_file(snakemake.input.regions_onshore).set_index("name")
|
2020-10-12 10:07:49 +00:00
|
|
|
|
2021-07-01 18:09:04 +00:00
|
|
|
hotmaps = prepare_hotmaps_database(regions)
|
2020-10-12 10:07:49 +00:00
|
|
|
|
2023-03-09 10:04:41 +00:00
|
|
|
keys = build_nodal_distribution_key(hotmaps, regions, countries)
|
2020-10-12 10:07:49 +00:00
|
|
|
|
2021-07-01 18:09:04 +00:00
|
|
|
keys.to_csv(snakemake.output.industrial_distribution_key)
|