-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDash_bike_App.py
237 lines (208 loc) · 10.3 KB
/
Dash_bike_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
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
#!usr/bin/env
import networkx as nx
import pandas as pd
import osmnx as ox
from dash import Dash
import json
import dash_html_components as html
import dash_core_components as dcc
import dash_leaflet as dl
import plotly.express as px
import requests
import dash_leaflet.express as dlx
from dash_extensions.javascript import arrow_function
from dash.dependencies import Output, Input, State
from dash_extensions.javascript import assign
from math import pi,sqrt,sin,cos,atan2
import folium
# 1. read data
df1 = pd.read_csv(r"dataset/somerville_trip.csv")
k1 = pd.read_csv(r"dataset/station_with_ox.csv")
def haversine(pos1, pos2): #pos1 is[lat, lon]
lat1 = float(pos1[0])
long1 = float(pos1[1])
lat2 = float(pos2[0])
long2 = float(pos2[1])
degree_to_rad = float(pi / 180.0)
d_lat = (lat2 - lat1) * degree_to_rad
d_long = (long2 - long1) * degree_to_rad
a = pow(sin(d_lat / 2), 2) + cos(lat1 * degree_to_rad) * cos(lat2 * degree_to_rad) * pow(sin(d_long / 2), 2)
c = 2 * atan2(sqrt(a), sqrt(1 - a))
km = 6367 * c
mi = 3956 * c
return {"km":km, "miles":mi}
# Create marker cluster.
dicts = [dict(value = m["station_id"],name=m["station_id"], lat=m["Latitude"], lon=m["Longitude"]) for _, m in k1.iterrows()]
dd_defaults = [o["value"] for o in dicts]
# Create javascript function that filters on feature name.
geojson_filter = assign("function(feature, context){return context.props.hideout != feature.properties.name;}")
cluster = dl.GeoJSON(id="markers", data=dlx.dicts_to_geojson(dicts), options = dict(filter = geojson_filter),
hideout = dd_defaults, cluster=True, zoomToBoundsOnClick=True,
children=[dl.Tooltip(id="tooltip")])
# Create app.
app = Dash(prevent_initial_callbacks=True)
app.layout = html.Div(children = [
html.H1("Bluebike App"),
html.Div(
className="div-for-dropdown",
children=[
# Dropdown to select times
dcc.Dropdown(
id="bar-selector",
options=[
{
"label": m["station_id"],
"value": m["station_id"],
}
for _, m in k1.iterrows()
],
multi=True,
placeholder="select station",
)]),
html.Div(["radius(meter): ",dcc.Input(id='radius-input', value='500', type='text')]),
html.Div(dl.Map([dl.TileLayer(), cluster, dl.LayerGroup(id="container", children=[]),
dl.LayerGroup(id = "selected_marker", children = [])],
zoom= 10, center=(42.3848, -71.0951)),
style={'width': '100%', 'height': '70vh', 'margin': "auto", "display": "block"}),
html.Div(id = "all_stations_in_radius"),
html.H5("shortest route between stations"),
dcc.Dropdown(id="from_selector",
options=[{
"label": m["station_id"],
"value": m["station_id"],
} for _, m in k1.iterrows()],
multi=False,
placeholder="start station"),
dcc.Dropdown(id="to_selector",
options=[{
"label": m["station_id"],
"value": m["station_id"],
} for _, m in k1.iterrows()],
multi=False,
placeholder="end station"),
html.Iframe(id = "station_distance",
srcDoc = open("ox_folium/basemap_station.html", "r").read(),
width = "100%", height = "450"),
])
# update the dropdown selector values when the marker is clicked
@app.callback(
Output("bar-selector", "value"),
[Input("markers", "click_feature")],
)
def update_bar_selector(feature):
#holder = []
#marker_id = dash.callback_context.triggered[0]["prop_id"].split(".")[0]
#holder.append(marker_id)
holder = []
holder.append(feature["properties"]["name"])
return list(set(holder))
## change marker color when clicked: by adding a another marker at the selected location with different color
app.clientside_callback("function(x){return x;}", Output("markers", "hideout"), Input("bar-selector", "value"))
# add a new CircleMarker here
@app.callback(
Output("selected_marker", "children"),
[Input("bar-selector", "value"), State("selected_marker", "children")]
)
def last_selected_marker(station_id, children):
if not station_id:
children.clear()
return children
station = k1[k1["station_id"] == station_id[0]]
lat, lon = station.loc[:,"station_lat"].values[0], station.loc[:,"station_lon"].values[0]
children.clear()
children.append(dl.Circle(center=[lat, lon], radius=10, color='rgb(0, 0, 255)')) #color red: (255, 0, 0)
return children
# get marker lonlat and pass it to circle position and make a circle with input radius in meters
@app.callback(
Output("container", "children"),
[Input("bar-selector", "value"), Input("radius-input", "value"),State("container", "children")]
)
def plot_radius(val1, val2, children):
if not val2 or val2 == "0":
radius = 1000
else:
radius = int(val2)
if not val1:
children.clear()
return children
else:
tooltip_text = "radius of " + str(radius)
station = k1[k1["station_id"] == val1[0]]
lat, lon = station.loc[:,"station_lat"].values[0], station.loc[:,"station_lon"].values[0]
children.clear()
children.append(dl.Circle(center=[lat, lon], radius=radius, color='rgb(255,128,0)', children=[
dl.Tooltip(tooltip_text)]))
return children
## calculate the all stations that is inside the radius, if radius is not selected, default is 1000 meters
@app.callback(
Output("all_stations_in_radius", "children"),
[Input("bar-selector", "value"), Input("radius-input", "value")]
)
def station_in_radius(station_id, radius_val):
if (not station_id) or (not radius_val):
return "none"
if int(radius_val) <= 0:
return "none"
station = k1[k1["station_id"] == station_id[0]]
lat, lon = station.loc[:,"station_lat"].values[0], station.loc[:,"station_lon"].values[0]
select_radius = int(radius_val)
output_stations = []
for _, row in k1.iterrows():
havr = haversine([row["Latitude"], row["Longitude"]], [lat, lon])
if havr["km"]*1000 <= select_radius:
output_stations.append((row["station_id"], round(havr["km"]*1000,2)))
return json.dumps(output_stations)
# 3. callback to calculate the shortest distance of two stations, and plot them
def get_single_marker(station_id, lat, lon):
m = folium.Map(location = [lat, lon], zoom_start = 13)
text_on_marker = station_id
folium.Marker([lat, lon], tooltip = text_on_marker,icon = folium.Icon(color = "red", icon = "info-sign")).add_to(m)
filepath = "ox_folium/signle_marker.html"
m.save(filepath)
return open(filepath, "r").read()
## calculate shortest path between two stations and display them
@app.callback(
Output("station_distance", "srcDoc"),
[Input("from_selector", "value"), Input("to_selector", "value")]
)
def get_shortest_path(val1, val2):
"""
A shortest path of bike path system,
1. some highway are not allowed bike ride
2. there might be several shortest paths between two nodes
"""
if val1 and val2:
from_station = k1[k1["station_id"] == int(val1)].copy().reset_index(drop = True)
from_lat, from_lon = from_station.loc[:,"station_lat"].values[0], from_station.loc[:,"station_lon"].values[0]
to_station = k1[k1["station_id"] == int(val2)].copy().reset_index(drop = True)
to_lat, to_lon = to_station.loc[:,"station_lat"].values[0], to_station.loc[:,"station_lon"].values[0]
if from_lat == to_lat and from_lon == to_lon:
return get_single_marker(int(val1), to_lat, to_lon)
graph_path = r"ox_folium/somerville.graphml"
G = ox.io.load_graphml(graph_path)
src_ox_id = from_station.loc[:, "ox_nearest_node_id"].values[0]
dst_ox_id = to_station.loc[:, "ox_nearest_node_id"].values[0]
route = nx.shortest_path(G, src_ox_id, dst_ox_id)
navigation_map = folium.Map(location = [42.401962, -71.092053], zoom_start=12)
tooltip_from_id = val1
tooltip_to_id = val2
folium.Marker([from_lat, from_lon],tooltip = tooltip_from_id).add_to(navigation_map)
folium.Marker([to_lat, to_lon], tooltip = tooltip_to_id).add_to(navigation_map)
shortest_path_map = ox.plot_route_folium(G, route, route_map = navigation_map, popup_attribute = "length", weight = 7)
shortest_route_html = "ox_folium/shortest_route_graph.html"
shortest_path_map.save(shortest_route_html)
return open(shortest_route_html, "r").read()
if (not val1) and (not val2):
return open("ox_folium/basemap_station.html", "r").read()
## update markers hover feature: show station traffic when hover on it
@app.callback(Output("tooltip", "children"), [Input("markers", "hover_feature")])
def update_tooltip(feature):
if feature is None:
return None
fig = px.histogram(df1[df1["station_id"] == feature["properties"]["name"]], x = "starttime_month",nbins = 12)
fig.layout.title = feature["properties"]["name"]
fig.layout.width = 400
fig.layout.height = 300
return dcc.Graph(figure=fig)
if __name__ == '__main__':
app.run_server(debug = False)