Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 51 additions & 21 deletions mbta_finder.py
Original file line number Diff line number Diff line change
@@ -1,59 +1,89 @@
"""
Geocoding and Web APIs Project Toolbox exercise
Updated March 20, 2016

Find the MBTA stops closest to a given location.
Find the MBTA stops closest to a given location. Prints the
closest MBTA stop and the distance from the given place to that stop.

Full instructions are at:
https://sites.google.com/site/sd15spring/home/project-toolbox/geocoding-and-web-apis
@author: Erica Lee
"""

import urllib # urlencode function
import urllib2 # urlopen function (better than urllib version)
import json
import urllib, urllib2,json
from pprint import pprint


# 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"
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"


# 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
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["results"][0]["geometry"]["location"]


def get_lat_long(place_name):
"""
Given a place name or address, return a (latitude, longitude) tuple
with the coordinates of the given place.

See https://developers.google.com/maps/documentation/geocoding/
for Google Maps Geocode API URL formatting requirements.

"""
pass

g = {}
g["address"] = place_name
g =sorted(g.iteritems())


url = GMAPS_BASE_URL + urllib.urlencode(g)

coord = get_json(url)
return coord




def get_nearest_station(latitude, longitude):
"""
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
i = {}
i['lat'] = latitude
i['lon'] = longitude
i['api_key'] = MBTA_DEMO_API_KEY

i = sorted(i.iteritems())
url2 = MBTA_BASE_URL + urllib.urlencode(i)

h = urllib2.urlopen(url2)
response_text = h.read()
response_data = json.loads(response_text)

a = response_data["stop"][0]["stop_name"]
b = response_data["stop"][0]["distance"]

return (str(a),str(b))



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
coordinates = get_lat_long(place_name).values()
stop = get_nearest_station(coordinates[0], coordinates[1])[0]
distance = get_nearest_station(coordinates[0], coordinates[1])[1]

return "%s is %s miles away from %s"%(stop, distance, place_name)


print find_stop_near("Fenway Park MA")