forked from Snowflake-Labs/sfquickstarts
/
opensky.py
executable file
·233 lines (202 loc) · 8.09 KB
/
opensky.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
'''
Snowflake Inc. Code Disclaimer:
The code provided by Snowflake is for demostration purposes only and is provided "as is," without any warranties. Use at your own risk. We do not provide technical support. You may modify the code but do not distribute it without proper attribution. Comply with all applicable laws. By using the code, you agree to this disclaimer.
Snowflake, Inc.
Last updated: 8/7/2023
'''
import requests,json,numpy as np,sys
import pandas as pd
import streamlit as st
import snowflake.connector
import time as time
import toml
import pydeck as pdk
import plotly.express as px
import folium
from streamlit_folium import folium_static
import altair as alt
from datetime import datetime, timedelta
import os, json
# Connect to Consumer Account
def init_connection():
return snowflake.connector.connect(**st.secrets["snowcat"], client_session_keep_alive=True)
def run_query(query):
with conn.cursor() as cur:
cur.execute(query)
# Return a Pandas DataFrame containing all of the results.
#df = cur.fetch_pandas_all()
return cur.fetchall();
def is_dst(zonename):
from datetime import datetime, timedelta
import pytz
tz = pytz.timezone(zonename)
now = pytz.utc.localize(datetime.utcnow())
return now.astimezone(tz).dst() != timedelta(0)
def update_query_params():
hour_selected = st.session_state["pickup_hour"]
st.experimental_set_query_params(pickup_hour=hour_selected)
# Main
# SETTING PAGE CONFIG TO WIDE MODE AND ADDING A TITLE AND FAVICON
conn = init_connection()
st.set_page_config(layout="wide", page_title="Flight Tracking Demo", page_icon=":airplane:")
headers = {'Content-type': 'application/json'}
hour_selected = st.slider(
"Select X hours into the past", 1, 24, key="pickup_hour", value=4, on_change=update_query_params
)
role=os.getenv('role')
wh=os.getenv('warehouse')
db=os.getenv('db')
schema=os.getenv('schema')
table=os.getenv('table')
view=".".join([db,schema,table])
run_query("use role " + role + " ;")
run_query("use warehouse " + wh + " ;")
#daylight saving time is utc-7, so current_time(pacific time) + 4 is past 3 hour data
#standard time is utc-8
past_x_hrs=int(hour_selected)
if is_dst("America/Los_Angeles"):
tdiff=7
else:
tdiff=8
delta=int(tdiff)-int(past_x_hrs)
col1, col2, col3 = st.columns(3)
#bar chart
results=run_query("select dest,count(distinct(id)) as arrivals from " + view + " WHERE ts_utc >= DATEADD(hour, " + str(delta) + ", convert_timezone('UTC','America/Los_Angeles',CURRENT_TIMESTAMP())) group by dest;")
df=pd.DataFrame(results,columns=['airport','arrivals'])
fig = px.bar(
df,
x = "airport",
y = "arrivals",
title = "Arrivals by Airports in the last " + str(abs(past_x_hrs)) +" hours"
)
col1.plotly_chart(fig)
#Pie chart
fig = px.pie(
df,
names = 'airport',
values = 'arrivals',
title = "Arrivals by Airports in the last " + str(abs(past_x_hrs)) +" hours"
)
col2.plotly_chart(fig)
#Stacked bar chart for arrival flights
results=run_query("select hour(dateadd(hour, -" + str(tdiff) + ", ts_utc)) as hhr,dest,count(distinct(id)) as flights from " + view + " WHERE ts_utc >= DATEADD(hour, " + str(delta) + ", convert_timezone('UTC','America/Los_Angeles',CURRENT_TIMESTAMP())) and (dest='KSFO' or dest='KOAK' or dest='KSJC') group by hhr,dest order by hhr;")
df=pd.DataFrame(results,columns=['hour','airport','flights'])
df_ksfo=df[df['airport']=='KSFO']
df_ksjc=df[df['airport']=='KSJC']
df_koak=df[df['airport']=='KOAK']
df_ksfo.columns=['hour','airport','KSFO']
f_ksfo=df_ksfo.iloc[:,[0,2]]
df_ksjc.columns=['hour','airport','KSJC']
f_ksjc=df_ksjc.iloc[:,[0,2]]
df_koak.columns=['hour','airport','KOAK']
f_koak=df_koak.iloc[:,[0,2]]
all=pd.merge(f_ksfo, f_ksjc, how='outer', on = 'hour')
all=pd.merge(all, f_koak, how='outer', on = 'hour')
fig = px.bar(
all, #Data Frame
x = "hour", #Columns from the data frame
y = ['KSFO','KSJC','KOAK'],
title = "Number of SFO,SJC,OAK arrivals in the last " + str(abs(past_x_hrs)) + " hours"
)
fig.update_yaxes(title=' ')
col3.plotly_chart(fig)
col21, col22 = st.columns(2)
#map plotting 2D, alt mono color
col21.write("2D arrival flight paths over the bay")
results=run_query("select * from " + view + " WHERE ts_utc >= DATEADD(hour, " + str(delta) + ", convert_timezone('UTC','America/Los_Angeles',CURRENT_TIMESTAMP())) and (dest='KSFO' or dest='KOAK' or dest='KSJC') order by ts_pt desc;")
df=pd.DataFrame(results)
of=df.iloc[:,1:14]
of.columns=['pt','alt','dest','orig','id','icao','lat','lon','geohash','yr','mo','dd','hr']
mdf = pd.DataFrame(of,columns=['lat', 'lon', 'alt']).dropna()
#Folium map plotting 2D, alt color coded
m = folium.Map(location=[37.6213129,-122.3811494], zoom_start=11, tiles='OpenStreetMap')
ceiling=10000 # don't plot over ceiling
radius=300
mdf=mdf[mdf['alt'] < ceiling]
mdf1=mdf[mdf['alt'].between(0,2000)]
mdf2=mdf[mdf['alt'].between(2000,4000)]
mdf3=mdf[mdf['alt'].between(4000,6000)]
mdf4=mdf[mdf['alt'] > 6000]
for idx, eq in mdf1.iterrows():
folium.Circle(location=(eq['lat'], eq['lon']), color='red', fill_color='red', fill_opacity=1.0, radius=radius).add_to(m)
for idx, eq in mdf2.iterrows():
folium.Circle(location=(eq['lat'], eq['lon']), color='purple', fill_color='purple', fill_opacity=1.0, radius=radius ).add_to(m)
for idx, eq in mdf3.iterrows():
folium.Circle(location=(eq['lat'], eq['lon']), color='blue', fill_color='blue', fill_opacity=1.0, radius=radius).add_to(m)
for idx, eq in mdf4.iterrows():
folium.Circle(location=(eq['lat'], eq['lon']), color='green', fill_color='green', fill_opacity=1.0, radius=radius).add_to(m)
with col21:
st.write("Red: below 2k ft, Purple: 2k-4k ft, Blue: 4k-6k ft, Green: above 6k ft")
folium_static(m, width=700, height=400)
#folium_static(m)
#map plotting 3D
col22.write("3D arrival flight paths over the bay, altitude color-coded")
radius=300 #control the radius of the columns
escale=15 #control height of the columns
col22.write("Red: below 2k ft, Purple: 2k-4k ft, Blue: 4k-6k ft, Green: above 6k ft")
col22.pydeck_chart(pdk.Deck(
map_style='mapbox://styles/mapbox/streets-v11',
initial_view_state=pdk.ViewState(
latitude=37.6213129,
longitude=-122.3789554,
zoom=9,
pitch=50,
height=400,
width=700
),
layers=[
pdk.Layer(
#'HexagonLayer', #HexagonLayer counts the dot in same coord and plot as height
'ColumnLayer', #ColumnLayer uses the get_elevation column as height
data=mdf1,
get_position='[lon, lat]',
get_elevation='[alt]/25',
get_fill_color=[255, 0, 0 , 150],
radius=radius,
elevation_scale=escale,
elevation_range=[1, 1000],
pickable=True,
extruded=True,
auto_highlight=True,
),
pdk.Layer(
'ColumnLayer', #ColumnLayer uses the get_elevation column as height
data=mdf2,
get_position='[lon, lat]',
get_elevation='[alt]/25',
get_fill_color=[148, 0, 211, 160],
radius=radius,
elevation_scale=escale,
elevation_range=[1, 1000],
pickable=True,
extruded=True,
auto_highlight=True,
),
pdk.Layer(
'ColumnLayer', #ColumnLayer uses the get_elevation column as height
data=mdf3,
get_position='[lon, lat]',
get_elevation='[alt]/25',
get_fill_color=[100, 149, 237 ,255],
radius=radius,
elevation_scale=escale,
elevation_range=[1, 1000],
pickable=True,
extruded=True,
auto_highlight=True,
),
pdk.Layer(
'ColumnLayer', #ColumnLayer uses the get_elevation column as height
data=mdf4,
get_position='[lon, lat]',
get_elevation='[alt]/25',
get_fill_color=[127,255,0,120],
radius=radius,
elevation_scale=escale,
elevation_range=[1, 2000],
pickable=True,
extruded=True,
auto_highlight=True,
),
],
))