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.1.tar.gz (8.4 kB view details)

Uploaded Source

Built Distributions

mapbox-0.3.1-py3-none-any.whl (13.7 kB view details)

Uploaded Python 3

mapbox-0.3.1-py2-none-any.whl (13.7 kB view details)

Uploaded Python 2

File details

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

File metadata

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

File hashes

Hashes for mapbox-0.3.1.tar.gz
Algorithm Hash digest
SHA256 3829309ab0ad397e7e6949db65dc01be39a54715e1d1751a8022925952bba599
MD5 b24fa3aaedb3040b4d2273a32a5ecbd3
BLAKE2b-256 dab4533482ec5db789546652ef80e7604870c475d3edd0cc75663d6400a8f012

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mapbox-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 62cb59d18f6c9acba5983eed87606e9ec69e63b6d974ec137220d8de84930995
MD5 67850575d13ddfb4ab2b923072cafe1f
BLAKE2b-256 be8bfc30a261d5911fcdc00018487c3038ba3f89fb7f7aa6d16c5fe960a4e9a7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mapbox-0.3.1-py2-none-any.whl
Algorithm Hash digest
SHA256 5c14c9ed6503b091eea463d3d3f4778304e0d5c111daf73d22dd49b96062ea2e
MD5 7bd4517170ec5173d3608b7b230aa3dc
BLAKE2b-256 86f92522ed5565a151ca9ef0e88d83ac5b48ca93f782464320bcd507d3dc995c

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