Passed
Pull Request — dev (#963)
by
unknown
01:31
created

map_all_used_buildings()   A

Complexity

Conditions 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 10
rs 10
c 0
b 0
f 0
cc 1
nop 0
1
from sqlalchemy import Column, Integer, String
2
from sqlalchemy.ext.declarative import declarative_base
3
from sqlalchemy.types import Boolean
4
5
from egon.data import db
6
7
Base = declarative_base()
8
9
10
class EgonMapZensusMvgdBuildings(Base):
11
    __tablename__ = "egon_map_zensus_mvgd_buildings"
12
    __table_args__ = {"schema": "boundaries"}
13
14
    building_id = Column(Integer, primary_key=True)
15
    sector = Column(String, primary_key=True)
16
    zensus_population_id = Column(Integer, index=True)
17
    bus_id = Column(Integer, index=True)
18
    osm = Column(Boolean, index=True)
19
    electricity = Column(Boolean, index=True, default="True")
20
    heat = Column(Boolean, index=True)
21
22
23
def map_all_used_buildings():
24
    """This function maps all used buildings from OSM and synthetic ones."""
25
26
    EgonMapZensusMvgdBuildings.__table__.drop(
27
        bind=db.engine(), checkfirst=True
28
    )
29
    EgonMapZensusMvgdBuildings.__table__.create(bind=db.engine())
30
31
    db.execute_sql(
32
        sql_string=f"""
33
        INSERT INTO {EgonMapZensusMvgdBuildings.__table_args__["schema"]}.
34
        {EgonMapZensusMvgdBuildings.__tablename__}
35
            SELECT
36
                bld.id as building_id,
37
                peak.sector,
38
                zensus.id as zensus_population_id,
39
                mvgd.bus_id::integer
40
            FROM (
41
                SELECT "id"::integer, geom_point
42
                FROM openstreetmap.osm_buildings_synthetic
43
                UNION
44
                SELECT "id"::integer, geom_point
45
                FROM openstreetmap.osm_buildings_filtered
46
            ) AS bld,
47
                demand.egon_building_electricity_peak_loads AS peak,
48
                society.destatis_zensus_population_per_ha
49
                AS zensus,
50
                boundaries.egon_map_zensus_grid_districts AS mvgd
51
            WHERE bld.id = peak.building_id
52
-- Buildings do not change in the scenarios
53
                AND peak.scenario = 'eGon2035'
54
                AND ST_Within(bld.geom_point, zensus.geom)
55
                AND mvgd.zensus_population_id = zensus.id;
56
57
        UPDATE boundaries.egon_map_zensus_mvgd_buildings
58
        SET     "osm" = TRUE;
59
60
        UPDATE boundaries.egon_map_zensus_mvgd_buildings as bld
61
        SET     "osm" = FALSE
62
        FROM (
63
            SELECT "id"::integer
64
            FROM openstreetmap.osm_buildings_synthetic
65
            ) as synth
66
        WHERE bld.building_id = synth.id;
67
68
        UPDATE boundaries.egon_map_zensus_mvgd_buildings
69
        SET     "heat" = FALSE;
70
71
-- Only residentials
72
        UPDATE boundaries.egon_map_zensus_mvgd_buildings as bld
73
        SET     "heat" = TRUE
74
        FROM (
75
            SELECT distinct(building_id)
76
            FROM demand.egon_heat_timeseries_selected_profiles
77
            ) as heat
78
        WHERE bld.building_id = heat.building_id
79
         AND bld.sector = 'residential';
80
81
-- All electricity cts also are heat cts also
82
        UPDATE boundaries.egon_map_zensus_mvgd_buildings as bld
83
        SET     "heat" = TRUE
84
        WHERE bld.sector = 'cts' AND electricity = TRUE;
85
86
87
88
        """
89
    )
90