forked from bruce0828/Traffic-Index
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVisualization.py
More file actions
222 lines (174 loc) · 7.89 KB
/
Copy pathVisualization.py
File metadata and controls
222 lines (174 loc) · 7.89 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
import pyodbc
import pandas as pd
import numpy as np
from geojson import LineString, Feature, FeatureCollection, dump
import folium
import streamlit as st
import os
import time
import glob
import geopandas
import branca
from folium.features import GeoJson, GeoJsonTooltip
from folium import plugins
import fiona
import fiona.crs
from datetime import datetime
from sys import platform
if platform == "linux" or platform == "linux2":
# linux
SQL_DRIVER = 'ODBC Driver 17 for SQL Server'
elif platform == "darwin":
# OS X
SQL_DRIVER = 'ODBC Driver 17 for SQL Server'
elif platform == "win32":
# Windows...
# SQL_DRIVER = 'ODBC Driver 17 for SQL Server'
SQL_DRIVER = 'SQL Server'
def getDatabaseConnection():
return pyodbc.connect(f'DRIVER={SQL_DRIVER};SERVER=128.95.29.74;DATABASE=RealTimeLoopData;UID=starlab;PWD=star*lab1')
def route_map_func(x):
if x in [5, 90, 405]:
return 'I-' + str(x)
else:
return 'SR ' + str(x)
def name_map_func(x):
return x['route_name'] + ' ' + x['direction_name'] + ' ' + str(int(x['milepost_small'])) + ' to ' + str(int(x['milepost_large']))
def GetSegmentGeo():
# load geo
geo = geopandas.read_file('geodata/step-2.shp')
geo_csv = pd.read_csv('geodata/step-2.csv')
geo_csv.drop(['geometry'], axis = 1, inplace = True)
geo = pd.concat([geo_csv, geo['geometry']], axis = 1)
geo_key = []
for i in range(len(geo)):
geo_key.append(str(geo['route'][i]) + '_' + geo['direct'][i].upper() + '_' + str(int(geo['mile_min'][i])) + '_' + str(int(geo['mile_max'][i])))
geo['key'] = geo_key
# load segments ids
conn = getDatabaseConnection()
SQL_Query = pd.read_sql_query(
''' SELECT *
FROM [RealTimeLoopData].[dbo].[Segments]''', conn)
segmentIDs = pd.DataFrame(SQL_Query)
segmentIDs['route'] = segmentIDs['route'].apply(lambda x: x.strip())
seg_key = []
for i in range(len(segmentIDs)):
seg_key.append(str(segmentIDs['route'][i]) + '_' + segmentIDs['mpdirection'][i].upper() + '_' + str(int(segmentIDs['milepost_small'][i])) + '_' + str(int(segmentIDs['milepost_large'][i])))
segmentIDs['key'] = seg_key
segment = geo.merge(segmentIDs, on = ['key'], how = 'inner')
# pdb.set_trace()
# segment.fillna(0, inplace = True)
# create name
segment['route_name'] = segment['route_x'].apply(route_map_func)
segment['direction_name'] = segment['direction'].apply(lambda x: str(x) + 'B')
segment['name'] = segment.apply(name_map_func, axis = 1)
return segment
colormap = branca.colormap.LinearColormap(vmin = 50,
vmax= 100,
colors=['darkred', 'red','orange','yellow','lightgreen','green'],
caption="Traffic Performance Score")
def style_func(feature):
value = feature['properties']['TrafficIndex_GP']
return {
"color": colormap(value)
if value is not None
else "transparent"
}
def style_func_HOV(feature):
value = feature['properties']['TrafficIndex_HOV']
return {
"color": colormap(value)
if value is not None
else "transparent"
}
def GenerateGeo(TPS):
segment = GetSegmentGeo()
# merge TPS with segment data
segment.rename(columns={"segmentid": "segmentID"}, inplace = True)
# st.write(segment[['segmentID', 'route_name', 'direction_name', 'name']])
# st.write(TPS)
# st.write(segment[['segmentID', 'route_name', 'direction_name', 'name']].merge(TPS, on = ['segmentID'], how = 'inner'))
data = segment.merge(TPS, on = ['segmentID'], how = 'left')
data['TrafficIndex_GP'].fillna(1, inplace = True) # fill nan with zero, becuase Out of range float values are not JSON compliant: nan
data['TrafficIndex_HOV'].fillna(1, inplace = True) # fill nan with zero, becuase Out of range float values are not JSON compliant: nan
scaled_data = data
scaled_data['TrafficIndex_GP'] = data['TrafficIndex_GP']*100
scaled_data['TrafficIndex_HOV'] = data['TrafficIndex_HOV']*100
tooltip = GeoJsonTooltip(
fields=["name", "TrafficIndex_GP", 'time'],
aliases=["Road Segment", "Traffic Performance Score", 'Time'],
localize=True,
sticky=False,
labels=True,
style="""
background-color: #F0EFEF;
border: 2px solid black;
border-radius: 3px;
box-shadow: 3px;
"""
)
tooltip_HOV = GeoJsonTooltip(
fields=["name", "TrafficIndex_HOV", 'time'],
aliases=["Road Segment", "Traffic Performance Score", 'Time'],
localize=True,
sticky=False,
labels=True,
style="""
background-color: #F0EFEF;
border: 2px solid black;
border-radius: 3px;
box-shadow: 3px;
"""
)
data_gdf = geopandas.GeoDataFrame(scaled_data, crs=fiona.crs.from_epsg(4326))
data_gdf['time'] = data_gdf['time'].apply(lambda x: x.isoformat())
m = folium.Map([47.673650, -122.260540], zoom_start=10, tiles="cartodbpositron")
folium.GeoJson(data_gdf, style_function= style_func, tooltip = tooltip, name = 'GP Lane').add_to(m)
folium.GeoJson(data_gdf, style_function= style_func_HOV, tooltip = tooltip_HOV, name = 'HOV Lane', show = False).add_to(m)
colormap.add_to(m)
STREAMLIT_STATIC_PATH = os.path.join(os.path.dirname(st.__file__), 'static')
# st.write(os.path.dirname(st.__file__) + '\\static')
for filename in glob.glob(os.path.join(STREAMLIT_STATIC_PATH, 'map*')):
os.remove(filename)
filename_with_time = f'map_{time.time()}.html'
map_path = os.path.join(STREAMLIT_STATIC_PATH, filename_with_time)
open(map_path, 'w').write(m._repr_html_())
# st.markdown('Below is the traffic performance score by segments:' + dt_string)
st.markdown("Please use **Chrome** for best visualization quality.")
st.markdown(f'<iframe src="/{filename_with_time}" ; style="width:100%; height:480px;"> </iframe>', unsafe_allow_html=True)
def GenerateGeoAnimation(TPS):
TPS.columns = ['time', 'segmentID', 'AVG_Spd_GP', 'AVG_Spd_HOV', 'AVG_Vol_GP', 'AVG_Vol_HOV', 'TrafficIndex_GP', 'TrafficIndex_HOV']
TPS['time'] = TPS['time'].apply(lambda x: datetime.fromtimestamp(datetime.timestamp(x)).astimezone().isoformat())
segment = GetSegmentGeo()
# merge TPS with segment data
segment.rename(columns={"segmentid": "segmentID"}, inplace = True)
data = segment.merge(TPS, on = ['segmentID'], how = 'left')
data['TrafficIndex_GP'] = data['TrafficIndex_GP'].fillna(1)
data['TrafficIndex_HOV'] = data['TrafficIndex_HOV'].fillna(1)
scaled_data = data
scaled_data['TrafficIndex_GP'] = data['TrafficIndex_GP']*100
scaled_data['TrafficIndex_HOV'] = data['TrafficIndex_HOV']*100
temporal_data = segment.merge(TPS, on = ['segmentID'], how = 'left')
temporal_data['TrafficIndex_GP'] = temporal_data['TrafficIndex_GP']*100
temporal_data['TrafficIndex_HOV'] = temporal_data['TrafficIndex_HOV']*100
features = []
for _, line in temporal_data.iterrows():
route = line['geometry']
features.append(Feature(geometry = route, properties={"TrafficIndex_GP":float(line['TrafficIndex_GP']),
"name": line["name"],"times":[line['time']]*len(line['geometry'].coords),
"style":{"color": colormap(line['TrafficIndex_GP']) if not np.isnan(line['TrafficIndex_GP']) else 'green'}}))
m = folium.Map([47.673650, -122.260540], zoom_start=10, tiles="cartodbpositron")
plugins.TimestampedGeoJson({
'type': 'FeatureCollection',
'features': features,
}, period='PT1H', add_last_point= False, max_speed = 10, min_speed = 0.1, transition_time = 1000, loop_button = True, time_slider_drag_update=True).add_to(m)
colormap.add_to(m)
STREAMLIT_STATIC_PATH = os.path.join(os.path.dirname(st.__file__), 'static')
for filename in glob.glob(os.path.join(STREAMLIT_STATIC_PATH, 'ani*')):
os.remove(filename)
filename_with_time = f'ani_{time.time()}.html'
map_path = os.path.join(STREAMLIT_STATIC_PATH, filename_with_time)
open(map_path, 'w').write(m._repr_html_())
# st.markdown('Below is the traffic performance score by segments:' + dt_string)
st.markdown("Please use **Chrome** for best visualization quality.")
st.markdown(f'<iframe src="/{filename_with_time}" ; style="width:100%; height:480px;"> </iframe>', unsafe_allow_html=True)