-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_augment.py
More file actions
81 lines (61 loc) · 3.43 KB
/
test_augment.py
File metadata and controls
81 lines (61 loc) · 3.43 KB
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
from augment_graph import intersection_len_fun
from calculate_polygon_shade import calculate_polygon_shade
import osmnx as ox
import pandas as pd
import geopandas as gpd
import folium
import shapely
from pathlib import Path
def test(html_output: str = "asd.html", start_location = (40.748817, -73.985428), end_location = (40.743291339946865, -73.98000681558766)):
buildings_path = Path("data/buildings_dwnld.json")
if buildings_path.exists():
buildings = gpd.read_file(buildings_path)
else:
buildings: gpd.GeoDataFrame = ox.features_from_bbox(bbox=(start_location[0], end_location[0], start_location[1], end_location[1]), tags={"building": True})
buildings = buildings[['geometry', 'height']]
buildings['height'] = buildings['height'].astype(float)
buildings['height'] = buildings['height'].fillna(5.0)
buildings_path.write_text(buildings.to_json())
shadows_path = Path("data/shadows.json")
shadows: gpd.GeoDataFrame = calculate_polygon_shade(buildings, pd.Timestamp("2022-06-21 14:00:00"))
shadows_path.write_text(shadows.to_json())
shadows_mp: shapely.MultiPolygon = shapely.union_all(shadows['geometry'])
network_path = Path("data/network.graphml")
if network_path.exists():
G = ox.load_graphml(network_path, edge_dtypes={'sun_length': float})
else:
G = ox.graph_from_bbox(bbox=(start_location[0], end_location[0], start_location[1], end_location[1]), network_type='walk')
ox.save_graphml(G, network_path)
# Get the nearest nodes to the start and end points
orig_node = ox.distance.nearest_nodes(G, start_location[1], start_location[0])
dest_node = ox.distance.nearest_nodes(G, end_location[1], end_location[0])
# Calculate the shortest path
shortest_path = ox.shortest_path(G, orig_node, dest_node, weight=intersection_len_fun(G, shadows_mp))
normal_path = ox.shortest_path(G, orig_node, dest_node, weight='length')
print("Found shortest path!")
midpoint = ((start_location[0] + end_location[0]) / 2, (start_location[1] + end_location[1]) / 2)
m = folium.Map(location=midpoint, zoom_start=13, tiles="OpenStreetMap")
for geometry in buildings['geometry']:
if not hasattr(geometry, 'exterior'):
continue
if len(geometry.exterior.coords) <= 3:
continue
inverted_coords = tuple((y, x) for x, y in geometry.exterior.coords)
folium.Polygon(inverted_coords, fill_color="#bbb", weight=0, fill_opacity=1).add_to(m)
for polygon in list(shadows_mp.geoms):
try:
inverted_coords = tuple((y, x) for x, y in polygon.exterior.coords)
folium.Polygon(inverted_coords, fill_color='black', fill_opacity=0.5, weight=0).add_to(m)
except Exception as e:
print(e)
# Plot the route on the map
route_coords = [(G.nodes[node]['y'], G.nodes[node]['x']) for node in shortest_path]
folium.PolyLine(route_coords, color='blue', weight=5, opacity=0.7).add_to(m)
route_coords = [(G.nodes[node]['y'], G.nodes[node]['x']) for node in normal_path]
folium.PolyLine(route_coords, color='green', weight=5, opacity=0.7).add_to(m)
# Add start and end markers
folium.Marker(location=start_location, popup='Start', icon=folium.Icon(color='green')).add_to(m)
folium.Marker(location=end_location, popup='End', icon=folium.Icon(color='red')).add_to(m)
m.save(html_output)
if __name__ == '__main__':
test()