-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmap_utils.py
More file actions
200 lines (167 loc) · 6.43 KB
/
Copy pathmap_utils.py
File metadata and controls
200 lines (167 loc) · 6.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
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# ============================================================
# map_utils.py
# FOLIUM MAP BUILDERS FOR THE STREAMLIT APP
# ============================================================
import geopandas as gpd
import folium
from folium.plugins import Draw, Fullscreen, MeasureControl
from analysis import ANALYSIS_CRS, WEB_CRS
MAP_LEGEND_HTML = """
<div style="
position: fixed;
bottom: 25px;
left: 25px;
width: 245px;
z-index: 9999;
background: white;
border: 2px solid #555;
border-radius: 6px;
padding: 12px;
font-size: 13px;
box-shadow: 2px 2px 8px rgba(0,0,0,.25);
">
<b>MAP LEGEND</b><hr>
<span style="color:#2C7BE5;font-size:20px">━━</span> Existing Routes<br>
<span style="color:#00A65A;font-size:20px">━━</span> Proposed / Selected Route<br>
<span style="color:#E74C3C;font-size:20px">━━</span> Overlap Segments<br>
<span style="color:#F39C12;font-size:20px">━━</span> Main Corridors<br>
<span style="color:#8E44AD;font-size:18px">●</span> Bus Stops
</div>
"""
def build_results_map(
routes: gpd.GeoDataFrame,
stops: gpd.GeoDataFrame,
corridors: gpd.GeoDataFrame,
proposed_gdf: gpd.GeoDataFrame,
result: dict,
tolerance: float,
) -> folium.Map:
"""Recreates the original build_map() function as a standalone helper."""
routes_web = routes.to_crs(WEB_CRS)
corridors_web = corridors.to_crs(WEB_CRS)
proposed_web = proposed_gdf.to_crs(WEB_CRS)
center = proposed_web.geometry.iloc[0].centroid
m = folium.Map(location=[center.y, center.x], zoom_start=11, tiles=None)
folium.TileLayer("CartoDB positron", name="Light Basemap").add_to(m)
folium.TileLayer("OpenStreetMap", name="OpenStreetMap").add_to(m)
folium.GeoJson(
routes_web.to_json(),
name="Existing Routes",
style_function=lambda feature: {"color": "#2C7BE5", "weight": 2, "opacity": 0.45},
tooltip=folium.GeoJsonTooltip(
fields=[f for f in ["Route_ID", "Operator"] if f in routes_web.columns],
aliases=[a for f, a in [("Route_ID", "Route ID"), ("Operator", "Operator")] if f in routes_web.columns],
sticky=True,
),
).add_to(m)
folium.GeoJson(
corridors_web.to_json(),
name="Main Corridors",
style_function=lambda feature: {"color": "#F39C12", "weight": 4, "opacity": 0.65},
tooltip=folium.GeoJsonTooltip(
fields=[f for f in ["Name"] if f in corridors_web.columns],
aliases=["Corridor"],
),
).add_to(m)
for item in result["overlap_geometries"]:
geom = item["geometry"]
if geom is None or geom.is_empty:
continue
overlap_gdf = gpd.GeoDataFrame(
{"Route_ID": [item["Route_ID"]]}, geometry=[geom], crs=ANALYSIS_CRS
)
overlap_web = overlap_gdf.to_crs(WEB_CRS)
folium.GeoJson(
overlap_web.to_json(),
name="Overlap - " + item["Route_ID"],
style_function=lambda feature: {"color": "#E74C3C", "weight": 7, "opacity": 0.90},
tooltip="Overlapping route: " + item["Route_ID"],
).add_to(m)
folium.GeoJson(
proposed_web.to_json(),
name="PROPOSED / SELECTED ROUTE",
style_function=lambda feature: {"color": "#00A65A", "weight": 7, "opacity": 1},
tooltip=folium.GeoJsonTooltip(
fields=["Route_ID"] if "Route_ID" in proposed_web.columns else [],
aliases=["Route"],
),
).add_to(m)
proposed_buffer = proposed_gdf.copy()
proposed_buffer["geometry"] = proposed_buffer.geometry.buffer(tolerance)
proposed_buffer = proposed_buffer.to_crs(WEB_CRS)
folium.GeoJson(
proposed_buffer.to_json(),
name=f"Analysis Buffer ({tolerance} m)",
style_function=lambda feature: {
"color": "#00A65A",
"weight": 1,
"fillColor": "#00A65A",
"fillOpacity": 0.08,
},
).add_to(m)
affected = result["affected_stops"].to_crs(WEB_CRS)
stop_group = folium.FeatureGroup(name="Affected Bus Stops")
for _, stop in affected.iterrows():
stop_name = str(stop.get("Name", "Bus Stop"))
route_id = str(stop.get("Route_ID", ""))
folium.CircleMarker(
location=[stop.geometry.y, stop.geometry.x],
radius=5,
color="#8E44AD",
fill=True,
fillColor="#8E44AD",
fillOpacity=0.85,
tooltip=f"<b>{stop_name}</b><br>Route: {route_id}",
).add_to(stop_group)
stop_group.add_to(m)
Fullscreen(position="topright").add_to(m)
MeasureControl(
position="topright",
primary_length_unit="kilometers",
secondary_length_unit="meters",
).add_to(m)
m.get_root().html.add_child(folium.Element(MAP_LEGEND_HTML))
folium.LayerControl(collapsed=False).add_to(m)
return m
def build_input_map(
routes: gpd.GeoDataFrame,
corridors: gpd.GeoDataFrame,
enable_draw: bool = True,
) -> folium.Map:
"""
Draw-mode input map (replaces the ipyleaflet DrawControl map).
Render this with streamlit_folium.st_folium() and read the
drawn line back from its return value's "last_active_drawing".
"""
routes_web = routes.to_crs(WEB_CRS)
corridors_web = corridors.to_crs(WEB_CRS)
center = routes_web.geometry.unary_union.centroid
m = folium.Map(location=[center.y, center.x], zoom_start=11, tiles=None)
folium.TileLayer("OpenStreetMap", opacity=0.4, name="OpenStreetMap").add_to(m)
folium.GeoJson(
routes_web.to_json(),
name="Existing Routes",
style_function=lambda feature: {"color": "#2C7BE5", "weight": 2, "opacity": 0.35},
).add_to(m)
folium.GeoJson(
corridors_web.to_json(),
name="Main Corridors",
style_function=lambda feature: {"color": "#F39C12", "weight": 4, "opacity": 0.5},
).add_to(m)
if enable_draw:
Draw(
export=False,
position="topleft",
draw_options={
"polyline": {"shapeOptions": {"color": "#00A65A", "weight": 5, "opacity": 1.0}},
"polygon": False,
"circle": False,
"circlemarker": False,
"marker": False,
"rectangle": False,
},
edit_options={"edit": True, "remove": True},
).add_to(m)
Fullscreen(position="topright").add_to(m)
folium.LayerControl(collapsed=False).add_to(m)
return m