Skip to content
This repository has been archived by the owner on Aug 2, 2022. It is now read-only.

Commit

Permalink
Merge b4ef22c into 0ecd094
Browse files Browse the repository at this point in the history
  • Loading branch information
perrygeo committed Oct 22, 2015
2 parents 0ecd094 + b4ef22c commit 7d28dc1
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 0 deletions.
1 change: 1 addition & 0 deletions mapbox/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
from .services.base import Service
from .services.geocoding import Geocoder, InvalidPlaceTypeError
from .services.upload import Uploader
from .services.directions import Directions
58 changes: 58 additions & 0 deletions mapbox/services/directions.py
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
1 change: 1 addition & 0 deletions tests/moors.json

Large diffs are not rendered by default.

57 changes: 57 additions & 0 deletions tests/test_directions.py
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

0 comments on commit 7d28dc1

Please sign in to comment.