-
Notifications
You must be signed in to change notification settings - Fork 1
/
rendering.py
287 lines (259 loc) · 9.32 KB
/
rendering.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
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
import altair
import folium
import seaborn
from branca.element import MacroElement, Template
from uuid import uuid4
def draw_stations(stations, color, shape="circle"):
marker_layer = folium.FeatureGroup(name=f"Stops {uuid4()}")
for row in stations.to_dict(orient="records"):
location = [row["stop_lat"], row["stop_lon"]]
tooltip = row["stop_name"]
if shape == "circle":
folium.CircleMarker(
location=location,
tooltip=tooltip,
radius=3,
fill=True,
fillColor=color,
color="#000000",
weight=1,
fillOpacity=1,
).add_to(marker_layer)
elif shape == "square":
icon = folium.plugins.BeautifyIcon(
icon_shape="rectangle-dot",
icon_size=[10, 10],
background_color=color,
border_width=1,
)
folium.Marker(
location,
tooltip=tooltip,
icon=icon,
).add_to(marker_layer)
else:
icon = folium.plugins.BeautifyIcon(
icon=shape,
inner_icon_style="font-size:10px",
icon_shape="marker",
icon_size=(20, 20),
icon_anchor=(10, 10),
background_color=color,
border_width=1,
)
folium.Marker(
location,
tooltip=tooltip,
icon=icon,
).add_to(marker_layer)
return marker_layer
def draw_routes(stop_data, color_name, COLOR_TYPE="colormap"):
path_layer = folium.FeatureGroup(name="Paths")
grouped = stop_data.sort_values(["trip_id", "stop_sequence"]).groupby("trip_id")
if COLOR_TYPE == "colormap":
colors = seaborn.color_palette(color_name, n_colors=grouped.ngroups).as_hex()
idx = 0
for name, group in grouped:
stop_coords = []
tooltip_content = f"<span style='display: none'>[{group.iloc[0]['route_id']}]</span>{group.iloc[0]['route_short_name']}"
for row_index, row in group.iterrows():
stop_coords.append(row[["stop_lat", "stop_lon"]].values)
tooltip_content += f"<br/>{row['stop_name']}"
color = colors[idx] if COLOR_TYPE == "colormap" else color_name
folium.PolyLine(stop_coords, tooltip=tooltip_content, color=color).add_to(
path_layer
)
idx += 1
return path_layer
def draw_route_detail(chart_data, time_data):
scale = altair.Scale(domain=[0.8, chart_data["stop_sequence"].max() + 0.2])
time_annotations = (
altair.Chart(time_data)
.mark_text(dy=15)
.encode(
altair.X(
"stop_sequence",
scale=scale,
),
altair.Y("route_short_name"),
altair.Text("next_stop"),
)
)
chart = altair.Chart(
chart_data, title="Reachable stations from selected route"
).encode(
altair.X(
"stop_sequence",
scale=scale,
axis=altair.Axis(grid=False, labels=False),
).title("Stops", color="black"),
altair.Y("route_short_name").title(""),
)
layered = altair.layer(
chart.mark_line(),
chart.mark_point(filled=True, opacity=1).encode(
shape=altair.Shape(
"shared",
scale=altair.Scale(domain=[True, False], range=["square", "circle"]),
# TODO move legend up?
legend=altair.Legend(
title="Station reachable by both",
titleColor="#000000",
labelColor="#000000",
),
),
color=altair.Color(
"shared",
scale=altair.Scale(domain=[True, False], range=["#fc8d59", "#91bfdb"]),
),
),
chart.mark_text(dy=-20).encode(altair.Text("stop_name")),
time_annotations,
).configure(
background="#ffffff",
axisLeft={"labelColor": "#000000"},
axisBottom={"titleColor": "#000000"},
point=altair.MarkConfig(size=200),
)
return layered
LEGEND_STYLE = """
<style type='text/css'>
.maplegend .legend-title {
text-align: left;
margin-bottom: 5px;
font-weight: bold;
font-size: 90%;
}
.maplegend .legend-scale p {
font-weight: bold;
font-size: 80%;
margin-bottom: 0px;
}
.maplegend .legend-scale ul {
margin: 0;
margin-bottom: 5px;
padding: 0;
//float: left;
list-style: none;
}
.maplegend .legend-scale ul li {
font-size: 80%;
list-style: none;
margin-left: 0;
line-height: 18px;
margin-bottom: 2px;
}
.maplegend ul.legend-stations li span {
display: block;
float: left;
height: 10px;
width: 10px;
margin-right: 5px;
margin-left: 0;
margin-top: 4px;
border: 1px solid #000;
}
.maplegend ul.legend-lines li span {
display: block;
float: left;
height: 16px;
width: 30px;
margin-right: 5px;
margin-left: 0;
margin-top: 1px;
border: 1px solid #000;
}
.maplegend .legend-source {
font-size: 80%;
color: #777;
clear: both;
}
.maplegend {
color: #000 !important;
}
</style>
"""
LEGEND_TOP = """
{% macro html(this, kwargs) %}
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<div class='maplegend'
style='position: absolute; z-index:9999; border:2px solid grey; background-color:rgba(255, 255, 255, 0.7); border-radius:6px; padding: 10px; font-size:14px; right: 20px; top: 20px;'
>
<div class='legend-title'>Legend</div>
<div class='legend-scale'>
"""
LEGEND_BOTTOM = f"""
</div>
</div>
</body>
</html>
{LEGEND_STYLE}
{{% endmacro %}}
"""
def generate_main_legend():
# referenced from https://nbviewer.org/gist/talbertc-usgs/18f8901fc98f109f2b71156cf3ac81cd
template = f"""
{LEGEND_TOP}
<p>Stations</p>
<ul class='legend-stations'>
<li><span style='background:#ffffbf; border-radius:50px;'></span>Reachable by A</li>
<li><span style='background:#91bfdb; border-radius:50px;'></span>Reachable by B</li>
<li><span style='background:#fc8d59;'></span>Reachable by both</li>
<li><span class="beautify-marker marker" style='height: 20px; width: 20px'><i class="fa fa-star"></i></span><div style='margin-left: 3rem; margin-top: 0.5rem'>Destination A and B</div></li>
</ul>
<p style='margin-top: 2rem'>Lines</p>
<ul class='legend-lines'>
<li><span style='background: #808080;'></span>Routes of A and B</li>
<!--
<li><span style='background: #808080;'></span>Not shared routes</li>
inferno in css https://bennettfeely.com/scales/
<li><span style='background: #eb8f70; background-image: linear-gradient(90deg, #000004, #160b39, #420a68, #6a176e, #932667, #ba3655, #dd513a, #f3761b, #fca50a, #f6d746, #f6d746);'></span>Shared routes</li>
-->
</ul>
{LEGEND_BOTTOM}
"""
macro = MacroElement()
macro._template = Template(template)
return macro
def generate_sub_a_legend():
template = f"""
{LEGEND_TOP}
<p>Stations</p>
<ul class='legend-stations'>
<li><span style='background:#ffffbf; border-radius:50px;'></span>Reachable by A</li>
<li><span class="beautify-marker marker" style='background:#ffffbf;height: 20px; width: 20px'><i class="fa fa-star"></i></span><div style='margin-left: 3rem; margin-top: 0.5rem'>Destination B</div></li>
</ul>
<p style='margin-top: 2rem'>Lines</p>
<ul class='legend-lines'>
<!-- plasma in css https://bennettfeely.com/scales/ -->
<li><span style='background: #a84a73; background-image: linear-gradient(90deg, #0d0887, #41049d, #6a00a8, #8f0da4, #b12a90, #cb4679, #e16462, #f1834c, #fca636, #fcce25, #fcce25);'></span>Shared routes</li>
</ul>
{LEGEND_BOTTOM}
"""
macro = MacroElement()
macro._template = Template(template)
return macro
def generate_sub_b_legend():
template = f"""
{LEGEND_TOP}
<p>Stations</p>
<ul class='legend-stations'>
<li><span style='background:#91bfdb; border-radius:50px;'></span>Reachable by B</li>
<li><span class="beautify-marker marker" style='background:#91bfdb;height: 20px; width: 20px'><i class="fa fa-star"></i></span><div style='margin-left: 3rem; margin-top: 0.5rem'>Destination A</div></li>
</ul>
<p style='margin-top: 2rem'>Lines</p>
<ul class='legend-lines'>
<!-- viridis in css https://bennettfeely.com/scales/ -->
<li><span style='background: #4a7d70; background-image: linear-gradient(90deg, #440154, #482475, #414487, #355f8d, #2a788e, #21908d, #22a884, #42be71, #7ad151, #bddf26, #bddf26);'></span>Shared routes</li>
</ul>
{LEGEND_BOTTOM}
"""
macro = MacroElement()
macro._template = Template(template)
return macro