Skip to content
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

Added transportation mode option and updated Mapbox API #2

Open
wants to merge 2 commits 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
26 changes: 22 additions & 4 deletions directions/routers.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
class Google(Router):
url = 'http://maps.googleapis.com/maps/api/directions/json'
default_name = 'google'
modes = ('driving','cycling','walking','transit')

def __init__(self, *args, **kwargs):
Router.__init__(self, *args, **kwargs)
Expand Down Expand Up @@ -58,7 +59,18 @@ def _query_params(self, waypoints):
return payload

def raw_query(self, waypoints, **kwargs):


payload = self._query_params(waypoints)

# Set transit mode of request
mode = kwargs.pop('mode',None)
if mode == 'cycling':
# Name change
mode = 'bicycling'
if mode is not None:
payload['mode'] = mode

payload.update(kwargs)

r = requests.get(self.url, params=payload)
Expand Down Expand Up @@ -191,21 +203,27 @@ class MapquestOpen(Mapquest):

class Mapbox(Router):
default_name = 'mapbox'
modes = ('driving','cycling','walking')

# https://www.mapbox.com/developers/api/directions/
def __init__(self, mapid, *args, **kwargs):
def __init__(self, access_token, *args, **kwargs):
Router.__init__(self, *args, **kwargs)
self.mapid = mapid
self.access_token = access_token

def _convert_coordinate(self, p):
return '{0[0]},{0[1]}'.format(p)

def raw_query(self, waypoints, **kwargs):
baseurl = 'http://api.tiles.mapbox.com/v3/{mapid}/directions/driving/{waypoints}.json'
baseurl = 'https://api.mapbox.com/v4/directions/{profile}/{waypoints}.json?access_token={access_token}'
formatted_points = ';'.join(self._convert_coordinate(p)
for p in waypoints)

url = baseurl.format(mapid=self.mapid, waypoints=formatted_points)
mode = kwargs.pop('mode',self.modes[0])

url = baseurl.format(
access_token=self.access_token,
profile='mapbox.'+mode,
waypoints=formatted_points)
payload = {'alternatives': 'false'}
r = requests.get(url, params=payload)

Expand Down