-
Notifications
You must be signed in to change notification settings - Fork 0
/
streamlit_app.py
164 lines (140 loc) · 4.74 KB
/
streamlit_app.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
import urllib.request
from pathlib import Path
import folium
import gtfs_kit as gk
import plotly.express as px
import streamlit as st
from shapely.geometry import mapping
from streamlit_folium import st_folium
filename = "CT_GTFS.zip"
st.set_page_config(page_title="yyc-gtfs", page_icon=":oncoming_bus:")
_, center, _ = st.columns([2, 1, 2])
with center:
st.image(
"https://cdn0.iconfinder.com/data/icons/citycons/150/Citycons_bus-512.png",
use_column_width=True,
)
st.title("yyc-gtfs")
st.caption("A Streamlit app to visualize City of Calgary's GTFS feed")
path = Path(filename)
if path.exists():
st.success(f"{filename} has been found from disk!")
else:
with st.spinner(f"Downloading {filename}..."):
urllib.request.urlretrieve(
r"https://data.calgary.ca/download/npk7-z3bj/application%2Fx-zip-compressed",
filename,
)
st.success(f"{filename} has been downloaded!")
@st.cache_data
def load_feed(path):
feed = gk.read_feed(path, dist_units="km")
return feed
@st.cache_data
def get_trip_stats(_feed):
return _feed.compute_trip_stats()
feed = load_feed(path)
colors = gk.COLORS_SET2
color_size = len(colors)
centroid = feed.compute_centroid()
lat, lon = centroid.y, centroid.x
if st.secrets.get("describe"):
st.subheader("Stats")
with st.spinner(f"loading {filename}..."):
st.dataframe(feed.describe(), use_container_width=True)
if st.secrets.get("validate"):
st.subheader("Validation")
with st.spinner(f"validating {filename}..."):
st.dataframe(feed.validate(), use_container_width=True)
with st.spinner(f"Computing trip stats..."):
trip_stats = get_trip_stats(feed)
st.subheader("Stops")
st.text(f"There are {feed.get_stops()['stop_id'].nunique()} stops.")
m = feed.map_stops(feed.get_stops()["stop_id"])
st_folium(m)
st.subheader("Routes")
geometrize_routes = feed.geometrize_routes()
duplicated = geometrize_routes["route_short_name"].duplicated()
geometrize_routes = geometrize_routes[~duplicated]
st.text(f"There are {geometrize_routes['route_short_name'].nunique()} routes.")
fig = px.histogram(
trip_stats.groupby(["route_short_name", "speed"]).nunique().reset_index(),
x="speed",
labels={
"speed": "Speed (km/h)",
},
)
fig
fig = px.histogram(
trip_stats.groupby(["route_short_name", "distance"])
.nunique()
.reset_index()
.groupby("route_short_name")
.count()
.reset_index(),
x="distance",
color="distance",
labels={
"distance": "Number of Trips",
},
)
fig
st.text("Each route could have different numbers of trips.")
selected_routes = st.multiselect(
"Choose routes for info about their longest trips",
[
f"{d} {n}"
for d, n in zip(
geometrize_routes["route_short_name"].tolist(),
geometrize_routes["route_long_name"].tolist(),
)
],
)
if selected_routes:
# feed.map_routes() does not seem to work with streamlit
def get_route_short_name(name):
return name.split()[0]
route_short_names = [get_route_short_name(name) for name in selected_routes]
routes = geometrize_routes[
geometrize_routes["route_short_name"].isin(route_short_names)
]
m = folium.Map((lat, lon))
def get_locations(coords):
return [(b, a) for a, b in coords]
color_ind = 0
colors_used = {}
for row in routes[["route_short_name", "geometry"]].to_dict("records"):
geometry = row["geometry"]
name = row["route_short_name"]
coords = mapping(geometry)["coordinates"]
color = colors[color_ind % color_size]
colors_used[name] = color
try:
folium.PolyLine(get_locations(coords), color=color).add_to(m)
except ValueError:
for _coords in coords:
folium.PolyLine(get_locations(_coords), color=color).add_to(m)
finally:
color_ind += 1
st_folium(m)
selected_trip_stats = (
trip_stats[trip_stats["route_short_name"].isin(route_short_names)]
.groupby(["route_short_name"])
.max()
)
for name, route_short_name in zip(selected_routes, route_short_names):
_stats = selected_trip_stats.loc[route_short_name]
distance = _stats["distance"]
duration = _stats["duration"] * 60
speed = _stats["speed"]
loop = "Yes" if _stats["is_loop"] else "No"
color = colors_used[route_short_name]
st.caption(
f'<p style="color:{color};">{name}</p>',
unsafe_allow_html=True,
)
col1, col2, col3, col4 = st.columns(4)
col1.metric("Distance (km)", f"{distance:.2f}")
col2.metric("Duration (mins)", f"{duration:.2f}")
col3.metric("Speed (km/h)", f"{speed:.2f}")
col4.metric("Loop?", loop)