Created
May 14, 2015 12:21
-
-
Save evamvid/1b9483c993d52413ab7f to your computer and use it in GitHub Desktop.
Python Project Partially Done
This file contains hidden or 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
#initialize | |
import googlemaps | |
from datetime import datetime | |
import json | |
import csv | |
import os | |
import scriptpath | |
gmaps = googlemaps.Client(key='my_api_key') | |
os.chdir(os.path.dirname(os.path.realpath(sys.argv[0])))#redefines cwd to the script's home dir bc I'm running it in the IDE | |
#ask for start address, get it formatted correctly | |
while True: | |
try: | |
start=input('please enter start address') | |
geocode_result = gmaps.geocode(start) | |
x=json.dumps(geocode_result) | |
r=json.loads(x) | |
startformatted = r[0]['formatted_address'] | |
print(startformatted) #debugging | |
break | |
except IndexError: | |
print('sorry, invalid address. Please try again.') | |
#ask for destination address, get it formatted correctly | |
while True: | |
try: | |
destination=input('please enter destination address') #ask for destination address | |
geocode_result = gmaps.geocode(destination) #make the request for geocode | |
y=json.dumps(geocode_result) #format into json | |
s=json.loads(y) #load json object | |
destinationformatted = s[0]['formatted_address'] #formatted address to variable | |
print(destinationformatted) | |
break | |
except IndexError: | |
print('sorry, invalid address. Please try again.') | |
#get directions | |
while True: | |
try: | |
mode=input('please enter the preferred mode of travel for route calculation.Driving, walking, transit, or bicycling.') | |
directions = gmaps.directions(startformatted, destinationformatted, mode=mode) | |
break | |
except ValueError: | |
print('sorry, invalid mode of transportation. Please try again') | |
print(directions) #debugging | |
z=json.dumps(directions) | |
t=json.loads(z) | |
polyline=t[0]['overview_polyline']['points'] | |
print(polyline) #debugging | |
distance_matrix = gmaps.distance_matrix(startformatted, destinationformatted, mode=mode) | |
print(distance_matrix) #debugging | |
a=json.dumps(distance_matrix) | |
u=json.loads(a) | |
distance = u['rows'][0]['elements'][0]['distance']['value']#some funky data types going on! lists inside dictionaries and vice versa | |
print (distance) #distance in meters | |
while True: | |
try: | |
samples = input('at how many points would you like to sample elevation?') | |
samples = int(samples) | |
break | |
except ValueError: | |
print("That's not an int!") | |
elevations = gmaps.elevation_along_path(polyline, samples=samples) | |
print(elevations) | |
b=json.dumps(elevations) | |
v=json.loads(b) | |
print(v) | |
with open('elev.csv','w') as elev: | |
writer = csv.writer(elev) | |
writer.writerow(('Lat','Long','Elev')) | |
for x in range (0, samples): | |
print(v[x]) | |
writer.writerow((v[x]['location']['lat'],v[x]['location']['lng'],v[x]['elevation'])) | |
elev.close() | |
print(open("elev.csv").read()) #debugging | |
print('printed') #debugging |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment