-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
247 lines (213 loc) · 6.88 KB
/
main.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
import re
import folium
import pandas as pd
import streamlit as st
from st_pages import add_page_title, show_pages_from_config
from streamlit_folium import st_folium
from processing import (
find_shared,
get_routes,
get_stops,
load_feed,
parse_stations,
route_details,
)
from rendering import (
draw_route_detail,
draw_routes,
draw_stations,
generate_main_legend,
generate_sub_a_legend,
generate_sub_b_legend,
)
MAP_CENTER = (46.848, 8.1336)
COLORS = ["#ffffbf", "#91bfdb"]
COLOR_SHARED = "#fc8d59"
# Streamlit app
#####
add_page_title(page_title="Home", layout="wide")
show_pages_from_config()
feed = load_feed()
stations = parse_stations(feed)
## Filters
#####
filter_container = st.container()
filter_col1, filter_col2 = filter_container.columns(2)
# TODO restricted to two for now, have it variable to min two and higher max
selected_city_a = filter_col1.selectbox(
"Destination A",
stations["stop_id"],
None,
format_func=lambda id: stations.loc[stations["stop_id"] == id]["stop_name"].iloc[0],
)
selected_city_b = filter_col1.selectbox(
"Destination B",
stations["stop_id"],
None,
format_func=lambda id: stations.loc[stations["stop_id"] == id]["stop_name"].iloc[0],
)
routes_a = get_routes(feed, selected_city_a)
routes_b = get_routes(feed, selected_city_b)
all_routes = (
pd.concat([routes_a, routes_b])
.drop_duplicates(subset="route_id")
.sort_values("route_short_name")
)
route_exclusion = filter_col2.multiselect(
"Exclude lines (optional)",
all_routes["route_id"],
format_func=lambda id: all_routes.loc[all_routes["route_id"] == id][
"route_short_name"
].iloc[0],
)
active_weekdays = filter_col2.multiselect(
"Active weekdays",
["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"],
["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"],
)
relevant_hours = filter_col2.slider("Relevant hours", 0, 23, (6, 22), 1)
if selected_city_a is not None and selected_city_a == selected_city_b:
st.error("Same cities selected")
stops_a = get_stops(
feed,
routes_a.loc[~routes_a["route_id"].isin(route_exclusion)]["route_id"],
active_weekdays,
relevant_hours,
)
stops_b = get_stops(
feed,
routes_b.loc[~routes_b["route_id"].isin(route_exclusion)]["route_id"],
active_weekdays,
relevant_hours,
)
shared_stops = find_shared(stops_a, stops_b)
st.markdown("""---""")
## Content
#####
map_container = st.container()
# with does not create a scope
with map_container:
map_main = folium.Map(tiles="cartodbpositron")
# TODO if there are 100% shared routes, highlight
COLOR_A = COLORS[0]
COLOR_B = COLORS[1]
routes_a_layer = draw_routes(stops_a, "#808080", "single")
routes_b_layer = draw_routes(stops_b, "#808080", "single")
stations_a_layer = draw_stations(stops_a, COLOR_A)
stations_b_layer = draw_stations(stops_b, COLOR_B)
stations_shared_layer = draw_stations(shared_stops, COLOR_SHARED, "square")
# if start is in shared use shared color
marker_a_color, marker_b_color = COLORS
if selected_city_a in shared_stops["parent_station"].values:
marker_a_color = COLOR_SHARED
if selected_city_b in shared_stops["parent_station"].values:
marker_b_color = COLOR_SHARED
start_a = draw_stations(
stops_a.loc[stops_a["parent_station"] == selected_city_a].drop_duplicates(
"parent_station"
),
marker_a_color,
"star",
)
start_b = draw_stations(
stops_b.loc[stops_b["parent_station"] == selected_city_b].drop_duplicates(
"parent_station"
),
marker_b_color,
"star",
)
routes_a_layer.add_to(map_main)
routes_b_layer.add_to(map_main)
stations_a_layer.add_to(map_main)
stations_b_layer.add_to(map_main)
stations_shared_layer.add_to(map_main)
start_a.add_to(map_main)
start_b.add_to(map_main)
map_main.get_root().add_child(generate_main_legend())
selection = st_folium(
map_main,
height=800,
returned_objects=["last_object_clicked_tooltip"],
zoom=8,
center=MAP_CENTER,
use_container_width=True,
key="main_map",
)
st.markdown(f"Destination A and B have {len(shared_stops)} shared stations.")
station_distance_container = st.container()
with station_distance_container:
st.markdown("## Station overview of selected route")
# after selected route
# display every station and how long it takes to get from each one
# some kind of graph
if selection["last_object_clicked_tooltip"]:
id_match = re.match(r"\[(.+)\]", selection["last_object_clicked_tooltip"])
if id_match:
selected_route = get_stops(
feed,
[id_match[1]],
active_weekdays,
relevant_hours,
)
chart_data, time_data = route_details(
selected_route, shared_stops["parent_station"]
)
detail_chart = draw_route_detail(chart_data, time_data)
st.altair_chart(detail_chart, True)
else:
st.markdown("Select a route from above to display its stations")
# TODO hover/ click on station highlights the station in other charts
else:
st.markdown("Select a route from above to display its stations")
mini_map_container = st.container()
mini_a, mini_b = mini_map_container.columns(2)
with mini_a:
st.markdown("## Routes and stations of destination A")
map_a = folium.Map(tiles="cartodbpositron")
routes_a_layer = draw_routes(stops_a, "plasma")
stations_a_layer = draw_stations(stops_a, "#ffffbf")
start_a = draw_stations(
stops_a.loc[stops_a["parent_station"] == selected_city_a].drop_duplicates(
"parent_station"
),
COLOR_A,
"star",
)
routes_a_layer.add_to(map_a)
stations_a_layer.add_to(map_a)
start_a.add_to(map_a)
map_a.get_root().add_child(generate_sub_a_legend())
st_folium(
map_a,
height=400,
zoom=8,
center=MAP_CENTER,
use_container_width=True,
key="mini_map_a",
returned_objects=[],
)
with mini_b:
st.markdown("## Routes and stations of destination B")
map_b = folium.Map(tiles="cartodbpositron")
routes_b_layer = draw_routes(stops_b, "viridis")
stations_b_layer = draw_stations(stops_b, "#91bfdb")
start_b = draw_stations(
stops_b.loc[stops_b["parent_station"] == selected_city_b].drop_duplicates(
"parent_station"
),
COLOR_B,
"star",
)
routes_b_layer.add_to(map_b)
stations_b_layer.add_to(map_b)
start_b.add_to(map_b)
map_b.get_root().add_child(generate_sub_b_legend())
st_folium(
map_b,
height=400,
zoom=8,
center=MAP_CENTER,
use_container_width=True,
key="mini_map_b",
returned_objects=[],
)