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

Uploaded Source

Built Distributions

mapbox-0.4.0-py3-none-any.whl (13.5 kB view details)

Uploaded Python 3

mapbox-0.4.0-py2-none-any.whl (13.5 kB view details)

Uploaded Python 2

File details

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

File metadata

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

File hashes

Hashes for mapbox-0.4.0.tar.gz
Algorithm Hash digest
SHA256 2a1ffeab891f082140e7585ecac6acc8716cbbddfc7c23ec6aa000a8dfce57f4
MD5 deb36540ceddd73652cf201503a07b6a
BLAKE2b-256 c399aeda92e932576462f4476518a928f8d5bf6de73b09119c147674e7713f57

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mapbox-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1188c5a3e0025c92345b92b6b95b431021e3b0907f109a46998c2f94ac9e1fc9
MD5 6d5c737128a3a91ec2ca408005d69b92
BLAKE2b-256 61f749f36b256ac08e38b05ea3a657447f49471b2888cbdd0f446b92ac280445

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mapbox-0.4.0-py2-none-any.whl
Algorithm Hash digest
SHA256 686e88da3ce012963ce842d31879e6a493abe4c9b0f7cded3db31340f07bb8a8
MD5 2fc1ff74e107fecc3a5756c73ea998a5
BLAKE2b-256 7c16fd8dfb9aa22dc731c5dbcb70b65ac051396bc7a57607b999dc3c23f03b34

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