-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathrealtime_stm_bus_positions.py
110 lines (73 loc) · 2.87 KB
/
realtime_stm_bus_positions.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
from flask import Flask, request, render_template
#from flask import render_template
import numpy as np
import requests
from google.transit import gtfs_realtime_pb2
import json
import plotly
import plotly.graph_objects as go
from plotly.subplots import make_subplots
class StmViz:
def getBuses():
'''
Fetch the current bus position from the stm api url,
Parse the location and return as an array
'''
url = "https://api.stm.info/pub/od/gtfs-rt/ic/v1/vehiclePositions/"
key = '<YOUR API KEY GOES HERE>' # Primary key for real-time visualization
# Get the bus position data
headers = {
'apikey': key
}
response = requests.request("POST", url, headers=headers)
feed = gtfs_realtime_pb2.FeedMessage()
feed.ParseFromString(response.content)
#Process the feed data
entities = feed.entity
locations = np.empty([len(entities),2])
for i,entity in enumerate(entities):
locations[i,0] = entity.vehicle.position.longitude
locations[i,1] = entity.vehicle.position.latitude
return locations
def create_map():
'''
Create a navigable plotly scatterplot on a map, which the bus locations plotted.
'''
locations = StmViz.getBuses()
mapbox_access_token = 'pk.eyJ1IjoicXVvcnVtZXRyaXgiLCJhIjoiY2p5OHFzaHU3MDJjOTNocjFkcGI3czh1eSJ9.sy8pqCm5v-3Wcp36JLwCeA'
fig = go.Figure(go.Scattermapbox(
lat=locations[:,1],
lon=locations[:,0],
mode='markers',
marker=go.scattermapbox.Marker(
size=5,
color='rgb(20, 20, 255)',
opacity=0.7
),
hoverinfo='text',
showlegend=False,
name="Bus Positions"))#,
fig.update_layout(
hovermode='closest',
mapbox=go.layout.Mapbox(
accesstoken=mapbox_access_token,
bearing=0,
center=go.layout.mapbox.Center(
lat=45.52,
lon=-73.7
),
pitch=0,
zoom=8,
)
)
graphJSON = json.dumps(fig, cls=plotly.utils.PlotlyJSONEncoder)
return graphJSON
def get_position_list():
locations = StmViz.getBuses()
position_list = json.dumps(locations, cls=NumpyEncoder)
return position_list
class NumpyEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, np.ndarray):
return obj.tolist()
return json.JSONEncoder.default(self, obj)