-
Notifications
You must be signed in to change notification settings - Fork 8
/
build_shapes.py
117 lines (88 loc) · 3.33 KB
/
build_shapes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# -*- coding: utf-8 -*-
import logging
import os
import geopandas as gpd
import pandas as pd
from _helpers_dist import configure_logging, save_to_geojson, sets_path_to_root
from shapely.geometry import Polygon
_logger = logging.getLogger(__name__)
_logger.setLevel(logging.INFO)
def create_microgrid_shapes(microgrids_list, output_path):
"""
Creates rectangular shapes for each microgrid in the list of microgrids in the config.yaml file
and saves them as a GeoJSON file.
"""
microgrids_list = microgrids_list
microgrids_list_df = pd.DataFrame(microgrids_list)
microgrid_shapes = []
microgrid_names = []
for col in range(len(microgrids_list_df.columns)):
values = microgrids_list_df.iloc[:, col]
# Definition of the vertices of the rectangle
Top_left = (values[0], values[3])
Top_right = (values[1], values[3])
Bottom_right = (values[1], values[2])
Bottom_left = (values[0], values[2])
microgrid_shape = Polygon(
[Top_left, Top_right, Bottom_right, Bottom_left, Top_left]
)
microgrid_name = f"microgrid_{col+1}"
microgrid_shapes.append(microgrid_shape)
microgrid_names.append(microgrid_name)
microgrid_gdf = gpd.GeoDataFrame(
{"name": microgrid_names, "geometry": microgrid_shapes}
)
save_to_geojson(microgrid_gdf, output_path)
def create_bus_regions(microgrids_list, output_path):
"""
Creates bus regions for each microgrid in the list of microgrids and saves them as a GeoJSON file.
"""
microgrids_list = microgrids_list
microgrids_list_df = pd.DataFrame(microgrids_list)
microgrid_shapes = []
microgrid_names = []
microgrid_x = []
microgrid_y = []
for col in range(len(microgrids_list_df.columns)):
values = microgrids_list_df.iloc[:, col]
# Definition of the vertices of the rectangle
Top_left = (values[0], values[3])
Top_right = (values[1], values[3])
Bottom_right = (values[1], values[2])
Bottom_left = (values[0], values[2])
microgrid_shape = Polygon(
[Top_left, Top_right, Bottom_right, Bottom_left, Top_left]
)
# The bus is the central bus of each microgrid
microgrid_name = f"bus_9"
microgrid_shapes.append(microgrid_shape)
microgrid_names.append(microgrid_name)
# Centre of the rectangle of the microgrid
x = (values[0] + values[1]) / 2
y = (values[2] + values[3]) / 2
microgrid_x.append(x)
microgrid_y.append(y)
microgrid_gdf = gpd.GeoDataFrame(
{
"name": microgrid_names,
"x": microgrid_x,
"y": microgrid_y,
"geometry": microgrid_shapes,
}
)
save_to_geojson(microgrid_gdf, output_path)
if __name__ == "__main__":
if "snakemake" not in globals():
from _helpers_dist import mock_snakemake
os.chdir(os.path.dirname(os.path.abspath(__file__)))
snakemake = mock_snakemake("build_shapes")
sets_path_to_root("pypsa-distribution")
configure_logging(snakemake)
create_microgrid_shapes(
snakemake.config["microgrids_list"],
snakemake.output["microgrid_shapes"],
)
create_bus_regions(
snakemake.config["microgrids_list"],
snakemake.output["microgrid_bus_shapes"],
)