Prepare for Hurricane Elsa 20210706-1700




Pay Notebook Creator: Roy Hyunjin Han0
Set Container: Numerical CPU with TINY Memory for 10 Minutes 0
Total0

Prepare for Hurricane Elsa 20210706-1830

To see where you can pick up sandbags to prepare for Hurricane Elsa in the Tampa Bay area, enter your address and click Run.

{my_address : Your Address including Zip Code ? Enter your street address}

{resources_select : Desired Resources ? Choose resources or clear to see all locations}

This runnable report was last updated on Tuesday, July 6, 2021 at 6:30pm. Please check https://twitter.com/crosscompute for the latest version of this runnable report.

Thank you to Kashfi Fahim for collaboration on this project.

In [1]:
# CrossCompute
my_address = '687 Central Ave N, St. Petersburg, FL 33701'
resources_select_path = 'resources.txt'
target_folder = '/tmp'
In [2]:
resources_text = open(resources_select_path, 'rt').read()
try:
    resources = resources_text.splitlines()[0].split()
except IndexError:
    resources = []
resources
Out[2]:
[]
In [3]:
GOOGLE_KEY = 'AIzaSyDqR8ePmVqzh-wF3R89tsZyhCZjaTI_mFI'
In [4]:
from geopy.geocoders import GoogleV3
geocode = GoogleV3(api_key=GOOGLE_KEY).geocode
In [5]:
my_location = geocode(my_address)
my_longitude = my_location.longitude
my_latitude = my_location.latitude
In [6]:
import pandas as pd
normalized_table_path = 'normalized-locations-20210706-1630.csv'
normalized_table = pd.read_csv(normalized_table_path)
In [7]:
from geopy.distance import distance

def get_distance(row):
    my_distance = distance((my_latitude, my_longitude), (row['Latitude'], row['Longitude']))
    row['Flying Distance in Miles'] = my_distance.miles
    row['Flying Distance in Meters'] = my_distance.m
    return row

distance_table = normalized_table.apply(get_distance, axis=1)
In [8]:
t = distance_table.sort_values('Flying Distance in Meters')

# TODO: Try reducing multiple conditions instead of hardcoding

if 'Sandbags' in resources:
    t = t.dropna(subset=['Resources'])
    t = t[t['Resources'].str.contains('Sandbags')]
    
t[:3]
Out[8]:
<style scoped> .dataframe tbody tr th:only-of-type { vertical-align: middle; } .dataframe tbody tr th { vertical-align: top; } .dataframe thead th { text-align: right; } </style>
Location Name Normalized Address Location County Open Close Resources Type Source Organization Name Longitude Latitude Flying Distance in Miles Flying Distance in Meters
2 Year-Round Self-Service Sandbag Site 1744 9th Avenue N, St. Petersburg, FL 33713 Pinellas County, FL NaN NaN NaN NaN https://www.wfla.com/news/local-news/tropical-... City of St. Petersburg, FL -82.656868 27.780487 1.033477 1663.220693
4 James "JC" Turner Fields > South Side of Bartl... 643 22nd Avenue S, St. Petersburg, FL 33705 Pinellas County, FL 20210708-0900 NaN Sandbags NaN https://www.wfla.com/news/local-news/tropical-... City of St. Petersburg, FL -82.641473 27.748947 1.553084 2499.446204
3 Northeast Park > Cardinal Drive Entrance to Ma... 875 62nd Avenue NE, St. Petersburg, FL 33702 Pinellas County, FL 20210708-0900 NaN Sandbags NaN https://www.wfla.com/news/local-news/tropical-... City of St. Petersburg, FL -82.623047 27.830147 4.230553 6808.415631
In [9]:
destination_addresses = list(t[:3]['Normalized Address'])
In [10]:
base_url = 'https://maps.googleapis.com/maps/api/distancematrix/json'
d = {
    'origins': '|'.join([my_address]),
    'destinations': '|'.join(destination_addresses),
    'units': 'imperial',
    'key': GOOGLE_KEY,
}
url = base_url + '?' + '&'.join(f'{k}={v}' for k, v in d.items())
url
Out[10]:
'https://maps.googleapis.com/maps/api/distancematrix/json?origins=687 Central Ave N, St. Petersburg, FL 33701&destinations=1744 9th Avenue N, St. Petersburg, FL 33713|643 22nd Avenue S, St. Petersburg, FL 33705|875 62nd Avenue NE, St. Petersburg, FL 33702&units=imperial&key=AIzaSyDqR8ePmVqzh-wF3R89tsZyhCZjaTI_mFI'
In [11]:
import requests
response = requests.get(url)
response_json = response.json()
In [12]:
distance_texts = []
duration_texts = []
for response_index, response_element in enumerate(response_json['rows'][0]['elements']):
    distance_text = response_element['distance']['text']
    duration_text = response_element['duration']['text']
    distance_texts.append(distance_text)
    duration_texts.append(duration_text)    
In [13]:
selected_t = t[:3].copy()
In [14]:
selected_t['Driving Distance'] = distance_texts
selected_t['Driving Time'] = duration_texts
In [15]:
from os.path import join
In [16]:
selected_locations_table_path = join(target_folder, 'selected-locations.csv')
selected_t[[
    'Driving Time',
    'Driving Distance',
    'Location Name',
    'Normalized Address',
    'Open',
    'Close',
    'Resources',
    'Type',
    'Location County',
    'Organization Name',
    'Source',
]].to_csv(selected_locations_table_path, index=False)
print('selected_locations_table_path = ' + selected_locations_table_path)
selected_locations_table_path = /tmp/selected-locations.csv
In [17]:
map_geotable_path = join(target_folder, 'map.csv')
map_table = t[[
    'Location Name',
    'Normalized Address',
    'Open',
    'Close',
    'Latitude',
    'Longitude',
]][:3].copy()
map_table['FillColor'] = 'blue'
map_table = map_table.append({
    'Location Name': 'Your Address',
    'Latitude': my_latitude,
    'Longitude': my_longitude,
    'FillColor': 'red',
}, ignore_index=True)
map_table;
In [18]:
map_table.to_csv(map_geotable_path, index=False)
print('map_geotable_path = ' + map_geotable_path)
map_geotable_path = /tmp/map.csv
In [19]:
destination_addresses
Out[19]:
['643 22nd Avenue S, St. Petersburg, FL 33705',
 '875 62nd Avenue NE, St. Petersburg, FL 33702',
 '2331 60th Street N, St. Petersburg, FL 33710']
In [20]:
url_template = 'https://www.google.com/maps/dir/?api=1&destination=AAA&travelmode=driving'
directions_url = url_template.replace('AAA', destination_addresses[0].replace(' ', '%20'))
In [22]:
print('location_count = ' + str(len(t)))
print('directions_url = ' + directions_url)
location_count = 25
directions_url = https://www.google.com/maps/dir/?api=1&destination=643%2022nd%20Avenue%20S,%20St.%20Petersburg,%20FL%2033705&travelmode=driving

Selected Resource Locations

Here are the locations where you can get the resources you selected.

  • Please call the location to confirm as schedules may vary.
  • Bring proof of residency.
  • You may need to bring your own shovel.

{directions_url : Directions to Nearest Location ? Copy and Paste URL to Get Driving Directions Using Google Maps}

{selected_locations_table : Selected Locations ? Top 3 Nearby Locations }

{map_geotable : Map ? Map of Locations}

{location_count : Total # of Resource Locations ? Total Number of Locations with Matching Resources}