mattritchey
commited on
Commit
•
81e5514
1
Parent(s):
7d56d7f
Upload 2 files
Browse files- app.py +182 -0
- requirements.txt +9 -0
app.py
ADDED
@@ -0,0 +1,182 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# -*- coding: utf-8 -*-
|
2 |
+
"""
|
3 |
+
Created on Thu Jun 8 03:39:02 2023
|
4 |
+
|
5 |
+
@author: mritchey
|
6 |
+
"""
|
7 |
+
# streamlit run "hail all.py"
|
8 |
+
|
9 |
+
import pandas as pd
|
10 |
+
import numpy as np
|
11 |
+
import streamlit as st
|
12 |
+
from geopy.extra.rate_limiter import RateLimiter
|
13 |
+
from geopy.geocoders import Nominatim
|
14 |
+
import folium
|
15 |
+
from streamlit_folium import st_folium
|
16 |
+
from vincenty import vincenty
|
17 |
+
import duckdb
|
18 |
+
import os
|
19 |
+
import requests
|
20 |
+
import urllib
|
21 |
+
|
22 |
+
geocode_key=os.environ["geocode_key"]
|
23 |
+
st.set_page_config(layout="wide")
|
24 |
+
|
25 |
+
|
26 |
+
@st.cache_data
|
27 |
+
def convert_df(df):
|
28 |
+
return df.to_csv(index=0).encode('utf-8')
|
29 |
+
|
30 |
+
|
31 |
+
def duck_sql(sql_code):
|
32 |
+
con = duckdb.connect()
|
33 |
+
con.execute("PRAGMA threads=2")
|
34 |
+
con.execute("PRAGMA enable_object_cache")
|
35 |
+
return con.execute(sql_code).df()
|
36 |
+
|
37 |
+
|
38 |
+
def get_data(lat, lon, date_str):
|
39 |
+
code = f"""
|
40 |
+
select "#ZTIME" as "Date_utc", LON, LAT, MAXSIZE
|
41 |
+
from
|
42 |
+
'data/*.parquet'
|
43 |
+
where LAT<={lat}+1 and LAT>={lat}-1
|
44 |
+
and LON<={lon}+1 and LON>={lon}-1
|
45 |
+
and "#ZTIME"<={date_str}
|
46 |
+
|
47 |
+
"""
|
48 |
+
return duck_sql(code)
|
49 |
+
|
50 |
+
|
51 |
+
def map_location(address, lat, lon):
|
52 |
+
|
53 |
+
m = folium.Map(location=[lat, lon],
|
54 |
+
|
55 |
+
zoom_start=9,
|
56 |
+
height=400)
|
57 |
+
folium.Marker(
|
58 |
+
location=[lat, lon],
|
59 |
+
tooltip=f'Address: {address}',
|
60 |
+
).add_to(m)
|
61 |
+
|
62 |
+
return m
|
63 |
+
|
64 |
+
|
65 |
+
def distance(x):
|
66 |
+
left_coords = (x[0], x[1])
|
67 |
+
right_coords = (x[2], x[3])
|
68 |
+
return vincenty(left_coords, right_coords, miles=True)
|
69 |
+
|
70 |
+
|
71 |
+
def geocode(address):
|
72 |
+
try:
|
73 |
+
try:
|
74 |
+
address2 = address.replace(' ', '+').replace(',', '%2C')
|
75 |
+
df = pd.read_json(
|
76 |
+
f'https://geocoding.geo.census.gov/geocoder/locations/onelineaddress?address={address2}&benchmark=2020&format=json')
|
77 |
+
results = df.iloc[:1, 0][0][0]['coordinates']
|
78 |
+
lat, lon = results['y'], results['x']
|
79 |
+
except:
|
80 |
+
geolocator = Nominatim(user_agent="GTA Lookup")
|
81 |
+
geocode = RateLimiter(geolocator.geocode, min_delay_seconds=1)
|
82 |
+
location = geolocator.geocode(address)
|
83 |
+
lat, lon = location.latitude, location.longitude
|
84 |
+
except:
|
85 |
+
try:
|
86 |
+
address = urllib.parse.quote(address)
|
87 |
+
url = 'https://api.geocod.io/v1.7/geocode?q=+'+address+f'&api_key={geocode_key}'
|
88 |
+
json_reponse=requests.get(url,verify=False).json()
|
89 |
+
lat,lon = json_reponse['results'][0]['location'].values()
|
90 |
+
except:
|
91 |
+
st.header("Sorry...Did not Find Address. Try to Correct with Google or just use City, State & Zip.")
|
92 |
+
st.header("")
|
93 |
+
st.header("")
|
94 |
+
return lat, lon
|
95 |
+
|
96 |
+
|
97 |
+
|
98 |
+
#Side Bar
|
99 |
+
address = st.sidebar.text_input("Address", "Dallas, TX")
|
100 |
+
date = st.sidebar.date_input("Loss Date (Max)", pd.Timestamp(2024, 11, 20), key='date') # change here
|
101 |
+
show_data = st.sidebar.selectbox('Show Data At Least Within:', ('Show All', '1 Mile', '3 Miles', '5 Miles'))
|
102 |
+
|
103 |
+
#Geocode Addreses
|
104 |
+
date_str=date.strftime("%Y%m%d")
|
105 |
+
|
106 |
+
lat, lon = geocode(address)
|
107 |
+
|
108 |
+
#Filter Data
|
109 |
+
df_hail_cut = get_data(lat,lon, date_str)
|
110 |
+
|
111 |
+
|
112 |
+
df_hail_cut["Lat_address"] = lat
|
113 |
+
df_hail_cut["Lon_address"] = lon
|
114 |
+
df_hail_cut['Miles to Hail'] = [
|
115 |
+
distance(i) for i in df_hail_cut[['LAT', 'LON', 'Lat_address', 'Lon_address']].values]
|
116 |
+
df_hail_cut['MAXSIZE'] = df_hail_cut['MAXSIZE'].round(2)
|
117 |
+
|
118 |
+
df_hail_cut = df_hail_cut.query("`Miles to Hail`<10")
|
119 |
+
df_hail_cut['Category'] = np.where(df_hail_cut['Miles to Hail'] < 1, "Within 1 Mile",
|
120 |
+
np.where(df_hail_cut['Miles to Hail'] < 3, "Within 3 Miles",
|
121 |
+
np.where( df_hail_cut['Miles to Hail'] < 5, "Within 5 Miles",
|
122 |
+
np.where(df_hail_cut['Miles to Hail'] < 10, "Within 10 Miles", 'Other'))))
|
123 |
+
|
124 |
+
df_hail_cut_group = pd.pivot_table(df_hail_cut, index='Date_utc',
|
125 |
+
columns='Category',
|
126 |
+
values='MAXSIZE',
|
127 |
+
aggfunc='max')
|
128 |
+
|
129 |
+
cols = df_hail_cut_group.columns
|
130 |
+
cols_focus = [ "Within 1 Mile","Within 3 Miles",
|
131 |
+
"Within 5 Miles", "Within 10 Miles"]
|
132 |
+
|
133 |
+
missing_cols = set(cols_focus)-set(cols)
|
134 |
+
for c in missing_cols:
|
135 |
+
df_hail_cut_group[c] = np.nan
|
136 |
+
|
137 |
+
#Filter
|
138 |
+
df_hail_cut_group2 = df_hail_cut_group[cols_focus]
|
139 |
+
|
140 |
+
if show_data=='Show All':
|
141 |
+
pass
|
142 |
+
else:
|
143 |
+
df_hail_cut_group2 = df_hail_cut_group2.query(
|
144 |
+
f"`Within {show_data}`==`Within {show_data}`")
|
145 |
+
|
146 |
+
for i in range(len(cols_focus)-1):
|
147 |
+
df_hail_cut_group2[cols_focus[i+1]] = np.where(df_hail_cut_group2[cols_focus[i+1]].fillna(0) <
|
148 |
+
df_hail_cut_group2[cols_focus[i]].fillna(0),
|
149 |
+
df_hail_cut_group2[cols_focus[i]],
|
150 |
+
df_hail_cut_group2[cols_focus[i+1]])
|
151 |
+
|
152 |
+
|
153 |
+
df_hail_cut_group2 = df_hail_cut_group2.sort_index(ascending=False)
|
154 |
+
|
155 |
+
df_hail_cut_group2.index=pd.to_datetime(df_hail_cut_group2.index,format='%Y%m%d')
|
156 |
+
df_hail_cut_group2.index=df_hail_cut_group2.index.strftime("%Y-%m-%d")
|
157 |
+
|
158 |
+
|
159 |
+
#Map Data
|
160 |
+
m = map_location(address, lat, lon)
|
161 |
+
|
162 |
+
#Display
|
163 |
+
col1, col2 = st.columns((3, 2))
|
164 |
+
|
165 |
+
with col1:
|
166 |
+
st.header('Estimated Maximum Hail Size')
|
167 |
+
st.write('Data from 2010 to 2024-11-20') # change here
|
168 |
+
df_hail_cut_group2
|
169 |
+
|
170 |
+
data=df_hail_cut_group2.reset_index()
|
171 |
+
data['Address']=''
|
172 |
+
data.loc[0,'Address']=address
|
173 |
+
csv2 = convert_df(data)
|
174 |
+
|
175 |
+
st.download_button(
|
176 |
+
label="Download data as CSV",
|
177 |
+
data=csv2,
|
178 |
+
file_name=f'{address}_{date_str}.csv',
|
179 |
+
mime='text/csv')
|
180 |
+
with col2:
|
181 |
+
st.header('Map')
|
182 |
+
st_folium(m, height=400)
|
requirements.txt
ADDED
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
folium
|
2 |
+
geopy
|
3 |
+
numpy
|
4 |
+
pandas
|
5 |
+
streamlit
|
6 |
+
streamlit_folium
|
7 |
+
vincenty
|
8 |
+
duckdb
|
9 |
+
requests
|