Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
@@ -7,6 +7,8 @@ import pickle
|
|
7 |
import rasterio
|
8 |
import h5py
|
9 |
from skimage.morphology import disk
|
|
|
|
|
10 |
|
11 |
app = FastAPI()
|
12 |
|
@@ -16,9 +18,24 @@ app = FastAPI()
|
|
16 |
@app.get("/")
|
17 |
def root():
|
18 |
return {"API": "Hail Docker Data"}
|
|
|
|
|
19 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
|
21 |
-
def get_hail_data(
|
22 |
|
23 |
start_date = pd.Timestamp(str(start_date)).strftime('%Y%m%d')
|
24 |
end_date = pd.Timestamp(str(end_date)).strftime('%Y%m%d')
|
@@ -28,9 +45,13 @@ def get_hail_data(lat, lon, start_date, end_date, radius_miles, get_max):
|
|
28 |
|
29 |
if len(years) == 0:
|
30 |
years = [pd.Timestamp(start_date).year]
|
31 |
-
|
|
|
|
|
|
|
32 |
# Convert Lat Lon to row & col on Array
|
33 |
transform = pickle.load(open('Data/transform_mrms.pkl', 'rb'))
|
|
|
34 |
row, col = rasterio.transform.rowcol(transform, lon, lat)
|
35 |
|
36 |
files = [
|
@@ -93,10 +114,10 @@ def get_hail_data(lat, lon, start_date, end_date, radius_miles, get_max):
|
|
93 |
|
94 |
|
95 |
@app.get('/Hail_Docker_Data')
|
96 |
-
async def predict(
|
97 |
|
98 |
try:
|
99 |
-
results = get_hail_data(
|
100 |
end_date, radius_miles, get_max)
|
101 |
except:
|
102 |
results = pd.DataFrame({'Date': ['error'], 'Hail_max': ['error']})
|
|
|
7 |
import rasterio
|
8 |
import h5py
|
9 |
from skimage.morphology import disk
|
10 |
+
from geopy.extra.rate_limiter import RateLimiter
|
11 |
+
from geopy.geocoders import Nominatim
|
12 |
|
13 |
app = FastAPI()
|
14 |
|
|
|
18 |
@app.get("/")
|
19 |
def root():
|
20 |
return {"API": "Hail Docker Data"}
|
21 |
+
|
22 |
+
def geocode_address(address):
|
23 |
|
24 |
+
try:
|
25 |
+
address2 = address.replace(' ', '+').replace(',', '%2C')
|
26 |
+
df = pd.read_json(
|
27 |
+
f'https://geocoding.geo.census.gov/geocoder/locations/onelineaddress?address={address2}&benchmark=2020&format=json')
|
28 |
+
results = df.iloc[:1, 0][0][0]['coordinates']
|
29 |
+
lat, lon = results['y'], results['x']
|
30 |
+
except:
|
31 |
+
geolocator = Nominatim(user_agent='GTA Lookup')
|
32 |
+
geocode = RateLimiter(geolocator.geocode, min_delay_seconds=2)
|
33 |
+
location = geolocator.geocode(address)
|
34 |
+
lat, lon = location.latitude, location.longitude
|
35 |
+
|
36 |
+
return lat, lon
|
37 |
|
38 |
+
def get_hail_data(address, start_date, end_date, radius_miles, get_max):
|
39 |
|
40 |
start_date = pd.Timestamp(str(start_date)).strftime('%Y%m%d')
|
41 |
end_date = pd.Timestamp(str(end_date)).strftime('%Y%m%d')
|
|
|
45 |
|
46 |
if len(years) == 0:
|
47 |
years = [pd.Timestamp(start_date).year]
|
48 |
+
|
49 |
+
# Geocode Address
|
50 |
+
lat, lon= geocode_address(address)
|
51 |
+
|
52 |
# Convert Lat Lon to row & col on Array
|
53 |
transform = pickle.load(open('Data/transform_mrms.pkl', 'rb'))
|
54 |
+
|
55 |
row, col = rasterio.transform.rowcol(transform, lon, lat)
|
56 |
|
57 |
files = [
|
|
|
114 |
|
115 |
|
116 |
@app.get('/Hail_Docker_Data')
|
117 |
+
async def predict(address: str, start_date: str, end_date: str, radius_miles: int, get_max: bool):
|
118 |
|
119 |
try:
|
120 |
+
results = get_hail_data(address, start_date,
|
121 |
end_date, radius_miles, get_max)
|
122 |
except:
|
123 |
results = pd.DataFrame({'Date': ['error'], 'Hail_max': ['error']})
|