Skip to main content

A Python client for Mapbox services

Project description

https://travis-ci.org/mapbox/mapbox-sdk-py.png https://coveralls.io/repos/mapbox/mapbox-sdk-py/badge.png

A Python client for Mapbox web services

The Mapbox Python SDK is a low-level client API, not a Resource API such as the ones in boto3 or github3.py. Its methods return objects containing HTTP responses from the Mapbox API.

Services

  • Directions

    • Profiles for driving, walking, and cycling

    • GeoJSON & Polyline formatting

    • Instructions as text or HTML

  • Distance

    • Travel-time tables between up to 100 points

    • Profiles for driving, walking and cycling

  • Geocoding

    • Forward (place names ⇢ longitude, latitude)

    • Reverse (longitude, latitude ⇢ place names)

  • Surface

    • Interpolates values along lines. Useful for elevation traces.

  • Uploads

    • Upload data to be processed and hosted by Mapbox.

Other services coming soon.

Installation

$ pip install mapbox

API Usage

Geocoder

To begin geocoding, import the mapbox module and create a new Geocoder object with your Mapbox access token.

import mapbox

geocoder = mapbox.Geocoder(access_token='YOUR_ACCESS_TOKEN')

Geocoder’s methods return Requests style response objects.

response = geocoder.forward('Chester, NJ')

# response.json() returns the geocoding result as GeoJSON.
# response.status_code returns the HTTP API status code.

response = geocoder.reverse(lon=-74.7083, lat=40.7851)

See import mapbox; help(mapbox.Geocoder) for more detailed usage.

Upload

To upload data, you must created a token with uploads:* scopes at https://www.mapbox.com/account/apps/. Then upload any supported file to your account using the Uploader

from mapbox import Uploader
conxn = Uploader('username', access_token='MY_TOKEN')
resp = conxn.upload('RGB.byte.tif', 'RGB-byte-tif')
upload_id = resp.json()['id']

resp = conxn.status(upload_id).json()
resp['complete']  # True
resp['tileset']   # "username.RGB-byte-tif"

See import mapbox; help(mapbox.Uploader) for more detailed usage.

Directions

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.

from mapbox import Directions
resp = Directions('mapbox.driving').directions([origin, destination])
driving_routes = resp.geojson()
first_route = driving_routes['features'][0]

See import mapbox; help(mapbox.Directions) for more detailed usage.

Distance

If you need to optimize travel between several waypoints, you can use the Distance API to create a “Distance Matrix” showing travel times between all waypoints. Each of your input waypoints should be a GeoJSON point feature.

from mapbox import Distance
resp = Distance('mapbox.driving').distance(points['features'])
resp.json()

which returns:

{
  "durations": [
    [ 0,    2910, null ],
    [ 2903, 0,    5839 ],
    [ 4695, 5745, 0    ]
  ]
}

See import mapbox; help(mapbox.Distance) for more detailed usage.

Surface

To query vector tile attributes along a series of points or a line, you can use the Surface API. For example, you could create an elevation profile against a GeoJSON LineString feature

from mapbox import Surface
Surface().surface([route], mapid='mapbox.mapbox-terrain-v1',
                  layer='contour', fields=['ele'])
profile_pts = resp.geojson()

See import mapbox; help(mapbox.Surface) for more detailed usage.

Testing

pip install -e .[test]
py.test

See Also

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mapbox-0.3.0.tar.gz (7.5 kB view details)

Uploaded Source

Built Distributions

mapbox-0.3.0-py3-none-any.whl (12.2 kB view details)

Uploaded Python 3

mapbox-0.3.0-py2-none-any.whl (12.2 kB view details)

Uploaded Python 2

File details

Details for the file mapbox-0.3.0.tar.gz.

File metadata

  • Download URL: mapbox-0.3.0.tar.gz
  • Upload date:
  • Size: 7.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for mapbox-0.3.0.tar.gz
Algorithm Hash digest
SHA256 511e1eba81751af7f3538482b16c6da74276f0f10c91b92ebaaaa5b9e19aa7c6
MD5 6e0ca27ffb02fb7c67780b76d7ac125d
BLAKE2b-256 b0059e6b462dcd49fce50f3f0188803953192efef47e43306085f0d790fb6162

See more details on using hashes here.

File details

Details for the file mapbox-0.3.0-py3-none-any.whl.

File metadata

File hashes

Hashes for mapbox-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 50e8482d427054a62a8558d07b38a2a35f9560dba559f108f93b0008d21f44a2
MD5 4a12ab0ff68c80fac0fb03a1d4990971
BLAKE2b-256 cc0cbd4deece41e2362732e6cca7d810d4d361eba3ce1da8b21621d547333cff

See more details on using hashes here.

File details

Details for the file mapbox-0.3.0-py2-none-any.whl.

File metadata

File hashes

Hashes for mapbox-0.3.0-py2-none-any.whl
Algorithm Hash digest
SHA256 f82a9217385ab6c1716a807d0e98c4196f55e2b5311463fdecbdfb8d185b2f7e
MD5 71c7e73759a50cf94a5c1b54ead8b85b
BLAKE2b-256 4d52bb4c0abd396e59a8a5df92a7fb12ae17764e74881ebfca5f165760bcbfa9

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page