From 1025f260607e4704abddbb73f7ad051122628285 Mon Sep 17 00:00:00 2001 From: ClaireKincaid Date: Mon, 28 Mar 2016 23:19:09 -0400 Subject: [PATCH] Turning in my geocoding toolbox --- mbta_finder.py | 55 ++++++++++++++++++++++++++++++++------------------ 1 file changed, 35 insertions(+), 20 deletions(-) diff --git a/mbta_finder.py b/mbta_finder.py index 3ce9df8..29ef4fe 100755 --- a/mbta_finder.py +++ b/mbta_finder.py @@ -3,6 +3,8 @@ Find the MBTA stops closest to a given location. +API Key: AIzaSyBIS2m7-pBDH4hnIOlTd5bNGtnqboGGUZM + Full instructions are at: https://sites.google.com/site/sd15spring/home/project-toolbox/geocoding-and-web-apis """ @@ -10,50 +12,63 @@ import urllib # urlencode function import urllib2 # urlopen function (better than urllib version) import json +from pprint import pprint +import str # Useful URLs (you need to add the appropriate parameters for your requests) GMAPS_BASE_URL = "https://maps.googleapis.com/maps/api/geocode/json" MBTA_BASE_URL = "http://realtime.mbta.com/developer/api/v2/stopsbylocation" MBTA_DEMO_API_KEY = "wX9NwuHnZU2ToO7GmGR9uw" +My_API_URL = "https://maps.googleapis.com/maps/api/geocode/json?address=[number+street+type],[+cityname],[+state]&key=AIzaSyBIS2m7-pBDH4hnIOlTd5bNGtnqboGGUZM" # A little bit of scaffolding if you want to use it +def make_url(place_name): + """ + Given a place name or address, return a url that will open the necessary API + place name must be in string format, "number+street+type,+cityname,+state" + """ + url = "https://maps.googleapis.com/maps/api/geocode/json?address=" + place_name + "&key=AIzaSyBIS2m7-pBDH4hnIOlTd5bNGtnqboGGUZM" + return url + 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): """ Given a place name or address, return a (latitude, longitude) tuple with the coordinates of the given place. + place name must be in string format, "number+street+type,+cityname,+state" + See https://developers.google.com/maps/documentation/geocoding/ for Google Maps Geocode API URL formatting requirements. """ - pass - + url = make_url(place_name) + data = get_json(url) + coordinates = (data["results"]["geometry"]["location"]["lat"], data["results"][0]["geometry"]["location"]["lat"]) + return coordinates -def get_nearest_station(latitude, longitude): +def get_nearest_station(place_name): """ - Given latitude and longitude strings, return a (station_name, distance) - tuple for the nearest MBTA station to the given coordinates. - - See http://realtime.mbta.com/Portal/Home/Documents for URL - formatting requirements for the 'stopsbylocation' API. - """ - pass - - -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. + Given a place name, return a (station_name, distance) + tuple for the nearest MBTA station to the coordinates of the given place """ - pass - + coordinates = get_lat_long(place_name) + latitude = str(coordinates[1]) + longitude = str(coordinates[2]) + lat.lng = "&lat=" + latitude +"&lon=" + longitude + url = "http://realtime.mbta.com/developer/api/v2/stopsbylocation?" + lat.lng + "&format=json" + data = get_json(url) + nearest_stop_name = ["stop"][0]["stop_name"] + distance = ["stop"][0]["distance"] + station = (nearest_stop_name, distance) \ No newline at end of file