Source: https://gometry.monicz.dev/api/vector-tiles/

# Mapbox Vector Tiles

Prepare, encode and decode vector tiles, reuse indexed sources, and generate addressed tiles.

**Related:** [Vector-tile guide](https://gometry.monicz.dev/ecosystem/vector-tiles/)

## Types

## TileBounds

Accepted call forms

```python
TileBounds(
    bounds: Iterable[float], *, space: Space | None = None
) -> TileBounds
```

A tile rectangle and the coordinate frame in which it is expressed.

Define a rectangular tile coordinate frame.

Parameters:

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `bounds` | `Iterable[float]` | The `(minx, miny, maxx, maxy)` rectangle, with positive width and height. | *required* |
| `space` | `Space \| None` | Complete planar interpretation of the bounds. Omission means an unreferenced plane. A geographic CRS is valid with the planar model; chart and spherical models are not affine tile frames. | `None` |

Returns:

| Type | Description |
| --- | --- |
| `TileBounds` | The immutable tile rectangle and its coordinate frame. |

Raises:

| Type | Description |
| --- | --- |
| `TypeError` | If `space` is not a Space. |
| `GeometryError` | If the Space is not Planar or the bounds do not define a rectangle with positive width and height. |

### bounds `property`

```python
bounds: tuple[float, float, float, float]
```

The frame rectangle.

Returns:

| Type | Description |
| --- | --- |
| `tuple of float` | Bounds as `(minx, miny, maxx, maxy)`. |

### space `property`

```python
space: Space
```

The complete planar coordinate interpretation.

Returns:

| Type | Description |
| --- | --- |
| `Space` | The frame's CRS, model and coordinate epoch. |

## MVTLayer

Accepted call forms

```python
MVTLayer(features: Features, *, extent: SupportsIndex = 4096) -> MVTLayer
```

An immutable feature snapshot in an MVT-local integer grid.

Create a layer from geometry already in the local integer grid.

Parameters:

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `features` | `Features` | Local geometries, supported scalar properties, and optional unsigned integer IDs. Missing and empty geometries are omitted. | *required* |
| `extent` | `SupportsIndex` | Positive integer width and height of the tile grid. | `4096` |

Returns:

| Type | Description |
| --- | --- |
| `MVTLayer` | The immutable local layer. |

### extent `property`

```python
extent: int
```

The tile-grid extent.

Returns:

| Type | Description |
| --- | --- |
| `int` | Integer width and height of the grid. |

### features `property`

```python
features: Features[Geometry, str | bool | int | float | None, int]
```

The layer's feature snapshot.

Returns:

| Type | Description |
| --- | --- |
| `Features` | Local geometry, sparse properties and optional IDs. |

### version `property`

```python
version: int
```

The MVT wire version.

Returns:

| Type | Description |
| --- | --- |
| `int` | The version number, always 2. |

### to\_features

Accepted call forms

```python
to_features(
    *,
    tile: Tile,
    target_space: Space | None = None,
    tolerance: float | None = None,
    unit: Literal['meters'] | None = None,
) -> Features[Geometry, str | bool | int | float | None, int]
```

```python
to_features(
    *,
    frame: TileBounds,
    target_space: Space | None = None,
    tolerance: float | None = None,
    unit: Literal['meters'] | None = None,
) -> Features[Geometry, str | bool | int | float | None, int]
```

Locate local coordinates in an XYZ tile or custom frame.

Parameters:

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `tile` | `Tile` | XYZ address in Web Mercator. Supply exactly one of `tile` and `frame`. | `None` |
| `frame` | `TileBounds` | Custom rectangle and complete planar Space. | `None` |
| `target_space` | `Space or None` | Additional shape conversion after affine placement in the tile or frame Space. Omission retains that Space. An explicit target owns its complete CRS, model and epoch; `epoch=None` clears the placement epoch. | `None` |
| `tolerance` | `float or None` | Positional objective for shape conversion, in the target Space's natural units unless `unit='meters'`. | `None` |
| `unit` | `'meters'` | Unit for the conversion objective, with the ordinary `to_space` meaning. Omitted target means the unchanged placement Space. | `'meters'` |

Returns:

| Type | Description |
| --- | --- |
| `Features` | Located geometries with the layer's properties and IDs. |

Raises:

| Type | Description |
| --- | --- |
| `CRSError` | If the requested coordinate transformation cannot be performed. |
| `GeometryError` | If the located geometry cannot be represented. |

## MVTSource

Accepted call forms

```python
MVTSource(
    features: Features | Iterable[Features],
    *,
    target_space: Space | None = None,
    coverage: bool = False,
    **options: Unpack[PrepareOptions],
) -> MVTSource
```

An immutable original feature source with reusable spatial selection.

Snapshot and index a feature collection for repeated tile preparation.

Parameters:

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `features` | `Features \| Iterable[Features]` | Input features or batches. Batches are consumed once. | *required* |
| `target_space` | `Space \| None` | Complete planar preparation Space. Omission means planar EPSG:3857 without an epoch, as required by XYZ tiles. Source geometries retain their original Spaces until actual conversion. | `None` |
| `coverage` | `bool` | Treat the source as a polygonal coverage and validate it jointly. | `False` |
| `options` | `Unpack[PrepareOptions]` | Preparation settings: `extent` (4096), `buffer` (64), `tolerance` (0, in grid units), `method` (`'vw'` by default, or `'dp'`), `clip` (True), `on_collapse` (`'omit'` by default, or `'raise'`), and `drop_zm` (False). | `not applicable` |

Returns:

| Type | Description |
| --- | --- |
| `MVTSource` | The original snapshot and reusable source index. |

Raises:

| Type | Description |
| --- | --- |
| `CRSError` | If source and target CRS metadata do not admit the requested coordinate operation. |
| `GeometryError` | If an input geometry or declared coverage is invalid. |

### with\_options

```python
with_options(**options: Unpack[TileOptions]) -> MVTSource
```

Return another tile policy sharing the source and spatial index.

Parameters:

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `**options` | `Unpack[TileOptions]` | Replacement `extent`, `buffer`, `tolerance`, `method`, `clip`, or `on_collapse` settings. | `{}` |

Returns:

| Type | Description |
| --- | --- |
| `MVTSource` | A source sharing the indexed snapshot with the new tile policy. |

## Functions

## mvt\_prepare

Accepted call forms

```python
mvt_prepare(
    features: Features | Iterable[Features],
    *,
    tile: Tile,
    **options: Unpack[PrepareOptions],
) -> MVTLayer
```

```python
mvt_prepare(
    features: Features | Iterable[Features],
    *,
    frame: TileBounds,
    **options: Unpack[PrepareOptions],
) -> MVTLayer
```

```python
mvt_prepare(features: MVTSource, *, tile: Tile) -> MVTLayer
```

```python
mvt_prepare(features: MVTSource, *, frame: TileBounds) -> MVTLayer
```

Prepare feature geometry on a tile's integer grid.

Parameters:

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `features` | `Features, MVTSource or iterable of Features` | Features, a reusable indexed source, or batches of features. | *required* |
| `tile` | `Tile` | XYZ address. Supply exactly one of `tile` and `frame`. | `None` |
| `frame` | `TileBounds` | Custom tile frame. | `None` |
| `**options` | `Any` | Preparation settings for ordinary features: `extent` (4096), `buffer` (64), `tolerance` (0, in grid units), `method` (`'vw'` by default, or `'dp'`), `clip` (True), `on_collapse` (`'omit'` by default, or `'raise'`), and `drop_zm` (False). An `MVTSource` owns its settings and does not accept per-call options. | `{}` |

Returns:

| Type | Description |
| --- | --- |
| `MVTLayer` | Prepared local integer geometry and aligned properties and IDs. |

Notes

Coordinate conversion uses the original geometry's Space and a positional
objective of 0.1 continuous grid units for this tile, before integer rounding
and optional simplification. `drop_zm=True` permits Z/M loss in the tile;
physical source Z still participates in coordinate conversion.

## to\_mvt

Accepted call forms

```python
to_mvt(
    values: Features | Iterable[Features],
    *,
    layer: str,
    tile: Tile,
    **options: Unpack[PrepareOptions],
) -> bytes
```

```python
to_mvt(
    values: Features | Iterable[Features],
    *,
    layer: str,
    frame: TileBounds,
    **options: Unpack[PrepareOptions],
) -> bytes
```

```python
to_mvt(
    values: Mapping[str, Features | Iterable[Features]],
    *,
    tile: Tile,
    **options: Unpack[PrepareOptions],
) -> bytes
```

```python
to_mvt(
    values: Mapping[str, Features | Iterable[Features]],
    *,
    frame: TileBounds,
    **options: Unpack[PrepareOptions],
) -> bytes
```

```python
to_mvt(values: MVTSource, *, layer: str, tile: Tile) -> bytes
```

```python
to_mvt(values: MVTSource, *, layer: str, frame: TileBounds) -> bytes
```

```python
to_mvt(
    values: Mapping[str, Features | Iterable[Features] | MVTSource],
    *,
    tile: Tile,
) -> bytes
```

```python
to_mvt(
    values: Mapping[str, Features | Iterable[Features] | MVTSource],
    *,
    frame: TileBounds,
) -> bytes
```

```python
to_mvt(values: MVTLayer, *, layer: str) -> bytes
```

```python
to_mvt(values: Mapping[str, MVTLayer]) -> bytes
```

Prepare and encode feature data as an uncompressed vector tile.

Parameters:

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `values` | `(MVTLayer, Features, MVTSource, iterable or mapping)` | A single layer or a mapping of names to layer inputs. Ordinary features are prepared in the supplied tile or frame; local layers are encoded directly. | *required* |
| `layer` | `str` | Name of a single layer. Omit for a mapping of layers. | `None` |
| `tile` | `Tile` | XYZ address used to prepare ordinary or source features. | `None` |
| `frame` | `TileBounds` | Custom tile frame, mutually exclusive with `tile`. | `None` |
| `**options` | `Any` | Preparation settings for ordinary features; see `mvt_prepare`. Local layers and indexed sources do not accept per-call settings. | `{}` |

Returns:

| Type | Description |
| --- | --- |
| `bytes` | Uncompressed MVT data. |

## from\_mvt

Accepted call forms

```python
from_mvt(
    data: Buffer,
    *,
    layer: str,
    properties: Iterable[str] | None = None,
    target_space: None = None,
) -> MVTLayer
```

```python
from_mvt(
    data: Buffer,
    *,
    layers: Iterable[str] | None = None,
    properties: Iterable[str] | None = None,
    target_space: None = None,
) -> dict[str, MVTLayer]
```

```python
from_mvt(
    data: Buffer,
    *,
    layer: str,
    tile: Tile,
    properties: Iterable[str] | None = None,
    target_space: Space | None = None,
    tolerance: float | None = None,
    unit: Literal['meters'] | None = None,
) -> Features[Geometry, str | bool | int | float | None, int]
```

```python
from_mvt(
    data: Buffer,
    *,
    layer: str,
    frame: TileBounds,
    properties: Iterable[str] | None = None,
    target_space: Space | None = None,
    tolerance: float | None = None,
    unit: Literal['meters'] | None = None,
) -> Features[Geometry, str | bool | int | float | None, int]
```

```python
from_mvt(
    data: Buffer,
    *,
    tile: Tile,
    layers: Iterable[str] | None = None,
    properties: Iterable[str] | None = None,
    target_space: Space | None = None,
    tolerance: float | None = None,
    unit: Literal['meters'] | None = None,
) -> dict[str, Features[Geometry, str | bool | int | float | None, int]]
```

```python
from_mvt(
    data: Buffer,
    *,
    frame: TileBounds,
    layers: Iterable[str] | None = None,
    properties: Iterable[str] | None = None,
    target_space: Space | None = None,
    tolerance: float | None = None,
    unit: Literal['meters'] | None = None,
) -> dict[str, Features[Geometry, str | bool | int | float | None, int]]
```

Decode raw MVT bytes into local or located feature layers.

Parameters:

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `data` | `buffer` | Uncompressed MVT bytes. | *required* |
| `layer` | `str or None` | Select one named layer and return it directly. | `None` |
| `layers` | `iterable of str or None` | Select named layers, returning a mapping. Mutually exclusive with `layer`; if both are omitted, all layers are returned. | `None` |
| `properties` | `iterable of str or None` | Property names to retain, or `None` for all properties. | `None` |
| `tile` | `Tile or None` | Locate geometries in an XYZ tile. Mutually exclusive with `frame`. | `None` |
| `frame` | `TileBounds or None` | Locate geometries in a custom frame. | `None` |
| `target_space` | `Space or None` | Additional actual shape conversion after locating the grid in its planar tile/frame Space. Omission retains the placement Space; an explicit target supplies its complete model and epoch, including clearing the placement epoch with `epoch=None`. Requires a tile or frame. | `None` |
| `tolerance` | `float or None` | Positional objective for located shape conversion, with the ordinary `to_space` meaning rather than tile-grid simplification units. | `None` |
| `unit` | `'meters'` | Unit for that objective. Omitted target retains the placement Space. | `'meters'` |

Returns:

| Type | Description |
| --- | --- |
| `(MVTLayer, Features or dict)` | Local layers by default, or located features when a tile or frame is supplied. A single `layer` returns that value rather than a mapping. |

Raises:

| Type | Description |
| --- | --- |
| `CRSError` | If located geometries cannot be transformed to the requested frame. |
| `GeometryError` | If decoded geometry is invalid. |
| `ParseError` | If the vector tile is malformed. |

## mvt\_query\_regions

Accepted call forms

```python
mvt_query_regions(
    *, tile: Tile, extent: SupportsIndex = 4096, buffer: SupportsIndex = 64
) -> tuple[TileBounds, ...]
```

```python
mvt_query_regions(
    *,
    frame: TileBounds,
    extent: SupportsIndex = 4096,
    buffer: SupportsIndex = 64,
) -> tuple[TileBounds, ...]
```

Return conservative preparation-Space rectangles for clipped tiles.

Parameters:

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `tile` | `Tile` | XYZ address. Supply exactly one of `tile` and `frame`. | `None` |
| `frame` | `TileBounds` | Custom frame to expand. | `None` |
| `extent` | `int` | Positive integer tile-grid extent. | `4096` |
| `buffer` | `int` | Nonnegative tile-grid buffer. | `64` |

Returns:

| Type | Description |
| --- | --- |
| `tuple of TileBounds` | Conservative query rectangles in the custom frame's Space, or planar EPSG:3857 for XYZ tiles. XYZ rectangles may be split at the world seam. These are not inverse-projected windows in an arbitrary dataset Space. |

## mvt\_tiles

Accepted call forms

```python
mvt_tiles(
    sources: Mapping[str, MVTSource], *, tiles: Iterable[Tile]
) -> Generator[tuple[Tile, bytes], None, None]
```

```python
mvt_tiles(
    sources: Mapping[str, MVTSource],
    *,
    min_zoom: SupportsIndex,
    max_zoom: SupportsIndex,
) -> Generator[tuple[Tile, bytes], None, None]
```

Generate addressed tiles lazily, or traverse nonempty tiles in zoom order.

Parameters:

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `sources` | `mapping of str to MVTSource` | Named layers indexed in EPSG:3857 without an epoch. | *required* |
| `tiles` | `iterable of Tile` | Explicit addresses to generate in input order, including empty tiles. Do not combine with a zoom range. | `None` |
| `min_zoom` | `int` | First zoom level for automatic traversal. | `None` |
| `max_zoom` | `int` | Last zoom level for automatic traversal. Both zoom bounds are inclusive and must lie between 0 and 29. Automatic traversal requires clipped sources and omits empty tiles. | `None` |

Returns:

| Type | Description |
| --- | --- |
| `generator of (Tile, bytes)` | Tile addresses and their uncompressed MVT data. |
