db.powerplants.get_energymap_pps()   A
last analyzed

Complexity

Conditions 4

Size

Total Lines 30
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 20
nop 4
dl 0
loc 30
rs 9.4
c 0
b 0
f 0
1
# -*- coding: utf-8 -*-
2
"""
3
Created on Mon Aug 17 11:08:15 2015
4
5
This is a collection of helper functions which work on there own an can be
6
used by various classes. If there are too many helper-functions, they will
7
be sorted in different modules.
8
9
All special import should be in try/except loops to avoid import errors.
10
"""
11
12
import pandas as pd
13
14
de_en = {
15
    'Braunkohle': 'lignite',
16
    'Steinkohle': 'hard_coal',
17
    'Erdgas': 'natural_gas',
18
    'Öl': 'oil',
19
    'Solarstrom': 'solar_power',
20
    'Windkraft': 'wind_power',
21
    'Biomasse': 'biomass',
22
    'Wasserkraft': 'hydro_power',
23
    'Gas': 'methan',
24
    'Mineralölprodukte': 'mineral_oil',
25
    'Abfall': 'waste',
26
    'Sonstige Energieträger\n(nicht erneuerbar) ': 'waste',
27
    'Pumpspeicher': 'pumped_storage',
28
}
29
30
31
def translator(x):
32
    return de_en[x]
33
34
35
def get_all_power_plants(conn, geometry):
36
    return pd.concat(
37
        [get_bnetza_pps(conn, geometry), get_energymap_pps(conn, geometry)],
38
        ignore_index=True,
39
    )
40
41
42
def get_energymap_pps(conn, geometry1, geometry2=None, tsum=True):
43
    # TODO@Günni
44
    sql = """
45
        SELECT anlagentyp, anuntertyp, p_nenn_kwp
46
        FROM oemof_test.energy_map as ee
47
        WHERE st_contains(ST_GeomFromText('{wkt}',4326), ee.geom)
48
        """.format(
49
        wkt=geometry1.wkt
50
    )
51
52
    if geometry2 is None:
53
        sql += ';'
54
    else:
55
        sql += '''AND st_contains(ST_GeomFromText('{wkt}',4326),
56
            ee.geom);'''.format(
57
            wkt=geometry2.wkt
58
        )
59
    df_full = pd.DataFrame(
60
        conn.execute(sql).fetchall(), columns=['type', 'subtype', 'cap']
61
    )
62
    df = pd.DataFrame(columns=['type', 'cap'])
63
    if tsum:
64
        typelist = df_full.type.unique()
65
        for i in range(len(typelist)):
66
            cap_sum = df_full[df_full.type == typelist[i]].cap.sum()
67
            df.loc[i] = [typelist[i], cap_sum]
68
    else:
69
        df = df_full
70
    df['type'] = df['type'].apply(translator)
71
    return df
72
73
74
def get_bnetza_pps(conn, geometry):
75
    # TODO@Günni
76
    sql = """
77
        SELECT auswertung, ersatzbrennstoff, el_nennleistung
78
        FROM oemof_test.geo_power_plant_bnetza_2014 as pp
79
        WHERE st_contains(
80
        ST_GeomFromText('{wkt}',4326), ST_Transform(pp.geom, 4326))
81
        """.format(
82
        wkt=geometry.wkt
83
    )
84
    df = pd.DataFrame(
85
        conn.execute(sql).fetchall(), columns=['type', 'subtype', 'cap']
86
    )
87
    df['type'] = df['type'].apply(translator)
88
    return df
89