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.
# CrossCompute
my_address = '687 Central Ave N, St. Petersburg, FL 33701'
resources_select_path = 'resources.txt'
target_folder = '/tmp'
resources_text = open(resources_select_path, 'rt').read()
try:
resources = resources_text.splitlines()[0].split()
except IndexError:
resources = []
resources
GOOGLE_KEY = 'AIzaSyDqR8ePmVqzh-wF3R89tsZyhCZjaTI_mFI'
from geopy.geocoders import GoogleV3
geocode = GoogleV3(api_key=GOOGLE_KEY).geocode
my_location = geocode(my_address)
my_longitude = my_location.longitude
my_latitude = my_location.latitude
import pandas as pd
normalized_table_path = 'normalized-locations-20210706-1630.csv'
normalized_table = pd.read_csv(normalized_table_path)
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)
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]
destination_addresses = list(t[:3]['Normalized Address'])
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
import requests
response = requests.get(url)
response_json = response.json()
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)
selected_t = t[:3].copy()
selected_t['Driving Distance'] = distance_texts
selected_t['Driving Time'] = duration_texts
from os.path import join
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)
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;
map_table.to_csv(map_geotable_path, index=False)
print('map_geotable_path = ' + map_geotable_path)
destination_addresses
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'))
print('location_count = ' + str(len(t)))
print('directions_url = ' + directions_url)
Here are the locations where you can get the resources you selected.
{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}