mattritchey commited on
Commit
21fafcc
1 Parent(s): 807eb34

Upload 3 files

Browse files
app.py ADDED
@@ -0,0 +1,132 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -*- coding: utf-8 -*-
2
+ """
3
+ Created on Thu Jun 8 03:39:02 2023
4
+
5
+ @author: mritchey
6
+ """
7
+
8
+ import pandas as pd
9
+ import numpy as np
10
+ import streamlit as st
11
+ from geopy.extra.rate_limiter import RateLimiter
12
+ from geopy.geocoders import Nominatim
13
+ import folium
14
+ from streamlit_folium import st_folium
15
+ from vincenty import vincenty
16
+
17
+ st.set_page_config(layout="wide")
18
+
19
+ @st.cache_data
20
+ def convert_df(df):
21
+ return df.to_csv(index=0).encode('utf-8')
22
+
23
+ @st.cache_data
24
+ def get_data(file='hail2010-20230920_significant_bulk_all.parquet'):
25
+ return pd.read_parquet(file)
26
+
27
+
28
+ def map_perimeters(address,lat ,lon):
29
+
30
+ m = folium.Map(location=[lat, lon],
31
+
32
+ zoom_start=6,
33
+ height=400)
34
+ folium.Marker(
35
+ location=[lat, lon],
36
+ tooltip=f'Address: {address}',
37
+ ).add_to(m)
38
+
39
+ return m
40
+
41
+ def distance(x):
42
+ left_coords = (x[0], x[1])
43
+ right_coords = (x[2], x[3])
44
+ return vincenty(left_coords, right_coords, miles=True)
45
+
46
+ def geocode(address):
47
+ try:
48
+ address2 = address.replace(' ', '+').replace(',', '%2C')
49
+ df = pd.read_json(
50
+ f'https://geocoding.geo.census.gov/geocoder/locations/onelineaddress?address={address2}&benchmark=2020&format=json')
51
+ results = df.iloc[:1, 0][0][0]['coordinates']
52
+ lat, lon = results['y'], results['x']
53
+ except:
54
+ geolocator = Nominatim(user_agent="GTA Lookup")
55
+ geocode = RateLimiter(geolocator.geocode, min_delay_seconds=1)
56
+ location = geolocator.geocode(address)
57
+ lat, lon = location.latitude, location.longitude
58
+ return lat, lon
59
+
60
+
61
+
62
+
63
+
64
+ #Side Bar
65
+ address = st.sidebar.text_input(
66
+ "Address", "Dallas, TX")
67
+ date = st.sidebar.date_input("Loss Date", pd.Timestamp(2021, 7, 14), key='date')
68
+ df_hail=get_data()
69
+
70
+
71
+ #Geocode Addreses
72
+ lat, lon = geocode(address)
73
+
74
+ #Filter DAta
75
+ df_hail_cut=df_hail.query(f"{lat}-1<=LAT<={lat}+1 and {lon}-1<=LON<={lon}+1 ")
76
+ df_hail_cut=df_hail_cut.query("Date_est<=@date")
77
+
78
+
79
+ df_hail_cut["Lat_address"] = lat
80
+ df_hail_cut["Lon_address"] = lon
81
+ df_hail_cut['Miles to Hail'] = [
82
+ distance(i) for i in df_hail_cut[['LAT','LON','Lat_address','Lon_address']].values]
83
+ df_hail_cut['MAXSIZE'] = df_hail_cut['MAXSIZE'].round(2)
84
+
85
+ df_hail_cut=df_hail_cut.query("`Miles to Hail`<10")
86
+ df_hail_cut['Category']=np.where(df_hail_cut['Miles to Hail']<.1,"At Location",
87
+ np.where(df_hail_cut['Miles to Hail']<1,"Within 1 Mile",
88
+ np.where(df_hail_cut['Miles to Hail']<3,"Within 3 Miles",
89
+ np.where(df_hail_cut['Miles to Hail']<10,"Within 10 Miles",'Other'))))
90
+
91
+ df_hail_cut_group=pd.pivot_table(df_hail_cut,index='Date_est',
92
+ columns='Category',
93
+ values='MAXSIZE',
94
+ aggfunc='max')
95
+
96
+ cols=df_hail_cut_group.columns
97
+ cols_focus=['At Location',"Within 1 Mile","Within 3 Miles","Within 10 Miles"]
98
+
99
+ missing_cols=set(cols_focus)-set(cols)
100
+ for c in missing_cols:
101
+ df_hail_cut_group[c]=np.nan
102
+
103
+ df_hail_cut_group2=df_hail_cut_group[cols_focus]
104
+
105
+ for i in range(3):
106
+ df_hail_cut_group2[cols_focus[i+1]]=np.where(df_hail_cut_group2[cols_focus[i+1]]<
107
+ df_hail_cut_group2[cols_focus[i]],
108
+ df_hail_cut_group2[cols_focus[i]],
109
+ df_hail_cut_group2[cols_focus[i+1]])
110
+
111
+
112
+ df_hail_cut_group2=df_hail_cut_group2.sort_index(ascending=False)
113
+
114
+ #Map Data
115
+ m = map_perimeters(address,lat, lon)
116
+
117
+ #Display
118
+ col1, col2 = st.columns((3, 2))
119
+ with col1:
120
+ st.header('Estimated Maximum Hail Size')
121
+ st.write('Data from 2010 to 2023-09-20')
122
+ df_hail_cut_group2
123
+ csv2 = convert_df(df_hail_cut_group2.reset_index())
124
+ st.download_button(
125
+ label="Download data as CSV",
126
+ data=csv2,
127
+ file_name=f'{address}_{date}.csv',
128
+ mime='text/csv')
129
+ with col2:
130
+
131
+ st.header('Map')
132
+ st_folium(m, height=400)
hail2010-20230920_significant_bulk_all.parquet ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:86c86463c9f9180d9fff30973d31228b2f96bec03f460da9caaa7645fe00831b
3
+ size 75828421
requirements.txt ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ folium==0.12.1.post1
2
+ geopy==2.2.0
3
+ numpy==1.19.5
4
+ pandas==1.5.2
5
+ streamlit==1.20.0
6
+ streamlit_folium==0.6.15
7
+ vincenty==0.1.4