Skip to content

turning in geocoding project toolbox #19

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
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
48 changes: 38 additions & 10 deletions mbta_finder.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,38 @@
import urllib # urlencode function
import urllib2 # urlopen function (better than urllib version)
import 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"
GMAPS_API_KEY = "AIzaSyBAbkJTjX3Vk0eorBNKFAlIlbyH7NC4frg"
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_gmaps_url(location):
"""
Gives the url needed for the Google Maps API for a given location.
"""
url = GMAPS_BASE_URL + '?address='
words = location.split(' ')
for word in words[0:-1]:
url = url + word + '%20'
url = url + words[-1] + '&key=' + GMAPS_API_KEY
return url

def get_mbta_url(lat, lon):
"""
Gives the url needed for the mbta API for a given latitude and longitude
"""
return MBTA_BASE_URL + '?api_key=' + MBTA_DEMO_API_KEY + '&lat=' + str(lat) + '&lon=' + str(lon) + '&format=json'

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()
return json.loads(response_text)

def get_lat_long(place_name):
"""
Expand All @@ -36,7 +51,11 @@ def get_lat_long(place_name):
See https://developers.google.com/maps/documentation/geocoding/
for Google Maps Geocode API URL formatting requirements.
"""
pass
url = get_gmaps_url(place_name)
response_data = get_json(url)
latitude = response_data[u'results'][0][u'geometry'][u'location'][u'lat']
longitude = response_data[u'results'][0][u'geometry'][u'location'][u'lng']
return (latitude, longitude)


def get_nearest_station(latitude, longitude):
Expand All @@ -47,13 +66,22 @@ def get_nearest_station(latitude, longitude):
See http://realtime.mbta.com/Portal/Home/Documents for URL
formatting requirements for the 'stopsbylocation' API.
"""
pass
url = get_mbta_url(latitude, longitude)
f = urllib2.urlopen(url)
response_text = f.read()
response_data = json.loads(response_text)
distance = response_data[u'stop'][0][u'distance']
stop_name = response_data[u'stop'][0][u'stop_name']
return (stop_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.
>>>find_stop_near('Faneuil Hall')
The nearest stop is Congress St @ North St, which is 0.0524380765855312 miles away.
"""
pass

(lat, lon) = get_lat_long(place_name)
(stop_name, distance) = get_nearest_station(lat,lon)
print 'The nearest stop is ' + stop_name + ', which is ' + distance + ' miles away.'