This repository has been archived by the owner on Aug 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 111
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
117 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
from uritemplate import URITemplate | ||
from mapbox.encoding import encode_waypoints | ||
from .base import Service | ||
|
||
|
||
class Directions(Service): | ||
|
||
def __init__(self, profile='mapbox.driving', access_token=None): | ||
self.profile = self._validate_profile(profile) | ||
self.baseuri = 'https://api.mapbox.com/v4/directions' | ||
self.session = self.get_session(access_token) | ||
|
||
def _validate_profile(self, profile): | ||
valid_profiles = ['mapbox.driving', 'mapbox.cycling', 'mapbox.walking'] | ||
if profile not in valid_profiles: | ||
raise ValueError("{} is not a valid profile".format(profile)) | ||
return profile | ||
|
||
def route(self, | ||
features, | ||
alternatives=None, | ||
instructions=None, | ||
geometry=None, | ||
steps=None): | ||
|
||
waypoints = encode_waypoints(features, precision=6, | ||
min_limit=2, max_limit=30) | ||
|
||
# TODO add optional args as url params | ||
uri = URITemplate('%s/{profile}/{waypoints}.json' % self.baseuri).expand( | ||
profile=self.profile, waypoints=waypoints) | ||
return self.session.get(uri) | ||
|
||
def route_geojson(self, *args, **kwargs): | ||
res = self.route(*args, **kwargs) | ||
data = res.json() | ||
fc = { | ||
'type': 'FeatureCollection', | ||
'features': []} | ||
|
||
for route in data['routes']: | ||
|
||
feature = { | ||
'properties': { | ||
# TODO handle these nested structures | ||
# Flatten or ??? | ||
# 'destination': data['destination'], | ||
# 'origin': data['origin'], | ||
# 'waypoints': data['waypoints'], | ||
# 'steps': route['steps'] | ||
'distance': route['distance'], | ||
'duration': route['duration'], | ||
'summary': route['summary']}} | ||
|
||
feature['geometry'] = route['geometry'] | ||
fc['features'].append(feature) | ||
|
||
return fc |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import responses | ||
import mapbox | ||
|
||
|
||
points = [{ | ||
"type": "Feature", | ||
"properties": {}, | ||
"geometry": { | ||
"type": "Point", | ||
"coordinates": [ | ||
-87.33787536621092, | ||
36.539156961321574]}}, { | ||
"type": "Feature", | ||
"properties": {}, | ||
"geometry": { | ||
"type": "Point", | ||
"coordinates": [ | ||
-88.2476806640625, | ||
36.92217534275667]}}] | ||
|
||
|
||
@responses.activate | ||
def test_directions(): | ||
with open('tests/moors.json') as fh: | ||
body = fh.read() | ||
|
||
responses.add( | ||
responses.GET, | ||
'https://api.mapbox.com/v4/directions/mapbox.driving/-87.337875%2C36.539157%3B-88.247681%2C36.922175.json?access_token=pk.test', | ||
match_querystring=True, | ||
body=body, status=200, | ||
content_type='application/json') | ||
|
||
res = mapbox.Directions(access_token='pk.test').route(points) | ||
assert res.status_code == 200 | ||
assert sorted(res.json()['routes'][0].keys()) == ['distance', 'duration', 'geometry', 'steps', 'summary'] | ||
assert sorted(res.json().keys()) == ['destination', 'origin', 'routes', 'waypoints'] | ||
|
||
|
||
@responses.activate | ||
def test_directions_geojson(): | ||
with open('tests/moors.json') as fh: | ||
body = fh.read() | ||
|
||
responses.add( | ||
responses.GET, | ||
'https://api.mapbox.com/v4/directions/mapbox.driving/-87.337875%2C36.539157%3B-88.247681%2C36.922175.json?access_token=pk.test', | ||
match_querystring=True, | ||
body=body, status=200, | ||
content_type='application/json') | ||
|
||
fc = mapbox.Directions(access_token='pk.test').route_geojson(points) | ||
assert fc['type'] == 'FeatureCollection' | ||
assert sorted(fc['features'][0]['properties'].keys()) == ['distance', 'duration', 'summary'] | ||
assert fc['features'][0]['geometry']['type'] == "LineString" | ||
|
||
# TODO test as_featurecollection=True |