%matplotlib inline
import os
import matplotlib.pyplot as plt
from rivus.gridder.create_grid import create_square_grid
from rivus.gridder.extend_grid import extend_edge_data
from rivus.gridder.extend_grid import vert_init_commodities
Origo for grid creation.
You can copy LatLon into this list from osm, google.maps...
You do not need to input it, if not provided, it default to a point around Munich.
lat, lon = [48.13512, 11.58198]
base_directory = os.path.join('data', 'chessboard')
data_spreadsheet = os.path.join(base_directory, 'data.xlsx')
Get the raw gemetries
vertex, edge = create_square_grid(origo_latlon=(lat, lon), num_edge_x=4, dx=300, dy=150, noise_prop=0.1)
vertex.head(4)
geometry | Vertex | |
---|---|---|
0 | POINT (11.58198 48.13511999974561) | 0 |
1 | POINT (11.58601061427288 48.13511992931774) | 1 |
2 | POINT (11.59004122854023 48.13511985813629) | 2 |
3 | POINT (11.59407184280205 48.13511978757992) | 3 |
edge.head(4)
geometry | Edge | Vertex1 | Vertex2 | |
---|---|---|---|---|
0 | LINESTRING (11.58198 48.13511999974561, 11.586... | 0 | 0 | 1 |
1 | LINESTRING (11.58601061427288 48.1351199293177... | 1 | 1 | 2 |
2 | LINESTRING (11.59004122854023 48.1351198581362... | 2 | 2 | 3 |
3 | LINESTRING (11.59407184280205 48.1351197875799... | 3 | 3 | 4 |
sources = [('Elec', 0, 100000), ('Gas', 0, 50000)] # Commodity, VertexID, MaxCapacity
vert_init_commodities(vertex, ('Elec', 'Gas'), sources)
vertex.head(4)
geometry | Vertex | Elec | Gas | |
---|---|---|---|---|
0 | POINT (11.58198 48.13511999974561) | 0 | 100000 | 50000 |
1 | POINT (11.58601061427288 48.13511992931774) | 1 | 0 | 0 |
2 | POINT (11.59004122854023 48.13511985813629) | 2 | 0 | 0 |
3 | POINT (11.59407184280205 48.13511978757992) | 3 | 0 | 0 |
building_types = ['residential', 'industrial']
inits = [1000, 2000]
extend_edge_data(edge, sorts=building_types, inits=inits)
edge.head(4)
geometry | Edge | Vertex1 | Vertex2 | residential | industrial | |
---|---|---|---|---|---|---|
0 | LINESTRING (11.58198 48.13511999974561, 11.586... | 0 | 0 | 1 | 1000 | 2000 |
1 | LINESTRING (11.58601061427288 48.1351199293177... | 1 | 1 | 2 | 1000 | 2000 |
2 | LINESTRING (11.59004122854023 48.1351198581362... | 2 | 2 | 3 | 1000 | 2000 |
3 | LINESTRING (11.59407184280205 48.1351197875799... | 3 | 3 | 4 | 1000 | 2000 |
# Helper
def _calc_xytext_offset(line):
dx, dy = [abs(cc[0] - cc[1]) for cc in list(zip(*line.coords[:]))]
if dx == 0 or (dy / dx) > 1:
shx, shy = 30, 0
else:
shx, shy = 0, -30
return (shx, shy)
annotate_vertex_ids = True
annotate_edge_ids = True
fig, ax = plt.subplots(figsize=(12,12))
ax.set_aspect('equal')
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.axis('off')
vertex.plot(ax=ax, marker='o', color='red', markersize=5)
if annotate_vertex_ids:
for idx, pnt in enumerate(vertex.geometry):
plt.annotate(
"{}".format(idx),
xy=(pnt.coords[0][0], pnt.coords[0][1]), xytext=(-20, 20),
textcoords='offset points', ha='center', va='center',
bbox=dict(boxstyle='round,pad=0.5', fc='red', alpha=0.3),
arrowprops=dict(arrowstyle = '->', connectionstyle='arc3,rad=0'))
edge.plot(ax=ax, color='blue')
if annotate_edge_ids:
for idx, lin in enumerate(edge.geometry):
textxy = lin.centroid
plt.annotate(
"{}".format(idx),
xy=(textxy.x, textxy.y), xytext=_calc_xytext_offset(lin),
textcoords='offset points', ha='right', va='bottom',
bbox=dict(boxstyle='round,pad=0.5', fc='blue', alpha=0.3),
arrowprops=dict(arrowstyle = '->', connectionstyle='arc3,rad=0'))
plt.tight_layout()
plt.show()