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: 52 additions & 4 deletions mbta_finder.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
Full instructions are at:
https://sites.google.com/site/sd15spring/home/project-toolbox/geocoding-and-web-apis
"""
"""
Completed by Kevin Zhang
Software Design Spring 2016
"""

import urllib # urlencode function
import urllib2 # urlopen function (better than urllib version)
Expand All @@ -25,7 +29,11 @@ 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):
Expand All @@ -36,7 +44,14 @@ def get_lat_long(place_name):
See https://developers.google.com/maps/documentation/geocoding/
for Google Maps Geocode API URL formatting requirements.
"""
pass

place = place_name.replace(" ", "+")
url = GMAPS_BASE_URL + "?address=" + place
json_data = get_json(url)
lat = json_data["results"][0]["geometry"]["location"]["lat"]
lon = json_data["results"][0]["geometry"]["location"]["lng"]

return lat, lon


def get_nearest_station(latitude, longitude):
Expand All @@ -47,13 +62,46 @@ def get_nearest_station(latitude, longitude):
See http://realtime.mbta.com/Portal/Home/Documents for URL
formatting requirements for the 'stopsbylocation' API.
"""
pass
url = MBTA_BASE_URL + "?api_key=" + MBTA_DEMO_API_KEY + "&lat=" + str(latitude) + "&lon=" + str(longitude) + "&format=json"
json_data = get_json(url)


station_name = json_data["stop"][0]["stop_name"]
distance = json_data["stop"][0]["distance"]

return station_name, distance



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
lat, lng = get_lat_long(place_name)
print "The location you gave is at " + str(lat) + " latitude and " + str(lng) + " longitude."
print ''

station_name, distance = get_nearest_station(lat, lng)
print "The nearest MBTA station to that location is " + str(station_name) + ", which is {:.4f} miles away.".format(float(distance))
print ''



if __name__ == "__main__":

done = False
count = 0
while not done:
if count == 0:
place = raw_input("Please type in the a location to determine your nearest MBTA Station (National Post Service format please!)\n\n")
count +=1
else:
place = raw_input("Wanna type in another location to find the nearest MBTA?\n\n")


if place == "done":
print "Thanks for using me!"
done = True
else:
find_stop_near(place)