From 2ff37f173a4f07a5ac86a073207c4ef5e16d2fd0 Mon Sep 17 00:00:00 2001 From: rerafyaw Date: Thu, 27 Oct 2016 07:53:34 -0400 Subject: [PATCH] Geocoding v1.1 complete: functions now output tuples as directed --- mbta_finder.py | 56 +++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 46 insertions(+), 10 deletions(-) diff --git a/mbta_finder.py b/mbta_finder.py index 3ce9df8..1b2f5a1 100755 --- a/mbta_finder.py +++ b/mbta_finder.py @@ -1,5 +1,6 @@ """ -Geocoding and Web APIs Project Toolbox exercise +Geocoding and Web APIs Project Toolbox +Anderson Ang Wei Jian - Toolbox #3 Find the MBTA stops closest to a given location. @@ -9,7 +10,8 @@ import urllib # urlencode function import urllib2 # urlopen function (better than urllib version) -import json +import json # JSon library +from pprint import pprint # Useful URLs (you need to add the appropriate parameters for your requests) @@ -18,14 +20,16 @@ MBTA_DEMO_API_KEY = "wX9NwuHnZU2ToO7GmGR9uw" -# A little bit of scaffolding if you want to use it - def get_json(url): """ - Given a properly formatted URL for a JSON web API request, return + Given a properly format ted URL for a JSON web API request, return a Python JSON object containing the response to that request. """ - pass + # append URL to Google Maps API base url + 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 +40,16 @@ def get_lat_long(place_name): See https://developers.google.com/maps/documentation/geocoding/ for Google Maps Geocode API URL formatting requirements. """ - pass + param = urllib.urlencode(place_name) + url = GMAPS_BASE_URL + ("?%s" % param) + # runs json query, and stores response data + response_data = get_json(url) + # acquires latitude, and longtitude of location in dict respectively + lat = response_data["results"][0]["geometry"]["location"]["lat"] + lng = response_data["results"][0]["geometry"]["location"]["lng"] + # enrolls the data into a tuple and returns it + tup_loc = (lat,lng) + return tup_loc def get_nearest_station(latitude, longitude): @@ -47,13 +60,36 @@ def get_nearest_station(latitude, longitude): See http://realtime.mbta.com/Portal/Home/Documents for URL formatting requirements for the 'stopsbylocation' API. """ - pass + station_name = "" + distance = 0.0 + mbta_url = MBTA_BASE_URL + "?api_key=%s&lat=%s&lon=%s&format=json" % (MBTA_DEMO_API_KEY, latitude, longitude) + station_data = get_json(mbta_url) + + # note: exceptions do exist for having no closest stops + # try attempts to find the closest stops + try: + station_name = station_data['stop'][0]['stop_name'] + distance = station_data['stop'][0]['distance'] + except: + # exception triggered - no stops found + print "ERROR!" + + return (station_name, distance) + def find_stop_near(place_name): """ - Given a place name or address, print the nearest MBTA stop and the + Given a place name or address, print the nearest MBTA stop and the distance from the given place to that stop. """ - pass + print "You hav entered:" + place_name + "\n" + tuple_loc = get_lat_long({'address' : ('%s' % place_name)}) + tuple_station = get_nearest_station(tuple_loc[0],tuple_loc[1]) + print "The nearest station to this location is at " + str(tuple_station[0]) + "\n" + print "Distance from location: " + str(tuple_station[1]) + " miles" + +if __name__ == '__main__': + find_stop_near("Yawkey Way") + find_stop_near("Cambridge")