Skip to content

Instantly share code, notes, and snippets.

@kelvinn
Created June 6, 2014 15:58
Show Gist options
  • Save kelvinn/7cd46f43183e19b43b5e to your computer and use it in GitHub Desktop.
Save kelvinn/7cd46f43183e19b43b5e to your computer and use it in GitHub Desktop.

Revisions

  1. kelvinn created this gist Jun 6, 2014.
    18 changes: 18 additions & 0 deletions StationList.csv
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,18 @@
    "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"
    37 changes: 37 additions & 0 deletions calc_travel.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,37 @@
    # 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)