From 7e0ce36bfd6caa46b9ce7be470b6fc7b40a03f75 Mon Sep 17 00:00:00 2001 From: Kevin Zhang Date: Tue, 15 Mar 2016 21:58:14 -0400 Subject: [PATCH] I have finished the Geocoding Toolbox 7/5 --- mbta_finder.py | 56 ++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 52 insertions(+), 4 deletions(-) diff --git a/mbta_finder.py b/mbta_finder.py index 3ce9df8..8ff58ba 100755 --- a/mbta_finder.py +++ b/mbta_finder.py @@ -6,6 +6,10 @@ Full instructions are at: https://sites.google.com/site/sd15spring/home/project-toolbox/geocoding-and-web-apis """ +""" +Completed by Kevin Zhang +Software Design Spring 2016 +""" import urllib # urlencode function import urllib2 # urlopen function (better than urllib version) @@ -25,7 +29,11 @@ def get_json(url): Given a properly formatted URL for a JSON web API request, return a Python JSON object containing the response to that request. """ - pass + f = urllib2.urlopen(url) + response_text = f.read() + response_data = json.loads(response_text) + + return response_data def get_lat_long(place_name): @@ -36,7 +44,14 @@ def get_lat_long(place_name): See https://developers.google.com/maps/documentation/geocoding/ for Google Maps Geocode API URL formatting requirements. """ - pass + + place = place_name.replace(" ", "+") + url = GMAPS_BASE_URL + "?address=" + place + json_data = get_json(url) + lat = json_data["results"][0]["geometry"]["location"]["lat"] + lon = json_data["results"][0]["geometry"]["location"]["lng"] + + return lat, lon def get_nearest_station(latitude, longitude): @@ -47,7 +62,15 @@ def get_nearest_station(latitude, longitude): See http://realtime.mbta.com/Portal/Home/Documents for URL formatting requirements for the 'stopsbylocation' API. """ - pass + url = MBTA_BASE_URL + "?api_key=" + MBTA_DEMO_API_KEY + "&lat=" + str(latitude) + "&lon=" + str(longitude) + "&format=json" + json_data = get_json(url) + + + station_name = json_data["stop"][0]["stop_name"] + distance = json_data["stop"][0]["distance"] + + return station_name, distance + def find_stop_near(place_name): @@ -55,5 +78,30 @@ def find_stop_near(place_name): Given a place name or address, print the nearest MBTA stop and the distance from the given place to that stop. """ - pass + lat, lng = get_lat_long(place_name) + print "The location you gave is at " + str(lat) + " latitude and " + str(lng) + " longitude." + print '' + + station_name, distance = get_nearest_station(lat, lng) + print "The nearest MBTA station to that location is " + str(station_name) + ", which is {:.4f} miles away.".format(float(distance)) + print '' + + + +if __name__ == "__main__": + + done = False + count = 0 + while not done: + if count == 0: + place = raw_input("Please type in the a location to determine your nearest MBTA Station (National Post Service format please!)\n\n") + count +=1 + else: + place = raw_input("Wanna type in another location to find the nearest MBTA?\n\n") + + if place == "done": + print "Thanks for using me!" + done = True + else: + find_stop_near(place)