-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmap_plot_.py
More file actions
266 lines (246 loc) · 10.2 KB
/
map_plot_.py
File metadata and controls
266 lines (246 loc) · 10.2 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
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
from dash import Dash, html, dcc, Input, Output, callback, State
import plotly.express as px
import pandas as pd
from plotly import graph_objs as go
from plotly.graph_objs import *
from datetime import datetime as dt
import numpy as np
# Plotly mapbox public token
mapbox_access_token = "pk.eyJ1IjoicGxvdGx5bWFwYm94IiwiYSI6ImNrOWJqb2F4djBnMjEzbG50amg0dnJieG4ifQ.Zme1-Uzoi75IaFbieBDl3A"
app = Dash(
__name__, meta_tags=[{"name": "viewport", "content": "width=device-width"}],
)
app.title = "New York Uber Rides"
server = app.server
# Dictionary of important locations in New York
list_of_locations = {
"Madison Square Garden": {"lat": 40.7505, "lon": -73.9934},
"Yankee Stadium": {"lat": 40.8296, "lon": -73.9262},
"Empire State Building": {"lat": 40.7484, "lon": -73.9857},
"New York Stock Exchange": {"lat": 40.7069, "lon": -74.0113},
"JFK Airport": {"lat": 40.644987, "lon": -73.785607},
"Grand Central Station": {"lat": 40.7527, "lon": -73.9772},
"Times Square": {"lat": 40.7589, "lon": -73.9851},
"Columbia University": {"lat": 40.8075, "lon": -73.9626},
"United Nations HQ": {"lat": 40.7489, "lon": -73.9680},
}
app.layout = html.Div(
children=[
html.Div(
className="row",
children=[
# Column for user controls
html.Div(
className="four columns div-user-controls",
children=[
html.A(
html.Img(
className="logo",
src=app.get_asset_url("dash-logo-new.png"),
),
href="https://plotly.com/dash/",
),
html.H2("DASH - UBER DATA APP"),
html.P(
"""Select different days using the date picker or by selecting
different time frames on the histogram."""
),
# Change to side-by-side for mobile layout
html.Div(
className="row",
children=[
html.Div(
className="div-for-dropdown",
children=[
# Dropdown for locations on map
dcc.Dropdown(
id="location-dropdown",
options=[
{"label": i, "value": i}
for i in list_of_locations
],
placeholder="Select a location",
)
],
),
html.Div(
className="div-for-dropdown",
children=[
# Dropdown to select times
dcc.Dropdown(
id="bar-selector",
options=[
{
"label": str(n) + ":00",
"value": str(n),
}
for n in range(24)
],
multi=True,
placeholder="Select certain hours",
)
],
),
],
),
html.P(id="total-rides"),
html.P(id="total-rides-selection"),
html.P(id="date-value"),
dcc.Markdown(
"""
Source: [FiveThirtyEight](https://github.com/fivethirtyeight/uber-tlc-foil-response/tree/master/uber-trip-data)
Links: [Source Code](https://github.com/plotly/dash-sample-apps/tree/main/apps/dash-uber-rides-demo) | [Enterprise Demo](https://plotly.com/get-demo/)
"""
),
],
),
# Column for app graphs and plots
html.Div(
className="eight columns div-for-charts bg-grey",
children=[
dcc.Graph(id="map-graph"),
],
),
],
)
]
)
# Get the Coordinates of the chosen months, dates and times
def getLatLonColor(selectedData, month, day):
listCoords = []
# No times selected, output all times for chosen month and date
if selectedData is None or len(selectedData) is 0:
return listCoords
listStr = "listCoords["
for time in selectedData:
if selectedData.index(time) is not len(selectedData) - 1:
listStr += "(totalList[month][day].index.hour==" + str(int(time)) + ") | "
else:
listStr += "(totalList[month][day].index.hour==" + str(int(time)) + ")]"
return eval(listStr)
# Update Map Graph based on date-picker, selected data on histogram and location dropdown
@app.callback(
Output("map-graph", "figure"),
[
# Input("date-picker", "date"),
# Input("bar-selector", "value"),
Input("location-dropdown", "value"),
],
)
def update_graph(selectedLocation):
zoom = 12.0
latInitial = 40.7272
lonInitial = -73.991251
bearing = 0
if selectedLocation:
zoom = 15.0
latInitial = list_of_locations[selectedLocation]["lat"]
lonInitial = list_of_locations[selectedLocation]["lon"]
# date_picked = dt.strptime(datePicked, "%Y-%m-%d")
# monthPicked = date_picked.month - 4
# dayPicked = date_picked.day - 1
# listCoords = getLatLonColor(selectedData, monthPicked, dayPicked)
listCoords = []
return go.Figure(
data=[
# Data for all rides based on date and time
Scattermapbox(
# lat=listCoords["Lat"],
# lon=listCoords["Lon"],
lat=[40.7589],
lon=[-73.9851],
mode="markers",
hoverinfo="lat+lon+text",
# text=listCoords.index.hour,
marker=dict(
showscale=True,
# color=np.append(np.insert(listCoords.index.hour, 0, 0), 23),
opacity=0.5,
size=5,
colorscale=[
[0, "#F4EC15"],
[0.04167, "#DAF017"],
[0.0833, "#BBEC19"],
[0.125, "#9DE81B"],
[0.1667, "#80E41D"],
[0.2083, "#66E01F"],
[0.25, "#4CDC20"],
[0.292, "#34D822"],
[0.333, "#24D249"],
[0.375, "#25D042"],
[0.4167, "#26CC58"],
[0.4583, "#28C86D"],
[0.50, "#29C481"],
[0.54167, "#2AC093"],
[0.5833, "#2BBCA4"],
[1.0, "#613099"],
],
colorbar=dict(
title="Time of<br>Day",
x=0.93,
xpad=0,
nticks=24,
tickfont=dict(color="#d8d8d8"),
titlefont=dict(color="#d8d8d8"),
thicknessmode="pixels",
),
),
),
# Plot of important locations on the map
Scattermapbox(
lat=[list_of_locations[i]["lat"] for i in list_of_locations],
lon=[list_of_locations[i]["lon"] for i in list_of_locations],
mode="markers",
hoverinfo="text",
text=[i for i in list_of_locations],
marker=dict(size=8, color="#ffa0a0"),
),
],
layout=Layout(
autosize=True,
margin=go.layout.Margin(l=0, r=35, t=0, b=0),
showlegend=False,
mapbox=dict(
accesstoken=mapbox_access_token,
center=dict(lat=latInitial, lon=lonInitial), # 40.7272 # -73.991251
style="dark",
bearing=bearing,
zoom=zoom,
),
updatemenus=[
dict(
buttons=(
[
dict(
args=[
{
"mapbox.zoom": 12,
"mapbox.center.lon": "-73.991251",
"mapbox.center.lat": "40.7272",
"mapbox.bearing": 0,
"mapbox.style": "dark",
}
],
label="Reset Zoom",
method="relayout",
)
]
),
direction="left",
pad={"r": 0, "t": 0, "b": 0, "l": 0},
showactive=False,
type="buttons",
x=0.45,
y=0.02,
xanchor="left",
yanchor="bottom",
bgcolor="#323130",
borderwidth=1,
bordercolor="#6d6d6d",
font=dict(color="#FFFFFF"),
)
],
),
)
if __name__ == "__main__":
app.run(debug=True,port=8004)