Mapbox Vector Tile
Mapbox Vector Tile (MVT) is a Python library for encoding and decoding Mapbox Vector Tiles. It is currently at version 2.2.0 and receives active maintenance releases, typically addressing dependency updates and minor improvements. MVTs are an efficient format for tiled vector data, commonly used in web mapping applications for performant rendering.
Common errors
-
ValueError: GeometryCollection types are not supported
cause Attempting to encode GeoJSON features of type 'GeometryCollection', which the `mapbox-vector-tile` library does not support directly.fixBefore encoding, flatten any `GeometryCollection` features into their constituent 'Point', 'LineString', or 'Polygon' geometries. You will need to process the input GeoJSON to extract individual geometry types. -
KeyError: 'features' when accessing decoded data (or similar structure mismatch)
cause You are decoding a tile with `mapbox_vector_tile.decode()` and expecting the output structure from versions prior to 2.0.0, but the default behavior changed to RFC7946 compliance.fixIf you need the pre-2.0.0 output format, pass `geojson=False` to the `decode` method: `decoded_data = mapbox_vector_tile.decode(tile_bytes, geojson=False)`. Otherwise, adjust your code to expect the RFC7946 compliant GeoJSON output structure. -
Mapbox GL JS / Renderer displays unexpected holes, overlapping polygons, or missing features from vector tiles.
cause The input geometries used for encoding did not strictly adhere to the Mapbox Vector Tile Specification v2, particularly regarding polygon winding order or OGC validity.fixEnsure that exterior rings of polygons are clockwise and interior rings (holes) are counter-clockwise when viewed in screen coordinates. Validate geometries for self-intersections or other invalid conditions before encoding. The library's internal `on_invalid_geometry_make_valid` option can help, but pre-validation is often more robust.
Warnings
- breaking Version 2.0.0 of `mapbox-vector-tile` dropped support for Python 2.x. It now requires Python 3.9 or newer.
- deprecated The `decode` function's `geojson` parameter default changed in versions >=2.0.0. It now defaults to `True`, which enforces RFC7946 compatible GeoJSON output. Using the old non-RFC7946 compliant output (pre-2.0.0 behavior) is deprecated.
- gotcha The `mapbox-vector-tile` library can utilize the C++ implementation of the underlying `protobuf` library for significantly better performance compared to the pure Python implementation. This often requires additional system-level setup.
- gotcha Mapbox Vector Tile Specification v2 (which `mapbox-vector-tile` aims to support) requires OGC-valid geometries and specific winding orders for polygons (exterior rings clockwise, interior rings counter-clockwise in screen coordinates) to correctly represent holes. Invalid geometries or incorrect winding orders can lead to rendering issues or dropped features.
Install
-
pip install mapbox-vector-tile -
pip install mapbox-vector-tile[proj]
Imports
- mapbox_vector_tile
import mapbox_vector_tile
Quickstart
import mapbox_vector_tile
# Example GeoJSON-like data structure for a layer
layer_data = {
"name": "water",
"features": [
{
"geometry": "POLYGON ((0 0, 0 1, 1 1, 1 0, 0 0))", # WKT geometry
"properties": {"uid": 123, "foo": "bar"}
},
{
"geometry": "LINESTRING(-71.160281 42.258729,-71.160837 42.259113)", # WKT geometry
"properties": {"id": 456, "type": "river"}
}
]
}
# Encode to MVT (bytes)
encoded_tile = mapbox_vector_tile.encode([layer_data])
print(f"Encoded tile size: {len(encoded_tile)} bytes")
# Decode MVT (returns dictionary of layers)
decoded_layers = mapbox_vector_tile.decode(encoded_tile)
# Accessing decoded data
print(f"Decoded layers: {list(dec_layers.keys())}")
water_layer = decoded_layers.get('water')
if water_layer:
print(f"First feature in 'water' layer: {water_layer['features'][0]['properties']}")