Created
June 6, 2014 15:58
-
-
Save kelvinn/7cd46f43183e19b43b5e to your computer and use it in GitHub Desktop.
An example usage of the googlemaps package to lookup origin / destination information using Google Maps API.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# You need to do a "pip install googlemaps" before running this. | |
# The input CSV file has an origin in Column A, and desinations in Column B | |
import csv | |
from googlemaps import GoogleMaps | |
from time import sleep | |
gmaps = GoogleMaps("your-super-long-API-key-from-Googles-API-Console") | |
stationReader = csv.reader(open('StationList.csv', 'rb'), delimiter=',') | |
timeWriter = csv.writer(open('Output.csv', 'wb'), delimiter=',', quotechar='|') | |
sta_from_list = [] | |
sta_to_list = [] | |
for row in stationReader: | |
if len(row[0]) > 3: | |
sta_from_list.append(row[0]) | |
if len(row[1]) > 3: | |
sta_to_list.append(row[1]) | |
timeWriter.writerow(["Station From", "Station To", "Meters", "Time in Sec", "Human Time"]) | |
for sta_from in sta_from_list: | |
for sta_to in sta_to_list: | |
sleep(1) | |
try: | |
directions = gmaps.directions(sta_from+", New South Wales, Australia", sta_to+", New South Wales, Australia") | |
meters = directions['Directions']['Distance']['meters'] | |
secs = directions['Directions']['Duration']['seconds'] | |
human_time = directions['Directions']['Duration']['html'] | |
if directions['Status']['code'] == 200: | |
timeWriter.writerow([sta_from, sta_to, meters, secs, human_time]) | |
else: | |
print "Lookup from %s to %s succeeded, but no results found." % (sta_from, sta_to) | |
except: | |
print "Error from %s to %s" % (sta_from, sta_to) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sydenham Station | Marrickville Station | |
---|---|---|
Dulwich Hill Station | ||
Hurlstone Park Station | ||
Canterbury Station | ||
Campsie Station | ||
Bankstown Station | ||
Berala Station | ||
Sefton Station | ||
Leightonfield Station | ||
Ashfield Station | ||
Homebush Station | ||
Flemington Station | ||
Lidcombe Station | ||
Granville Station | ||
Clyde Station | ||
Auburn Station | ||
Merrylands Station | ||
Yennora Station |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
hello
I'm interested in your application. Will you explain to me why when I exécutele script as I cca error "error from .. to .."
Thank you for your understanding
PS: I put my google key