Skip to content

Instantly share code, notes, and snippets.

@Big-al
Last active December 15, 2021 21:23
Show Gist options
  • Save Big-al/57b8bebc6d2f960fdf3a313573a71b91 to your computer and use it in GitHub Desktop.
Save Big-al/57b8bebc6d2f960fdf3a313573a71b91 to your computer and use it in GitHub Desktop.
Needed to get the distance between a few thousands of people and their workplace for an assignment. Turned out there was a number of overlaps, so this solution makes a naive improvement on a linear approach by using some simple memoization. Use this example as a helper on how the google maps location matrix api and csv file handling works in pyt…
import csv
from os import pathconf
import googlemaps # pip3 install googlemaps
print('Exepcting data in the following format (order is important):')
print('Some ID,Country of location 1,Zipcode of location 1,Zipcode of location 2;Country of location 2,distance')
print('Example line:')
print('id_log4j2,Denmark,8200,8250,Denmark, ')
api_key = input("Enter google maps api-key:")
input_file_name = input("Enter the input the complete file name (looks in current dir):")
# Google maps matrix API key
gmaps = googlemaps.Client(key=api_key)
# Do the stuff:
People = dict({})
api_calls = 0
with open('./' + input_file_name) as csv_file:
csv_reader = csv.reader(csv_file, delimiter=',')
lines = list(csv_reader)
line_count = 0
for row in lines:
if line_count == 0:
# Print and Skip header
print(f'Column names are {", ".join(row)}')
line_count += 1
else:
key = f'{row[2]}, {row[1]}'+';'+f'{row[3]}, {row[4]}'
if key in People.keys():
row[5] = People[key]
print(str(line_count).ljust(10) + ' | ' + 'Saved an API call!')
else:
startPosition = key.split(';')[0]
endPosition = key.split(';')[1]
# Get position
try:
distance = gmaps.distance_matrix(startPosition, endPosition)['rows'][0]['elements'][0]
api_calls += 1
# Save for retrieval
People[key] = distance.get("distance").get("text")
# Save in item list:
row[5] = distance.get("distance").get("text")
except:
print("An exception occurred with the following data: " + key + " on line: " + str(line_count))
row[5] = "N/A"
# Continue
print(str(line_count).ljust(10) + ' | ' + str(key).ljust(50) + " had distance: " + row[5])
line_count += 1
if line_count == lines.__len__():
break
# Make snarky comment
print('Saved you ' + str(line_count - api_calls) + ' calls to the api!')
# Save Changes to csv file
print('Writing result to file.')
writer = csv.writer(open('./' + 'edited_' + input_file_name, 'w'))
writer.writerows(lines)
# Finish
print(f'Processed {line_count} lines with {api_calls} api calls and cost 0.005$ / request = ' + str(api_calls*0.005) + '$')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment