use googles distance matrix api to find travel times for all destinations from origins
I built this app to choose hotels when traveling to cities for conferences or weddings
# Crosscompute
origins_text_path = 'origin.txt'
destinations_text_path = 'dest.txt'
mode_select = '''
driving
driving
walking
cycling
'''
target_folder = 'results'
def load_unique_lines(source_path):
if not source_path:
return []
with open(source_path, 'r') as f:
lines = set((x.strip('\n ,;') for x in f))
return sorted(filter(lambda x: x, lines))
from os.path import join
import geopy
import numpy as np
import requests
from pandas import DataFrame
from itertools import takewhile
origins = load_unique_lines(origins_text_path)
destinations = load_unique_lines(destinations_text_path)
mode = next(takewhile(lambda x: x.strip(), mode_select.strip().splitlines())).strip()
# Use google's distancematrix api
url = "https://maps.googleapis.com/maps/api/distancematrix/json?"
url_params = {"origins": "|".join(origins),
"mode": mode,
"destinations": "|".join(destinations),
"language": "en-EN",
"units": "imperial",
"key": 'AIzaSyBhNXrJJKvAj6-h5ceUe769JM-u4olg6Jo'}
response = requests.get(url, params=url_params).json()
if response['status'] != 'OK':
raise Exception('api error :/, check addresses')
origins, destinations = (response['origin_addresses'],
response['destination_addresses'])
d = [[i['duration']['value'] for i in x['elements']] for x in response['rows']]
d
df = DataFrame(d, columns=destinations, index=origins)
df.index.name = 'origins'
df['total'] = df.sum(axis=1)
df = df.sort_values(by='total')
df
results_path = join(target_folder, 'results.csv')
df.to_csv(results_path)
print("results_table_path = {0}".format(results_path))
google_geo = geopy.GoogleV3()
minimum_pixel_size, max_pixel_size = 10, 50
origins_color = 'red'
destinations_color = 'blue'
destinations_pixel_size = 10
sizes = np.linspace(minimum_pixel_size,
max_pixel_size,
len(df.index))[::-1]
sizes = np.append(sizes, np.full(len(destinations), destinations_pixel_size))
sizes
geotable = DataFrame(index=np.append(df.index, (destinations)))
geotable.index.name = 'address'
geotable['radius_in_pixels'] = sizes
geotable['FillColor'] = [origins_color] * len(origins) + [destinations_color] * len(destinations)
geotable.head()
# geocode address
from geopy.geocoders.googlev3 import GeocoderQuotaExceeded
from time import sleep
def get_coordinates(x):
for i in range(3):
try:
c = google_geo.geocode(x)
return [c.latitude, c.longitude]
except GeocoderQuotaExceeded:
print('geocoder quota exceeded, trying again')
sleep(3)
continue
else:
print('geocoder.error = GeocoderQuotaExceeded')
raise GeocoderQuotaExceeded
geotable['coord'] = geotable.index.map(get_coordinates)
geotable['latitude'] = geotable['coord'].apply(lambda x: x[0])
geotable['longitude'] = geotable['coord'].apply(lambda x: x[1])
del geotable['coord']
geotable
geomap_path = join(target_folder, 'geomap.csv')
geotable.to_csv(geomap_path)
print("points_geotable_path = {0}".format(geomap_path))