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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
"""
|
| 3 |
+
Created on Thu Jun 8 03:39:02 2023
|
| 4 |
+
|
| 5 |
+
@author: mritchey
|
| 6 |
+
"""
|
| 7 |
+
# streamlit run "C:\Users\mritchey\.spyder-py3\Python Scripts\streamlit projects\hail\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 |
+
|
| 19 |
+
st.set_page_config(layout="wide")
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
@st.cache_data
|
| 23 |
+
def convert_df(df):
|
| 24 |
+
return df.to_csv(index=0).encode('utf-8')
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
def duck_sql(sql_code):
|
| 28 |
+
con = duckdb.connect()
|
| 29 |
+
con.execute("PRAGMA threads=2")
|
| 30 |
+
con.execute("PRAGMA enable_object_cache")
|
| 31 |
+
return con.execute(sql_code).df()
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
def get_data(lat, lon, date_str):
|
| 35 |
+
code = f"""
|
| 36 |
+
select "#ZTIME" as "Date_utc", LON, LAT, MAXSIZE
|
| 37 |
+
from
|
| 38 |
+
'data/*.parquet'
|
| 39 |
+
where LAT<={lat}+1 and LAT>={lat}-1
|
| 40 |
+
and LON<={lon}+1 and LON>={lon}-1
|
| 41 |
+
and "#ZTIME"<={date_str}
|
| 42 |
+
|
| 43 |
+
"""
|
| 44 |
+
duck_sql(code)
|
| 45 |
+
return duck_sql(code)
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
def map_perimeters(address, lat, lon):
|
| 49 |
+
|
| 50 |
+
m = folium.Map(location=[lat, lon],
|
| 51 |
+
|
| 52 |
+
zoom_start=6,
|
| 53 |
+
height=400)
|
| 54 |
+
folium.Marker(
|
| 55 |
+
location=[lat, lon],
|
| 56 |
+
tooltip=f'Address: {address}',
|
| 57 |
+
).add_to(m)
|
| 58 |
+
|
| 59 |
+
return m
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
def distance(x):
|
| 63 |
+
left_coords = (x[0], x[1])
|
| 64 |
+
right_coords = (x[2], x[3])
|
| 65 |
+
return vincenty(left_coords, right_coords, miles=True)
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
def geocode(address):
|
| 69 |
+
try:
|
| 70 |
+
address2 = address.replace(' ', '+').replace(',', '%2C')
|
| 71 |
+
df = pd.read_json(
|
| 72 |
+
f'https://geocoding.geo.census.gov/geocoder/locations/onelineaddress?address={address2}&benchmark=2020&format=json')
|
| 73 |
+
results = df.iloc[:1, 0][0][0]['coordinates']
|
| 74 |
+
lat, lon = results['y'], results['x']
|
| 75 |
+
except:
|
| 76 |
+
geolocator = Nominatim(user_agent="GTA Lookup")
|
| 77 |
+
geocode = RateLimiter(geolocator.geocode, min_delay_seconds=1)
|
| 78 |
+
location = geolocator.geocode(address)
|
| 79 |
+
lat, lon = location.latitude, location.longitude
|
| 80 |
+
return lat, lon
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
#Side Bar
|
| 84 |
+
address = st.sidebar.text_input(
|
| 85 |
+
"Address", "Dallas, TX")
|
| 86 |
+
date = st.sidebar.date_input(
|
| 87 |
+
"Loss Date", pd.Timestamp(2023, 7, 14), key='date')
|
| 88 |
+
date_str = date.strftime("%Y%m%d")
|
| 89 |
+
|
| 90 |
+
#Geocode Addreses
|
| 91 |
+
lat, lon = geocode(address)
|
| 92 |
+
|
| 93 |
+
#Filter Data
|
| 94 |
+
df_hail_cut = get_data(lat, lon, date_str)
|
| 95 |
+
|
| 96 |
+
|
| 97 |
+
df_hail_cut["Lat_address"] = lat
|
| 98 |
+
df_hail_cut["Lon_address"] = lon
|
| 99 |
+
df_hail_cut['Miles to Hail'] = [
|
| 100 |
+
distance(i) for i in df_hail_cut[['LAT', 'LON', 'Lat_address', 'Lon_address']].values]
|
| 101 |
+
df_hail_cut['MAXSIZE'] = df_hail_cut['MAXSIZE'].round(1)
|
| 102 |
+
|
| 103 |
+
df_hail_cut = df_hail_cut.query("`Miles to Hail`<10")
|
| 104 |
+
df_hail_cut['Category'] = np.where(df_hail_cut['Miles to Hail'] < .25, "At Location",
|
| 105 |
+
np.where(df_hail_cut['Miles to Hail'] < 1, "Within 1 Mile",
|
| 106 |
+
np.where(df_hail_cut['Miles to Hail'] < 3, "Within 3 Miles",
|
| 107 |
+
np.where(df_hail_cut['Miles to Hail'] < 10, "Within 10 Miles", 'Other'))))
|
| 108 |
+
|
| 109 |
+
df_hail_cut_group = pd.pivot_table(df_hail_cut, index='Date_utc',
|
| 110 |
+
columns='Category',
|
| 111 |
+
values='MAXSIZE',
|
| 112 |
+
aggfunc='max')
|
| 113 |
+
|
| 114 |
+
cols = df_hail_cut_group.columns
|
| 115 |
+
cols_focus = ['At Location', "Within 1 Mile",
|
| 116 |
+
"Within 3 Miles", "Within 10 Miles"]
|
| 117 |
+
|
| 118 |
+
missing_cols = set(cols_focus)-set(cols)
|
| 119 |
+
for c in missing_cols:
|
| 120 |
+
df_hail_cut_group[c] = np.nan
|
| 121 |
+
|
| 122 |
+
df_hail_cut_group2 = df_hail_cut_group[cols_focus].query(
|
| 123 |
+
"`Within 3 Miles`==`Within 3 Miles`")
|
| 124 |
+
|
| 125 |
+
for i in range(3):
|
| 126 |
+
df_hail_cut_group2[cols_focus[i+1]] = np.where(df_hail_cut_group2[cols_focus[i+1]].fillna(0) <
|
| 127 |
+
df_hail_cut_group2[cols_focus[i]].fillna(
|
| 128 |
+
0),
|
| 129 |
+
df_hail_cut_group2[cols_focus[i]],
|
| 130 |
+
df_hail_cut_group2[cols_focus[i+1]])
|
| 131 |
+
|
| 132 |
+
|
| 133 |
+
df_hail_cut_group2 = df_hail_cut_group2.sort_index(ascending=False)
|
| 134 |
+
|
| 135 |
+
df_hail_cut_group2.index = pd.to_datetime(
|
| 136 |
+
df_hail_cut_group2.index, format='%Y%m%d').strftime("%Y-%m-%d")
|
| 137 |
+
|
| 138 |
+
|
| 139 |
+
#Map Data
|
| 140 |
+
m = map_perimeters(address, lat, lon)
|
| 141 |
+
|
| 142 |
+
#Display
|
| 143 |
+
col1, col2 = st.columns((3, 2))
|
| 144 |
+
|
| 145 |
+
with col1:
|
| 146 |
+
st.header('Estimated Maximum Hail Size')
|
| 147 |
+
st.write('Data from 2010 to 2023-09-24')
|
| 148 |
+
df_hail_cut_group2
|
| 149 |
+
csv2 = convert_df(df_hail_cut_group2.reset_index())
|
| 150 |
+
st.download_button(
|
| 151 |
+
label="Download data as CSV",
|
| 152 |
+
data=csv2,
|
| 153 |
+
file_name=f'{address}_{date_str}.csv',
|
| 154 |
+
mime='text/csv')
|
| 155 |
+
with col2:
|
| 156 |
+
st.header('Map')
|
| 157 |
+
st_folium(m, height=400)
|
requirements.txt
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
folium
|
| 2 |
+
geopy
|
| 3 |
+
numpy
|
| 4 |
+
pandas
|
| 5 |
+
streamlit
|
| 6 |
+
streamlit_folium
|
| 7 |
+
vincenty
|
| 8 |
+
duckdb
|