Skip to content

Finished Geocoding ToolBox #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
Binary file added Results.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
27 changes: 21 additions & 6 deletions mbta_finder.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@

# 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"
MBTA_BASE_URL = "http://realtime.mbta.com/developer/api/v2/stopsbylocation?api_key=wX9NwuHnZU2ToO7GmGR9uw"
GMAPS_API_KEY = "AIzaSyCoT-0Iv49u3-Sz2T5O4T8xYer8E16BGrw"


# A little bit of scaffolding if you want to use it
Expand All @@ -25,7 +25,10 @@ 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 +39,11 @@ def get_lat_long(place_name):
See https://developers.google.com/maps/documentation/geocoding/
for Google Maps Geocode API URL formatting requirements.
"""
pass
formatted_place_name = place_name.replace(' ','+')
request_url = GMAPS_BASE_URL + "?address=" + formatted_place_name + "&key=" + GMAPS_API_KEY
response_data = get_json(request_url)
#return latitude and longitude
return (str(response_data["results"][0]["geometry"]["location"]["lat"]),str(response_data["results"][0]["geometry"]["location"]["lng"]))


def get_nearest_station(latitude, longitude):
Expand All @@ -47,13 +54,21 @@ 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 + "&lat=" + latitude + "&lon=" + longitude + "&format=json"
f = urllib2.urlopen(url)
response_text = f.read()
response_data = json.loads(response_text)
station_name = response_data["stop"][0]["stop_name"]
distance = response_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,lon = get_lat_long(place_name)
station_name,distance = get_nearest_station(lat,lon)
print "The nearest station is " + station_name + ", " + distance + " miles away."