The Directions
class from the mapbox.services.directions
module provides
access to the Mapbox Directions API. You can also import it directly from the
mapbox
module.
>>> from mapbox import Directions
See https://www.mapbox.com/developers/api/directions/ for general documentation of the API.
Your Mapbox access token should be set in your environment; see the access tokens documentation for more information.
The methods of the Directions
class that provide access to the Directions API
return an instance of
requests.Response
.
In addition to the json()
method that returns Python data parsed from the
API, the Directions
responses provide a geojson()
method that converts that
data to a GeoJSON like form.
To get travel directions between waypoints, you can use the Directions API to route up to 25 points. Each of your input waypoints will be visited in order and should be represented by a GeoJSON point feature.
>>> service = Directions()
The input waypoints to the directions
method are
features, typically GeoJSON-like feature dictionaries.
>>> origin = {
... 'type': 'Feature',
... 'properties': {'name': 'Portland, OR'},
... 'geometry': {
... 'type': 'Point',
... 'coordinates': [-122.7282, 45.5801]}}
>>> destination = {
... 'type': 'Feature',
... 'properties': {'name': 'Bend, OR'},
... 'geometry': {
... 'type': 'Point',
... 'coordinates': [-121.3153, 44.0582]}}
The directions()
method can be called with a list of features and the desired
profile.
>>> response = service.directions([origin, destination],
... 'mapbox.driving')
>>> response.status_code
200
>>> response.headers['Content-Type']
'application/json; charset=utf-8'
It returns a response object with a geojson()
method for accessing the
route(s) as a GeoJSON-like FeatureCollection dictionary.
>>> driving_routes = response.geojson()
>>> driving_routes['features'][0]['geometry']['type']
'LineString'
See import mapbox; help(mapbox.Directions)
for more detailed usage.