Skip to content

Commit

Permalink
Merge pull request #963 from openego/features/#962-final-buildings-ma…
Browse files Browse the repository at this point in the history
…pping

Add mapping table for all used buildings
  • Loading branch information
nailend committed Nov 15, 2022
2 parents 0ff6664 + 6884078 commit da218df
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,8 @@ Added
`#919 <https://github.com/openego/eGon-data/issues/919>`_
* Add distribution of CHP plants for eGon100RE
`#851 <https://github.com/openego/eGon-data/issues/851>`_
* Add mapping table for all used buildings
`#962 <https://github.com/openego/eGon-data/issues/962>`_
* Add charging infrastructure for e-mobility
`#937 <https://github.com/openego/eGon-data/issues/937>`_
* Add zipfile check
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@
Mapping of demand time series and buildings including cell_id, building
area and peak load. This table is already created within
:func:`hh_buildings.get_building_peak_loads()`
* `boundaries.egon_map_zensus_mvgd_buildings`:
A final mapping table including all buildings used for residential and
cts, heat and electricity timeseries. Including census cells, mvgd bus_id,
building type (osm or synthetic)
**The following datasets from the database are mainly used for creation:**
Expand Down Expand Up @@ -184,6 +188,9 @@
BuildingElectricityPeakLoads,
OsmBuildingsSynthetic,
)
from egon.data.datasets.electricity_demand_timeseries.mapping import (
map_all_used_buildings,
)
from egon.data.datasets.electricity_demand_timeseries.tools import (
random_ints_until_sum,
random_point_in_square,
Expand Down Expand Up @@ -250,12 +257,13 @@ class CtsDemandBuildings(Dataset):
def __init__(self, dependencies):
super().__init__(
name="CtsDemandBuildings",
version="0.0.2",
version="0.0.3",
dependencies=dependencies,
tasks=(
cts_buildings,
{cts_electricity, cts_heat},
{get_cts_electricity_peak_load, get_cts_heat_peak_load},
map_all_used_buildings,
assign_voltage_level_to_buildings,
),
)
Expand Down
92 changes: 92 additions & 0 deletions src/egon/data/datasets/electricity_demand_timeseries/mapping.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
from sqlalchemy import Column, Integer, String
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.types import Boolean

from egon.data import db

Base = declarative_base()


class EgonMapZensusMvgdBuildings(Base):
__tablename__ = "egon_map_zensus_mvgd_buildings"
__table_args__ = {"schema": "boundaries"}

building_id = Column(Integer, primary_key=True)
sector = Column(String, primary_key=True)
zensus_population_id = Column(Integer, index=True)
bus_id = Column(Integer, index=True)
osm = Column(Boolean, index=True)
electricity = Column(Boolean, index=True)
heat = Column(Boolean, index=True)


def map_all_used_buildings():
"""This function maps all used buildings from OSM and synthetic ones."""

EgonMapZensusMvgdBuildings.__table__.drop(
bind=db.engine(), checkfirst=True
)
EgonMapZensusMvgdBuildings.__table__.create(bind=db.engine())

db.execute_sql(
sql_string=f"""
INSERT INTO {EgonMapZensusMvgdBuildings.__table_args__["schema"]}.
{EgonMapZensusMvgdBuildings.__tablename__}
SELECT
bld.id as building_id,
peak.sector,
zensus.id as zensus_population_id,
mvgd.bus_id::integer
FROM (
SELECT "id"::integer, geom_point
FROM openstreetmap.osm_buildings_synthetic
UNION
SELECT "id"::integer, geom_point
FROM openstreetmap.osm_buildings_filtered
) AS bld,
demand.egon_building_electricity_peak_loads AS peak,
society.destatis_zensus_population_per_ha
AS zensus,
boundaries.egon_map_zensus_grid_districts AS mvgd
WHERE bld.id = peak.building_id
-- Buildings do not change in the scenarios
AND peak.scenario = 'eGon2035'
AND ST_Within(bld.geom_point, zensus.geom)
AND mvgd.zensus_population_id = zensus.id;
UPDATE boundaries.egon_map_zensus_mvgd_buildings
SET "osm" = TRUE;
UPDATE boundaries.egon_map_zensus_mvgd_buildings as bld
SET "osm" = FALSE
FROM (
SELECT "id"::integer
FROM openstreetmap.osm_buildings_synthetic
) as synth
WHERE bld.building_id = synth.id;
UPDATE boundaries.egon_map_zensus_mvgd_buildings
SET "electricity" = TRUE;
UPDATE boundaries.egon_map_zensus_mvgd_buildings
SET "heat" = FALSE;
-- Only residentials
UPDATE boundaries.egon_map_zensus_mvgd_buildings as bld
SET "heat" = TRUE
FROM (
SELECT distinct(building_id)
FROM demand.egon_heat_timeseries_selected_profiles
) as heat
WHERE bld.building_id = heat.building_id
AND bld.sector = 'residential';
-- All electricity cts also are heat cts also
UPDATE boundaries.egon_map_zensus_mvgd_buildings as bld
SET "heat" = TRUE
WHERE bld.sector = 'cts' AND electricity = TRUE;
"""
)

0 comments on commit da218df

Please sign in to comment.