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
56 changes: 46 additions & 10 deletions mbta_finder.py
Original file line number Diff line number Diff line change
@@ -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.

Expand All @@ -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)
Expand All @@ -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):
Expand All @@ -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):
Expand All @@ -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")