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
55 changes: 35 additions & 20 deletions mbta_finder.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,57 +3,72 @@

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

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)