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

Commit

Permalink
add a route_geojson function
Browse files Browse the repository at this point in the history
  • Loading branch information
perrygeo committed Oct 22, 2015
1 parent 1eb5bb1 commit b4ef22c
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
27 changes: 27 additions & 0 deletions mapbox/services/directions.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,33 @@ def route(self,
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
19 changes: 17 additions & 2 deletions tests/test_directions.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@

@responses.activate
def test_directions():
"""Forward geocoding works"""

with open('tests/moors.json') as fh:
body = fh.read()

Expand All @@ -39,4 +37,21 @@ def test_directions():
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 b4ef22c

Please sign in to comment.