Skip to main content

Module with utility functions to generate static choropleth maps with Mapbox

Project description

Installation

Use pip to install mapboxutils:

pip install mapboxutils

Usage

An example script to generate a map with (some) locations of Olympic Games:

from mapboxutil import *

# Define tokens
MAPBOX_PUBLIC = 'pk.aRandomString0f5ixtySevenUpperAndL0werCaseCharactersAndNumb3rsPo1nt.andThenYet1other22M0re'
MAPBOX_SECRET = 'sk.aDiff3rentStr1ngWithRand0mUpperCaseAndLowerCaseCharactersAndNumbers.0fC0urseThese1sAreFak3'

# Define personal data
username = 'yourusername'
stylename = 'Olympic'

# Mapbox has it's own country tileset let's use that one
source_name = 'country_boundaries'
tileset_id = 'mapbox.country-boundaries-v1'

# Set the keys in the global module variables
set_mapbox_token(
    public_key = MAPBOX_PUBLIC,
    secret_key = MAPBOX_SECRET
)

# Define the style
style = make_style(
    stylename,
    add_sources([tileset_id]),
    [
        make_layer(
            source_name,
            make_paint('#CCC', '#FFF')
        ),
        make_layer(
            source_name,
            make_paint('#00C', '#006'),
            make_filter(2035743) # This is the id for Brazil
        ),
        make_layer(
            source_name,
            make_paint('#C00', '#600'),
            [
                "all",
                make_filter('China', 'name_en'),
                # China has some disputed borders
                # To select which version that is used
                # the worldview has to be added
                # This selects China's own worldview
                make_filter('CN', 'worldview')
            ],
        ),
        make_layer(
            source_name,
            make_paint('#CC0', '#660'),
            make_filter('AU', 'iso_3166_1') # Code for Australia
        ),
        make_layer(
            source_name,
            make_paint('#0C0', '#060'),
            make_filter('ESP', 'iso_3166_1_alpha_3') # Code for Spain
        ),
    ]
)

# Check if there is already a style with the name
style_id = get_style_id_by_name(stylename, username=username)
if style_id:
    # Update if the style already exists
    style = update_style(username, style_id, style)
else:
    # Create the style if it's not
    style = create_style(username, style)

# Determine the url
url = mapbox_url(
    **{
        **mapbox_dimensions(
            south = -43.643611,
            north =  53.550000,
            west  = -73.984444,
            east  = 153.638889,
            width = 600,
            height= 360
        ),
        **{
            'username': username,
            'style': style_id,
            'width': 640,
            'height': 400,
            'overlays': [
                overlay_marker(-22.911366, -43.205916, '66F', 'r'), # Rio de Janeiro
                overlay_marker( 39.906667, 116.397500, 'F66', 'p'), # Beijing / Peking
                overlay_marker(-33.865000, 151.209444, 'FF6', 's'), # Sidney
                overlay_marker( 41.383333,   2.183333, '6F6', 'b'), # Barcelona
            ]

        }
    }
)
# Print the url
print(url)

Version history

v1.1.0

  • Add draft parameter to get_style functions. (get_style, get_styles, ````get_style_id_by_name```)
  • Add relation parameter to make_filter function, when set to false it will exculde the matching polygons.
  • Add test parameter to mapbox_url: Prevents mapbox caching when designing maps.
  • Add type to make_layer: fill (default), background and line are implemented.
  • make_paint accepts more parameters: To have the appropriate parameters for each layer type.

v1.0.0

  • Initial version

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

mapboxutil-1.1.1.tar.gz (6.8 kB view details)

Uploaded Source

Built Distribution

mapboxutil-1.1.1-py3-none-any.whl (19.2 kB view details)

Uploaded Python 3

File details

Details for the file mapboxutil-1.1.1.tar.gz.

File metadata

  • Download URL: mapboxutil-1.1.1.tar.gz
  • Upload date:
  • Size: 6.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.22.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.56.2 CPython/3.7.4

File hashes

Hashes for mapboxutil-1.1.1.tar.gz
Algorithm Hash digest
SHA256 cd3bac9979011cfd6c30e96de77519a61fba66d0923f9620a1c6b9638e2145af
MD5 ba145664672d15e3f15c45fc8269194b
BLAKE2b-256 b51408e1b096d82c4ddc6a20aeb49d307296261a2c2eae73d36e8ef3e47a4b63

See more details on using hashes here.

File details

Details for the file mapboxutil-1.1.1-py3-none-any.whl.

File metadata

  • Download URL: mapboxutil-1.1.1-py3-none-any.whl
  • Upload date:
  • Size: 19.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.22.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.56.2 CPython/3.7.4

File hashes

Hashes for mapboxutil-1.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f71bee7ab6e55a4ff93ca17b0954d5deb5cf51f01a7394f34a134e3ac472c752
MD5 2b3c4a747958ad2de08b38d3551c7f65
BLAKE2b-256 7e76716fda7feb60c46a8752a21cfb35559952377e3ec10ee780139e8f0906a8

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