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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Importing the required libraries
|
2 |
import streamlit as st
|
3 |
+
import pandas as pd
|
4 |
+
import numpy as np
|
5 |
+
import snowflake.connector
|
6 |
+
import json
|
7 |
+
import folium
|
8 |
+
from streamlit_folium import st_folium
|
9 |
+
import pycountry_convert as pc
|
10 |
+
|
11 |
+
# Setup web page - App Title, Page Title and Page Layout
|
12 |
+
APP_TITLE = 'Plotting F1 Circuit Locations into a map using Folium'
|
13 |
+
st.set_page_config(
|
14 |
+
page_title='F1 Circuits',
|
15 |
+
layout='wide',
|
16 |
+
menu_items={'Get Help': 'https://www.linkedin.com/in/daanbakboord',
|
17 |
+
'About': "This app is powered by Snowflake, Streamlit and Folium | Developed by DaAnalytics (Daan Bakboord)"
|
18 |
+
}
|
19 |
+
)
|
20 |
+
|
21 |
+
# Create context
|
22 |
+
def create_sf_session_object():
|
23 |
+
|
24 |
+
if "snowflake_context" not in st.session_state:
|
25 |
+
|
26 |
+
# Setting up Snowflake connection
|
27 |
+
conn_location = '/Users/daanbakboord/Library/Mobile Documents/com~apple~CloudDocs/Seni_BV/Daanalytics/R&D/GitHub/Snowflake/Snowflake/python/streamlit/_streamlit'
|
28 |
+
credential_file = 'cred.json'
|
29 |
+
|
30 |
+
connect = json.loads(open(str(conn_location + '/' + credential_file)).read())
|
31 |
+
|
32 |
+
username = connect['user']
|
33 |
+
password = connect['password']
|
34 |
+
account = connect['account']
|
35 |
+
role = connect['role']
|
36 |
+
|
37 |
+
# Connect to Snowflake
|
38 |
+
|
39 |
+
ctx = snowflake.connector.connect(
|
40 |
+
user = username,
|
41 |
+
password = password,
|
42 |
+
account = account,
|
43 |
+
role = role
|
44 |
+
)
|
45 |
+
st.session_state['snowflake_context'] = ctx
|
46 |
+
|
47 |
+
else:
|
48 |
+
|
49 |
+
ctx = st.session_state['snowflake_context']
|
50 |
+
|
51 |
+
return ctx
|
52 |
+
|
53 |
+
# Get Continent based on Country
|
54 |
+
def country_convert(country_name):
|
55 |
+
|
56 |
+
try:
|
57 |
+
|
58 |
+
country_code = pc.country_name_to_country_alpha2(country_name, cn_name_format='default')
|
59 |
+
|
60 |
+
continent_name = pc.country_alpha2_to_continent_code(country_code)
|
61 |
+
|
62 |
+
return pc.convert_continent_code_to_continent_name(continent_name)
|
63 |
+
|
64 |
+
except (KeyError, TypeError):
|
65 |
+
|
66 |
+
return (country_name)
|
67 |
+
|
68 |
+
# Base color on Continent
|
69 |
+
# 'darkblue', 'white', 'lightblue', 'pink', 'gray', 'green', 'orange', 'darkred', 'black', 'blue', 'cadetblue'
|
70 |
+
#, 'lightgreen', 'purple', 'darkgreen', 'red', 'beige', 'lightred', 'darkpurple', 'lightgray'
|
71 |
+
|
72 |
+
## 'Oceania', 'Asia', 'Europe', 'North America', 'UK', 'South America', 'UAE',
|
73 |
+
## 'Africa', 'Korea'
|
74 |
+
|
75 |
+
def marker_color(continent_name):
|
76 |
+
if continent_name == 'Asia':
|
77 |
+
color = 'pink'
|
78 |
+
elif continent_name == 'Africa':
|
79 |
+
color = 'green'
|
80 |
+
elif continent_name == 'Europe':
|
81 |
+
color = 'blue'
|
82 |
+
elif continent_name == 'North America':
|
83 |
+
color = 'red'
|
84 |
+
elif continent_name == 'South America':
|
85 |
+
color = 'orange'
|
86 |
+
elif continent_name == 'Oceania':
|
87 |
+
color = 'purple'
|
88 |
+
elif continent_name == 'UK':
|
89 |
+
color = 'beige'
|
90 |
+
elif continent_name == 'UAE':
|
91 |
+
color = 'lightgreen'
|
92 |
+
elif continent_name == 'Korea':
|
93 |
+
color = 'cadetblue'
|
94 |
+
else:
|
95 |
+
color = 'grey'
|
96 |
+
return color
|
97 |
+
|
98 |
+
def load_data(cur, map_type):
|
99 |
+
|
100 |
+
global df_f1_con_circuits
|
101 |
+
|
102 |
+
global df_f1_cou_circuits
|
103 |
+
|
104 |
+
global df_qo_f1_circuits
|
105 |
+
|
106 |
+
# Connect to DEMO_DB database
|
107 |
+
cur.execute("USE DATABASE DEMO_DB")
|
108 |
+
|
109 |
+
# Connect to PRE_F1PY schema
|
110 |
+
f1_pre_schema = 'PRE_F1PY'
|
111 |
+
|
112 |
+
cur.execute("USE SCHEMA " + f1_pre_schema)
|
113 |
+
|
114 |
+
# Select Query F1 Circuits
|
115 |
+
|
116 |
+
sql_f1_circuits = """select replace(name, '"','') as name
|
117 |
+
, lat
|
118 |
+
, lng
|
119 |
+
, replace(country, '"','') as country
|
120 |
+
, replace(url, '"','') as url
|
121 |
+
from demo_db.pre_f1py.pre_f1py_circuits
|
122 |
+
"""
|
123 |
+
|
124 |
+
# Query F1 Circuits
|
125 |
+
cur.execute(sql_f1_circuits)
|
126 |
+
|
127 |
+
# Convert Query output to a DataFrame
|
128 |
+
df_qo_f1_circuits = cur.fetch_pandas_all()
|
129 |
+
|
130 |
+
# Add Continent to df_f1_circuits DataFrame
|
131 |
+
df_qo_f1_circuits['CONTINENT'] = df_qo_f1_circuits['COUNTRY'].apply(country_convert)
|
132 |
+
|
133 |
+
# Create Continents DataFrame
|
134 |
+
df_f1_con_circuits = df_qo_f1_circuits['CONTINENT'].unique()
|
135 |
+
|
136 |
+
# Create Country DataFrame
|
137 |
+
df_f1_cou_circuits = df_qo_f1_circuits['COUNTRY'].unique()
|
138 |
+
|
139 |
+
def filter_data(map_type, detail):
|
140 |
+
|
141 |
+
global df_f1_circuits
|
142 |
+
|
143 |
+
if map_type == 'World':
|
144 |
+
|
145 |
+
df_f1_circuits = df_qo_f1_circuits
|
146 |
+
|
147 |
+
elif map_type == 'Continent':
|
148 |
+
|
149 |
+
# Filter df_f1_circuits DataFrame on 'Europe' Continent
|
150 |
+
df_f1_circuits = df_qo_f1_circuits.loc[df_qo_f1_circuits['CONTINENT'] == detail]
|
151 |
+
|
152 |
+
elif map_type == 'Country':
|
153 |
+
|
154 |
+
# Filter df_f1_circuits DataFrame on Country
|
155 |
+
df_f1_circuits = df_qo_f1_circuits.loc[df_qo_f1_circuits['COUNTRY'] == detail]
|
156 |
+
|
157 |
+
# Draw the Folium Map
|
158 |
+
draw_folium_map(map_type)
|
159 |
+
|
160 |
+
def draw_folium_map(map_type):
|
161 |
+
|
162 |
+
global CircuitsMap
|
163 |
+
|
164 |
+
# Creating the Folium Map
|
165 |
+
CircuitsMap = folium.Map(location=[df_f1_circuits.LAT.mean()
|
166 |
+
, df_f1_circuits.LNG.mean()]
|
167 |
+
, zoom_start=5
|
168 |
+
, control_scale=True
|
169 |
+
, tiles='openstreetmap')
|
170 |
+
|
171 |
+
# Adding Tile Layers
|
172 |
+
folium.TileLayer('openstreetmap').add_to(CircuitsMap)
|
173 |
+
folium.TileLayer('cartodb positron').add_to(CircuitsMap)
|
174 |
+
folium.TileLayer('stamenterrain').add_to(CircuitsMap)
|
175 |
+
folium.TileLayer('stamentoner').add_to(CircuitsMap)
|
176 |
+
folium.TileLayer('stamenwatercolor').add_to(CircuitsMap)
|
177 |
+
folium.TileLayer('cartodbdark_matter').add_to(CircuitsMap)
|
178 |
+
|
179 |
+
# Other mapping code (e.g. lines, markers etc.)
|
180 |
+
folium.LayerControl().add_to(CircuitsMap)
|
181 |
+
|
182 |
+
# Add Markers to the map
|
183 |
+
if map_type == 'World':
|
184 |
+
|
185 |
+
for index, location_info in df_f1_circuits.iterrows():
|
186 |
+
folium.Marker([location_info["LAT"], location_info["LNG"]], popup='<a href=' + location_info["URL"] + ' target="_blank">' + location_info["NAME"] + '</a>', icon=folium.Icon(icon_color='white', icon="car", prefix='fa', color=marker_color(location_info["CONTINENT"]))).add_to(CircuitsMap)
|
187 |
+
|
188 |
+
elif map_type == 'Continent':
|
189 |
+
|
190 |
+
for index, location_info in df_f1_circuits.iterrows():
|
191 |
+
folium.Marker([location_info["LAT"], location_info["LNG"]], popup='<a href=' + location_info["URL"] + ' target="_blank">' + location_info["NAME"] + '</a>', icon=folium.Icon(icon_color='white', icon="car", prefix='fa', color=marker_color(location_info["CONTINENT"]))).add_to(CircuitsMap)
|
192 |
+
|
193 |
+
elif map_type == 'Country':
|
194 |
+
|
195 |
+
for index, location_info in df_f1_circuits.iterrows():
|
196 |
+
folium.Marker([location_info["LAT"], location_info["LNG"]], popup='<a href=' + location_info["URL"] + ' target="_blank">' + location_info["NAME"] + '</a>', icon=folium.Icon(icon_color='white', icon="car", prefix='fa', color='darkgreen')).add_to(CircuitsMap)
|
197 |
+
|
198 |
+
# Zoom to LAT, LONG bounds
|
199 |
+
lft_dwn = df_f1_circuits[['LAT', 'LNG']].min().values.tolist() # Left Down
|
200 |
+
top_rgt = df_f1_circuits[['LAT', 'LNG']].max().values.tolist() # Top Right
|
201 |
+
|
202 |
+
CircuitsMap.fit_bounds([lft_dwn, top_rgt])
|
203 |
+
|
204 |
+
# Main code
|
205 |
+
if __name__ == "__main__":
|
206 |
+
|
207 |
+
# Connect to Snowflake
|
208 |
+
ctx = create_sf_session_object()
|
209 |
+
cur = ctx.cursor()
|
210 |
+
|
211 |
+
# Select Map type to show
|
212 |
+
st.sidebar.title("Select Map Type")
|
213 |
+
|
214 |
+
map_type = st.sidebar.radio(
|
215 |
+
"Which map would you like to show?",
|
216 |
+
('World', 'Continent', 'Country'))
|
217 |
+
|
218 |
+
# Load Data into a DataFrame
|
219 |
+
load_data(cur, map_type)
|
220 |
+
|
221 |
+
if map_type == 'World':
|
222 |
+
|
223 |
+
detail = map_type
|
224 |
+
|
225 |
+
st.sidebar.write('You selected:', detail)
|
226 |
+
|
227 |
+
elif map_type == 'Continent':
|
228 |
+
|
229 |
+
idx = int(np.where(df_f1_con_circuits == "Europe")[0][0])
|
230 |
+
|
231 |
+
continent = st.sidebar.selectbox(
|
232 |
+
'Which continent would you like to see?',
|
233 |
+
(df_f1_con_circuits)
|
234 |
+
, index = idx)
|
235 |
+
|
236 |
+
detail = continent
|
237 |
+
|
238 |
+
st.sidebar.write('You selected:', detail)
|
239 |
+
|
240 |
+
elif map_type == 'Country':
|
241 |
+
|
242 |
+
idx = int(np.where(df_f1_cou_circuits == "France")[0][0])
|
243 |
+
|
244 |
+
country = st.sidebar.selectbox(
|
245 |
+
'Which country would you like to see?',
|
246 |
+
(df_f1_cou_circuits)
|
247 |
+
, index = idx)
|
248 |
+
|
249 |
+
detail = country
|
250 |
+
|
251 |
+
st.sidebar.write('You selected:', detail)
|
252 |
+
|
253 |
+
# Filter Data based on Map Type
|
254 |
+
filter_data(map_type, detail)
|
255 |
+
|
256 |
+
# Draw the 'World' folium Map
|
257 |
+
# draw_folium_map(map_type)
|
258 |
+
|
259 |
+
st.title('Plotting F1 Circuit Locations into a map using Folium')
|
260 |
+
|
261 |
+
if map_type == 'World':
|
262 |
+
|
263 |
+
st.subheader('Map of the ' + map_type )
|
264 |
+
|
265 |
+
else:
|
266 |
+
|
267 |
+
st.subheader('Map of ' + map_type + ' ' + detail)
|
268 |
+
|
269 |
+
|
270 |
+
# Render Folium Map in Streamlit
|
271 |
+
st_data = st_folium(CircuitsMap, width = 1250)
|