Skip to main content

Mapbox MBtiles Utilities

A Python 2.7 and 3 library for working with Mabox mbtiles v1.1

Build Status Coverage Status

Features

Provides a lighweight Python API for reading and writing mbtiles files.

Mabox mbtiles v1.1 allow you to store geographic data as rendered image tiles or as vector tiles, along with associated metadata.

Installation

pip install pymbtiles

To install from master branch on GitHub using pip:

pip install git+https://github.com/consbio/pymbtiles.git#egg=pymbtiles --upgrade

Usage

Python API

open for reading and read a tile:

from pymbtiles import MBTiles
with MBtiles('my.mbtiles') as src:
    tile_data = src.read_tile(z=0, x=0, y=0)

returns tile data in bytes.

open for writing (existing file will be overwritten):

with MBtiles('my.mbtiles', mode='w') as out:
    out.write_tile(z=0, x=0, y=0, tile_data)

or write a bunch of tiles at once:

from pymbtiles import MBTiles, Tile

tiles = (
    Tile(z=1, x=0, y=0, tile_data=first_tile),
    ...
)

with MBtiles('my.mbtiles', mode='w') as out:
    out.write_tiles(tiles)

Use r+ mode to read and write.

Metadata is stored in the meta attribute of the mbtiles instance:

with MBtiles('my.mbtiles') as src:
    metadata = src.meta

This metadata is stored in the metadata table in the mbtiles file, and contains a number of records required or optional under the mbtiles specification .

To update metadata:

with MBtiles('my.mbtiles', 'r+') as out:
    out.meta['some_key'] = 'some_value'

You can set several values at once by passing in a new dict object:

with MBtiles('my.mbtiles', 'w') as out:
    out.meta = my_metadata_dict

Listing available tiles

To list available tiles in the tileset:

with MBtiles('my.mbtiles') as src:
    for tile_coords in src.list_tiles():  # [TileCoordinate(z, x, y)...]
        ...

WARNING: for large tilesets, this can exceed available memory.

To list available tilesets for large tilesets, use:

with MBtiles('my.mbtiles') as src:
    for batch in src.list_tiles_batched():
        for tile_coords in batch: # [TileCoordinate(z, x, y)...]
            ...

Set operations

The ops module provides extend, union, and difference functions to perform set operations on tilesets.

Extend a tileset with new tiles from a second:

extend(source_filename, target_filename)

Create a new tileset with unique tiles combined from both left and right tilesets:

union(left_filename, right_filename, out_filename)

Create a new tileset from the tileset in the left tileset not present in the right tileset:

difference(left_filename, right_filename, out_filename)

Tile Scheme

Tiles are output to mbtiles format in xyz tile scheme.

Possibly useful:

  • mbtileserver: a lightweight Go tile server
  • tpkutils: a library for converting ArcGIS tile cache to mbtiles

Changes :

0.5.0

  • added zoom_range, row_range, col_range to provide basic information about tiles available in the tileset

0.4.0

  • added list_tiles to list tiles and list_tiles_batch to list tiles in batches
  • added ops module with extend, union, difference functions

0.3.0

  • all write-like operations for metadata and tiles are now overwrite by default

Credits:

Inspired by:

SQL for creating mbtiles database derived from node-mbtiles

License:

See LICENSE.md

Metadata

Release files for pymbtiles 0.5.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pymbtiles 0.5.0
File Size Uploaded
pymbtiles-0.5.0.tar.gz 7.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pymbtiles 0.5.0
File Interpreter ABI Platform
pymbtiles-0.5.0-py3-none-any.whl Python 3 none any Details

Total release size: 16.1 kB

Release files / pymbtiles-0.5.0.tar.gz

Download URL pymbtiles-0.5.0.tar.gz
Size 7.1 kB
Tags Source
SHA-256 checksum
How to use checksums
b4eb2c470d2eb3d94627cdc8a8ae448b8899af2dd696f9a5eca706ddf8293b58
BLAKE2b-256 checksum
How to use checksums
75ff9ae83bb0cf6c504d675b917eae3ad9e9f919fda3fea51de9f737ac0ccf27
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.4.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.7.4

Release files / pymbtiles-0.5.0-py3-none-any.whl

Download URL pymbtiles-0.5.0-py3-none-any.whl
Size 9.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
91c1c2fa3e25f581d563a60e705105f7277b0dbb9ff727c8c28cb66f0f891c84
BLAKE2b-256 checksum
How to use checksums
82baa05974655e73d7937b8e5438bf7ca5dba0b7d84dc67e98fb40c81dc92fca
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.4.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.7.4

Release history Release notifications | RSS feed

This release

0.5.0 This release

2 release files

0.4.0

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page