Class: QgsVectorTileWriter

class qgis.core.QgsVectorTileWriter

Bases: sip.wrapper

Takes care of writing vector tiles. The intended use is to set up the class by setting the destination URI, extent, zoom level range and input vector layers. Then with a call to writeTiles() the data gets written. In case of a failure, errorMessage() returns the cause of the problem during writing.

The syntax of destination URIs is the same like the data source string used by vector tile layers: parameters are encoded as a HTTP query string, where “type” key determines the type of the destination container, the “url” key is normally the path. Currently supported types:

  • “xyz” - tile data written as local files, using a template where {x},{y},{z} are replaced by the actual tile column, row and zoom level numbers, e.g.: file:///home/qgis/tiles/{z}/{x}/{y}.pbf

  • “mbtiles” - tile data written to a new MBTiles file, the “url” key should be ordinary file system path, e.g.: /home/qgis/output.mbtiles

Currently the writer only support MVT encoding of data.

Metadata support: it is possible to pass a QVariantMap with metadata. This is backend dependent. Currently only “mbtiles” source type supports writing of metadata. The key-value pairs will be written to the “metadata” table in the MBTiles file. Some useful metadata keys listed here, but see MBTiles spec for more details:

  • “name” - human-readable name of the tileset

  • “bounds” - extent in WGS 84: “minlon,minlat,maxlon,maxlat”

  • “center” - default view of the map: “longitude,latitude,zoomlevel”

  • “minzoom” - lowest zoom level

  • “maxzoom” - highest zoom level

  • “attribution” - string that explains the sources of data

  • “description” - descriptions of the content

  • “type” - whether this is an overlay or a basemap

  • “version” - version of the tileset

Vector tile writer always writes “format” and “json” metadata. If not specified in metadata by the client, tile writer also writes “name”, “bounds”, “minzoom” and “maxzoom”.

New in version 3.14:

Methods

errorMessage

Returns error message related to the previous call to writeTiles().

fullExtent

Returns calculated extent that combines extent of all input layers

setDestinationUri

Sets where and how the vector tiles will be written.

setExtent

Sets extent of vector tile output.

setLayers

Sets vector layers and their configuration for output of vector tiles

setMaxZoom

Sets the maximum zoom level of tiles.

setMetadata

Sets that will be written to the output dataset.

setMinZoom

Sets the minimum zoom level of tiles.

setRootTileMatrix

Sets zoom level 0 tile matrix

setTransformContext

Sets coordinate transform context for transforms between layers and tile matrix CRS

writeSingleTile

Encodes single MVT tile

writeTiles

Writes vector tiles according to the configuration.

class Layer

Bases: sip.wrapper

Configuration of a single input vector layer to be included in the output

New in version 3.14.

QgsVectorTileWriter.Layer(layer: QgsVectorLayer) Constructs an entry for a vector layer

QgsVectorTileWriter.Layer(QgsVectorTileWriter.Layer)

filterExpression(self) str

Returns filter expression. If not empty, only features matching the expression will be used

Return type:

str

layer(self) QgsVectorLayer

Returns vector layer of this entry

Return type:

QgsVectorLayer

layerName(self) str

Returns layer name in the output. If not set, layer()->:py:func:~Layer.name will be used.

Return type:

str

maxZoom(self) int

Returns maximum zoom level at which this layer will be used. Negative value means no max. zoom level

Return type:

int

minZoom(self) int

Returns minimum zoom level at which this layer will be used. Negative value means no min. zoom level

Return type:

int

setFilterExpression(self, expr: str)

Sets filter expression. If not empty, only features matching the expression will be used

Parameters:

expr (str) –

setLayerName(self, name: str)

Sets layer name in the output. If not set, layer()->:py:func:~Layer.name will be used.

Parameters:

name (str) –

setMaxZoom(self, maxzoom: int)

Sets maximum zoom level at which this layer will be used. Negative value means no max. zoom level

Parameters:

maxzoom (int) –

setMinZoom(self, minzoom: int)

Sets minimum zoom level at which this layer will be used. Negative value means no min. zoom level

Parameters:

minzoom (int) –

errorMessage(self) str

Returns error message related to the previous call to writeTiles(). Will return an empty string if writing was successful.

Return type:

str

fullExtent(self) QgsRectangle

Returns calculated extent that combines extent of all input layers

Return type:

QgsRectangle

setDestinationUri(self, uri: str)

Sets where and how the vector tiles will be written. See the class description about the syntax of destination URIs.

Parameters:

uri (str) –

setExtent(self, extent: QgsRectangle)

Sets extent of vector tile output. If unset, it will use the full extent of all input layers combined

Parameters:

extent (QgsRectangle) –

setLayers(self, layers: Iterable[QgsVectorTileWriter.Layer])

Sets vector layers and their configuration for output of vector tiles

Parameters:

layers (Iterable[QgsVectorTileWriter.Layer]) –

setMaxZoom(self, maxZoom: int)

Sets the maximum zoom level of tiles. Allowed values are in interval [0,24]

Parameters:

maxZoom (int) –

setMetadata(self, metadata: Dict[str, Any])

Sets that will be written to the output dataset. See class description for more on metadata support

Parameters:

metadata (Dict[str) –

setMinZoom(self, minZoom: int)

Sets the minimum zoom level of tiles. Allowed values are in interval [0,24]

Parameters:

minZoom (int) –

setRootTileMatrix(self, tileMatrix: QgsTileMatrix) bool

Sets zoom level 0 tile matrix

Parameters:

tileMatrix (QgsTileMatrix) –

Return type:

bool

setTransformContext(self, transformContext: QgsCoordinateTransformContext)

Sets coordinate transform context for transforms between layers and tile matrix CRS

Parameters:

transformContext (QgsCoordinateTransformContext) –

writeSingleTile(self, tileID: QgsTileXYZ, feedback: QgsFeedback = None, buffer: int = 256, resolution: int = 4096) QByteArray

Encodes single MVT tile

Parameters:
  • tileID (QgsTileXYZ) – Tile identifier

  • feedback (QgsFeedback = None) – optional, provide cancellation functionality

  • resolution (int = 4096) – the resolution of coordinates of geometries within the tile. The default is 4096

  • buffer (int = 256) – size of the buffer zone around tile edges in integer tile coordinates. The default is 256 (~6%)

Returns a QByteArray with the encoded data

New in version 3.22.

Return type:

QByteArray

writeTiles(self, feedback: QgsFeedback = None) bool

Writes vector tiles according to the configuration. Returns True on success (upon failure one can get error cause using errorMessage())

If a pointer to a feedback object is provided, it can be used to track progress or provide cancellation functionality.

Parameters:

feedback (QgsFeedback = None) –

Return type:

bool