Skip to content

Vector tiles (MVT)

Decode PMTiles, MBTiles, XYZ and .mvt vector tiles into DuckDB rows with GEOMETRY columns

Connector software
Free and open source
Service credentials
Not needed
Use with an agent

Install the Orchard skill in your project to help your agent find connectors, configure them, and verify SQL results.

npx skills add Query-farm/vgi-skills --skill orchard

Copy this request into your coding agent to get started.

Use https://query.farm/products/orchard/connectors/mvt/index.md to help me query Vector tiles (MVT) with DuckDB or Haybarn 2.0.1 and VGI protocol 3.4. Ask only for missing details about the result I need or whether I want local execution, self-hosting or Query Farm hosting. Check the connector's actual hosting availability, credentials and input requirements. Read one format of each needed reference and the exact object declarations before writing SQL. Use a released connector, and distinguish a saved example result from anything you actually execute. If you cannot run the query, tell me what remains to verify.

Road kinds in the Protomaps Florence sample, read over HTTP range requests

Example output · captured
Connector release
v0.4.0
Engine
2.0.1
VGI protocol
3.4.0
SELECT properties->>'pmap:kind' AS kind, count(*) AS n
FROM mvt.main.read_tiles('https://raw.githubusercontent.com/protomaps/PMTiles/main/spec/v3/protomaps(vector)ODbL_firenze.pmtiles', zoom := 14)
WHERE layer = 'roads'
GROUP BY ALL
ORDER BY n DESC
Result · first 5 rows
kind n
minor_road 2115
path 946
medium_road 306
major_road 166
other 17

Quick start

Choose where to run Vector tiles (MVT), then follow the steps below.

The connector runs alongside DuckDB or Haybarn. No Query Farm account is needed.

1. Install the vgi extension

Run this in your SQL client. On DuckDB, keep FROM query_farm on LOAD in later sessions.

setup.sql
SET allow_extension_repositories = 'allowed';

CREATE EXTENSION REPOSITORY IF NOT EXISTS query_farm
    WITH PREFIX 'https://duckdb-extensions.query.farm';

INSTALL vgi FROM query_farm;
LOAD vgi FROM query_farm;

2. Attach the connector

Attach with uvx

Needs uv. uvx builds the worker from its release tag on first attach and reuses the cached environment after that.

ATTACH 'mvt' AS mvt (
  TYPE vgi,
  LOCATION 'uvx --from git+https://github.com/Query-farm/[email protected] vgi-mvt-worker'
);

3. Run a query

query.sql
SELECT properties->>'pmap:kind' AS kind, count(*) AS n
FROM mvt.main.read_tiles('https://raw.githubusercontent.com/protomaps/PMTiles/main/spec/v3/protomaps(vector)ODbL_firenze.pmtiles', zoom := 14)
WHERE layer = 'roads'
GROUP BY ALL
ORDER BY n DESC
More example queries →
Technical details
Verified connector release
v0.4.0
Engine
2.0.1
VGI protocol
3.4.0
Verified on
December 1, 2026
Release notes on GitHub
Pin a version for production

When running the connector yourself, keep a specific release until you’re ready to upgrade. Replace the ATTACH statement above with this one and follow the same extension and credential setup.

Attach with uvx

Needs uv. uvx builds the worker from its release tag on first attach and reuses the cached environment after that.

ATTACH 'mvt' AS mvt (
  TYPE vgi,
  LOCATION 'uvx --from git+https://github.com/Query-farm/[email protected] vgi-mvt-worker'
);

Overview

Decodes Mapbox Vector Tiles from PMTiles and MBTiles files, XYZ tile servers, single tiles and tile blobs into DuckDB rows with GEOMETRY and attribute columns, and writes query results back out as PMTiles vector tilesets.

ATTACH options

tileset
PMTiles file or http(s) URL, or local MBTiles file, to expose as tables (one per layer). Empty attaches only the reader functions.VARCHAR
zoom
Zoom level the layer tables read; -1 (default) uses the tileset's maxzoom.BIGINT
bbox
Limit every layer table to the tiles covering 'west,south,east,north' (degrees); empty reads all.VARCHAR
coordinates
Geometry coordinates of the layer tables: 'wgs84' (default), 'web_mercator' or 'tile'.VARCHAR
max_tiles
A layer table scan fails rather than read more than this many tiles.BIGINT
tile_edges
Features reaching a tile edge: 'merge' (default, one row per feature), 'clip' (one non-overlapping piece per tile) or 'raw' (as stored, tile buffers included).VARCHAR

Catalog

Everything mvt exposes once attached — read straight from the worker's released build.

mvt 1 schema
main 6 functions

Vector tile readers and tileset catalogues that return features as GEOMETRY rows.

Schema examples (16)
Examples (16)

Features per layer of the fixture tileset: one row each, joined across tile edges

SELECT layer, geometry_type, count(*) AS features,
  round(sum(ST_Area(geometry)), 1) AS area
FROM mvt.main.read_tiles('https://raw.githubusercontent.com/Query-farm/vgi-mvt/v0.3.1/test/fixtures/edges.pmtiles', zoom := 3)
GROUP BY ALL
ORDER BY ALL;

Tag each city with the region polygon that contains it (NULL when none): a spatial join of two layers

SELECT c.name AS city, r.label AS region
FROM mvt.main.read_tiles('https://raw.githubusercontent.com/Query-farm/vgi-mvt/v0.3.1/test/fixtures/sample.pmtiles', layer := 'cities') AS c
LEFT JOIN mvt.main.read_tiles('https://raw.githubusercontent.com/Query-farm/vgi-mvt/v0.3.1/test/fixtures/sample.pmtiles', layer := 'regions') AS r ON ST_Within(c.geometry, r.geometry)
ORDER BY city;

A spatial filter on the geometry also chooses which tiles are read

SELECT layer, count(*) AS features
FROM mvt.main.read_tiles('https://raw.githubusercontent.com/Query-farm/vgi-mvt/v0.3.1/test/fixtures/sample.pmtiles')
WHERE ST_Intersects(geometry, ST_MakeEnvelope(8, 44, 18, 54))
GROUP BY layer
ORDER BY layer;

Road kinds in the Protomaps Florence sample, read over HTTP range requests

SELECT properties->>'pmap:kind' AS kind, count(*) AS n
FROM mvt.main.read_tiles('https://raw.githubusercontent.com/protomaps/PMTiles/main/spec/v3/protomaps(vector)ODbL_firenze.pmtiles', zoom := 14)
WHERE layer = 'roads'
GROUP BY ALL
ORDER BY n DESC;
Result · first 5 rows
kind n
minor_road 2115
path 946
medium_road 306
major_road 166
other 17
12 more examples

US ZIP code areas touching a Manhattan bbox, from a remote PMTiles archive

SELECT DISTINCT ZCTA5CE10 AS zip
FROM mvt.main.read_tiles('https://r2-public.protomaps.com/protomaps-sample-datasets/cb_2018_us_zcta510_500k.pmtiles', zoom := 7, layer := 'zcta', west := -74.02, south := 40.70, east := -73.93, north := 40.80)
WHERE ST_Intersects(geometry, ST_MakeEnvelope(-74.02, 40.70, -73.93, 40.80))
ORDER BY zip;

Cities from one tile with their attributes as typed columns

SELECT name, geometry
FROM mvt.main.read_mvt('https://raw.githubusercontent.com/Query-farm/vgi-mvt/v0.3.1/test/fixtures/0-0-0.mvt', 0, 0, 0, layer := 'cities')
ORDER BY population DESC;

Feature counts per layer of a Mapbox Streets sample tile

SELECT layer, count(*) AS features
FROM mvt.main.read_mvt('https://raw.githubusercontent.com/mapbox/vector-tile-js/main/test/fixtures/14-8801-5371.vector.pbf', 14, 8801, 5371)
GROUP BY layer
ORDER BY features DESC;

Decode XYZ tile files fetched with read_blob, taking each tile's z/x/y from its URL path

SELECT layer, z, count(*) AS n
FROM mvt.main.mvt_decode((SELECT content AS tile, p[-3]::INTEGER AS z, p[-2]::INTEGER AS x, split_part(p[-1], '.', 1)::INTEGER AS y FROM (SELECT content, string_split(filename, '/') AS p FROM read_blob(['https://raw.githubusercontent.com/Query-farm/vgi-mvt/v0.3.1/test/fixtures/xyz/0/0/0.mvt', 'https://raw.githubusercontent.com/Query-farm/vgi-mvt/v0.3.1/test/fixtures/xyz/1/0/0.mvt', 'https://raw.githubusercontent.com/Query-farm/vgi-mvt/v0.3.1/test/fixtures/xyz/1/1/0.mvt']))))
GROUP BY ALL
ORDER BY ALL;

Attribute names of each layer of the fixture PMTiles archive

SELECT layer, list(field ORDER BY field) AS attributes
FROM mvt.main.tileset_layers('https://raw.githubusercontent.com/Query-farm/vgi-mvt/v0.3.1/test/fixtures/sample.pmtiles')
GROUP BY layer
ORDER BY layer;

Check each declared attribute against the decoded features: how many of its layer's features carry it

SELECT l.layer, l.field, count(*) AS features,
  count(*) FILTER (WHERE t.properties ->> l.field IS NOT NULL) AS with_field
FROM mvt.main.tileset_layers('https://raw.githubusercontent.com/Query-farm/vgi-mvt/v0.3.1/test/fixtures/sample.pmtiles') AS l
JOIN mvt.main.read_tiles('https://raw.githubusercontent.com/Query-farm/vgi-mvt/v0.3.1/test/fixtures/sample.pmtiles') AS t USING (layer)
GROUP BY ALL
ORDER BY ALL;

How many attributes each layer of a remote PMTiles basemap declares

SELECT layer, count(field) AS attributes
FROM mvt.main.tileset_layers('https://raw.githubusercontent.com/protomaps/PMTiles/main/spec/v3/protomaps(vector)ODbL_firenze.pmtiles')
GROUP BY layer
ORDER BY layer;

Name and bounds of the fixture PMTiles archive

SELECT key, value
FROM mvt.main.tileset_metadata('https://raw.githubusercontent.com/Query-farm/vgi-mvt/v0.3.1/test/fixtures/sample.pmtiles')
WHERE key IN ('name', 'bounds')
ORDER BY key;

Compare each layer's feature count in the tilestats metadata with the features read_tiles decodes at max zoom

SELECT s.layer, s.count AS declared, count(t.layer) AS decoded
FROM (SELECT unnest(from_json(value -> 'layers', '[{"layer": "VARCHAR", "count": "BIGINT"}]'), recursive := true) FROM mvt.main.tileset_metadata('https://raw.githubusercontent.com/Query-farm/vgi-mvt/v0.3.1/test/fixtures/sample.pmtiles') WHERE key = 'tilestats') AS s
LEFT JOIN mvt.main.read_tiles('https://raw.githubusercontent.com/Query-farm/vgi-mvt/v0.3.1/test/fixtures/sample.pmtiles') AS t USING (layer)
GROUP BY ALL
ORDER BY ALL;

Max zoom of a remote PMTiles archive (reads only its header)

SELECT value
FROM mvt.main.tileset_metadata('https://r2-public.protomaps.com/protomaps-sample-datasets/cb_2018_us_zcta510_500k.pmtiles')
WHERE key = 'maxzoom';

Write two cities as a one-layer tileset (zoom 0 to 8)

COPY (SELECT 'cities' AS layer, name, population, ST_Point(lon, lat) AS geometry FROM (VALUES ('Rome', 2800000, 12.5, 41.9), ('Paris', 2100000, 2.35, 48.86)) t(name, population, lon, lat)) TO 'cities.pmtiles' (FORMAT 'mvt.pmtiles', maxzoom 8);

Re-tile every layer of an existing tileset into a new archive

COPY (SELECT layer, feature_id, properties, geometry FROM mvt.main.read_tiles('https://raw.githubusercontent.com/Query-farm/vgi-mvt/v0.3.1/test/fixtures/sample.pmtiles')) TO 'sample-copy.pmtiles' (FORMAT 'mvt.pmtiles', maxzoom 4, name 'sample copy');
Table functions
read_mvt (source VARCHAR, z BIGINT, x BIGINT, y BIGINT, coordinates := VARCHAR, layer := VARCHAR) 3 examples

Read the features of one Mapbox Vector Tile (.mvt/.pbf) at a given z/x/y

Positional arguments
source VARCHAR
One vector tile: a .mvt/.pbf file path or http(s) URL (gzip is detected).
z BIGINT
Zoom level of the tile.
x BIGINT
XYZ column of the tile.
y BIGINT
XYZ row of the tile (0 at the north edge).
Named arguments
coordinates := VARCHAR
Output coordinate system: 'wgs84' (longitude/latitude degrees, the default), 'web_mercator' (EPSG:3857 metres) or 'tile' (raw tile pixels, 0..extent from the tile's top-left corner). · default "wgs84"
layer := VARCHAR
Read only this layer. Also adds one typed column per attribute of the layer (from the tileset's vector_layers metadata, or inferred from the tile). NULL reads every layer. · default null
Returns columns
layer VARCHAR
Vector tile layer name.
z INTEGER
Tile zoom level.
x INTEGER
Tile column (XYZ scheme).
y INTEGER
Tile row (XYZ scheme, 0 at the north).
feature_id UBIGINT
Feature id from the tile; NULL if absent.
geometry_type VARCHAR
OGC geometry type of the feature.
geometry GEOMETRY('OGC:CRS84')
Feature geometry: WGS84 lon/lat (OGC:CRS84) by default, EPSG:3857 with coordinates := 'web_mercator', no CRS with 'tile'.
properties JSON
All feature attributes as a JSON object.
layer VARCHAR
Vector tile layer name.
z INTEGER
Tile zoom level.
x INTEGER
Tile column (XYZ scheme).
y INTEGER
Tile row (XYZ scheme, 0 at the north).
feature_id UBIGINT
Feature id from the tile; NULL if absent.
geometry_type VARCHAR
OGC geometry type of the feature.
geometry GEOMETRY('OGC:CRS84')
Feature geometry: WGS84 lon/lat (OGC:CRS84) by default, EPSG:3857 with coordinates := 'web_mercator', no CRS with 'tile'.
properties JSON
All feature attributes as a JSON object.
name VARCHAR
Example: an attribute holding strings.
population BIGINT
Example: an attribute holding whole numbers.
capital BOOLEAN
Example: an attribute holding booleans.
Returns columns · depends on the arguments
layer VARCHAR
Vector tile layer name.
z INTEGER
Tile zoom level.
x INTEGER
Tile column (XYZ scheme).
y INTEGER
Tile row (XYZ scheme, 0 at the north).
feature_id UBIGINT
Feature id from the tile; NULL if absent.
geometry_type VARCHAR
OGC geometry type of the feature.
geometry GEOMETRY('OGC:CRS84')
Feature geometry: WGS84 lon/lat (OGC:CRS84) by default, EPSG:3857 with coordinates := 'web_mercator', no CRS with 'tile'.
properties JSON
All feature attributes as a JSON object.
layer VARCHAR
Vector tile layer name.
z INTEGER
Tile zoom level.
x INTEGER
Tile column (XYZ scheme).
y INTEGER
Tile row (XYZ scheme, 0 at the north).
feature_id UBIGINT
Feature id from the tile; NULL if absent.
geometry_type VARCHAR
OGC geometry type of the feature.
geometry GEOMETRY('OGC:CRS84')
Feature geometry: WGS84 lon/lat (OGC:CRS84) by default, EPSG:3857 with coordinates := 'web_mercator', no CRS with 'tile'.
properties JSON
All feature attributes as a JSON object.
name VARCHAR
Example: an attribute holding strings.
population BIGINT
Example: an attribute holding whole numbers.
capital BOOLEAN
Example: an attribute holding booleans.
Examples (3)

Cities from one tile with their attributes as typed columns

SELECT name, geometry
FROM mvt.main.read_mvt('https://raw.githubusercontent.com/Query-farm/vgi-mvt/v0.3.1/test/fixtures/0-0-0.mvt', 0, 0, 0, layer := 'cities')
ORDER BY population DESC;

Feature counts per layer of a Mapbox Streets sample tile

SELECT layer, count(*) AS features
FROM mvt.main.read_mvt('https://raw.githubusercontent.com/mapbox/vector-tile-js/main/test/fixtures/14-8801-5371.vector.pbf', 14, 8801, 5371)
GROUP BY layer
ORDER BY features DESC;
SELECT geometry_type, count(*) AS n
FROM mvt.main.read_mvt('https://raw.githubusercontent.com/Query-farm/vgi-mvt/v0.3.1/test/fixtures/0-0-0.mvt', 0, 0, 0)
GROUP BY geometry_type
ORDER BY geometry_type;
Link
read_tiles (source VARCHAR, coordinates := VARCHAR, east := DOUBLE, layer := VARCHAR, max_tiles := BIGINT, north := DOUBLE, south := DOUBLE, tile_edges := VARCHAR, west := DOUBLE, zoom := BIGINT) 6 examples

Read the features of a PMTiles, MBTiles or XYZ vector tileset as rows with a GEOMETRY column

Positional arguments
source VARCHAR
Tileset to read: a .pmtiles file or http(s) URL, a local .mbtiles file, or a tile URL template containing {z}, {x} and {y}.
Named arguments
coordinates := VARCHAR
Output coordinate system: 'wgs84' (longitude/latitude degrees, the default), 'web_mercator' (EPSG:3857 metres) or 'tile' (raw tile pixels, 0..extent from the tile's top-left corner). · default "wgs84"
east := DOUBLE
East edge of the area to read, degrees longitude. · default null
layer := VARCHAR
Read only this layer. Also adds one typed column per attribute of the layer (from the tileset's vector_layers metadata, or inferred from the tile). NULL reads every layer. · default null
max_tiles := BIGINT
Fail instead of reading more than this many tiles (a guard for huge archives). · default 10000
north := DOUBLE
North edge of the area to read, degrees latitude. · default null
south := DOUBLE
South edge of the area to read, degrees latitude. · default null
tile_edges := VARCHAR
How features that reach a tile edge come back: 'merge' (the default: one row per feature; points only in the tile that owns them, lines and polygons clipped to their tile and the pieces sharing a feature id joined into one geometry), 'clip' (clipped to each tile, not joined: pieces never overlap) or 'raw' (exactly as stored in the tiles, including the buffer each tile repeats from its neighbours). · default "merge"
west := DOUBLE
West edge of the area to read, degrees longitude. · default null
zoom := BIGINT
Zoom level to read; defaults to the tileset's maxzoom. · default null
Returns columns
layer VARCHAR
Vector tile layer name.
z INTEGER
Tile zoom level.
x INTEGER
Tile column (XYZ scheme).
y INTEGER
Tile row (XYZ scheme, 0 at the north).
feature_id UBIGINT
Feature id from the tile; NULL if absent.
geometry_type VARCHAR
OGC geometry type of the feature.
geometry GEOMETRY('OGC:CRS84')
Feature geometry: WGS84 lon/lat (OGC:CRS84) by default, EPSG:3857 with coordinates := 'web_mercator', no CRS with 'tile'.
properties JSON
All feature attributes as a JSON object.
layer VARCHAR
Vector tile layer name.
z INTEGER
Tile zoom level.
x INTEGER
Tile column (XYZ scheme).
y INTEGER
Tile row (XYZ scheme, 0 at the north).
feature_id UBIGINT
Feature id from the tile; NULL if absent.
geometry_type VARCHAR
OGC geometry type of the feature.
geometry GEOMETRY('OGC:CRS84')
Feature geometry: WGS84 lon/lat (OGC:CRS84) by default, EPSG:3857 with coordinates := 'web_mercator', no CRS with 'tile'.
properties JSON
All feature attributes as a JSON object.
name VARCHAR
Example: a String attribute.
population DOUBLE
Example: a Number attribute.
capital BOOLEAN
Example: a Boolean attribute.
Returns columns · depends on the arguments
layer VARCHAR
Vector tile layer name.
z INTEGER
Tile zoom level.
x INTEGER
Tile column (XYZ scheme).
y INTEGER
Tile row (XYZ scheme, 0 at the north).
feature_id UBIGINT
Feature id from the tile; NULL if absent.
geometry_type VARCHAR
OGC geometry type of the feature.
geometry GEOMETRY('OGC:CRS84')
Feature geometry: WGS84 lon/lat (OGC:CRS84) by default, EPSG:3857 with coordinates := 'web_mercator', no CRS with 'tile'.
properties JSON
All feature attributes as a JSON object.
layer VARCHAR
Vector tile layer name.
z INTEGER
Tile zoom level.
x INTEGER
Tile column (XYZ scheme).
y INTEGER
Tile row (XYZ scheme, 0 at the north).
feature_id UBIGINT
Feature id from the tile; NULL if absent.
geometry_type VARCHAR
OGC geometry type of the feature.
geometry GEOMETRY('OGC:CRS84')
Feature geometry: WGS84 lon/lat (OGC:CRS84) by default, EPSG:3857 with coordinates := 'web_mercator', no CRS with 'tile'.
properties JSON
All feature attributes as a JSON object.
name VARCHAR
Example: a String attribute.
population DOUBLE
Example: a Number attribute.
capital BOOLEAN
Example: a Boolean attribute.
Examples (6)

Features per layer of the fixture tileset: one row each, joined across tile edges

SELECT layer, geometry_type, count(*) AS features,
  round(sum(ST_Area(geometry)), 1) AS area
FROM mvt.main.read_tiles('https://raw.githubusercontent.com/Query-farm/vgi-mvt/v0.3.1/test/fixtures/edges.pmtiles', zoom := 3)
GROUP BY ALL
ORDER BY ALL;

Tag each city with the region polygon that contains it (NULL when none): a spatial join of two layers

SELECT c.name AS city, r.label AS region
FROM mvt.main.read_tiles('https://raw.githubusercontent.com/Query-farm/vgi-mvt/v0.3.1/test/fixtures/sample.pmtiles', layer := 'cities') AS c
LEFT JOIN mvt.main.read_tiles('https://raw.githubusercontent.com/Query-farm/vgi-mvt/v0.3.1/test/fixtures/sample.pmtiles', layer := 'regions') AS r ON ST_Within(c.geometry, r.geometry)
ORDER BY city;

A spatial filter on the geometry also chooses which tiles are read

SELECT layer, count(*) AS features
FROM mvt.main.read_tiles('https://raw.githubusercontent.com/Query-farm/vgi-mvt/v0.3.1/test/fixtures/sample.pmtiles')
WHERE ST_Intersects(geometry, ST_MakeEnvelope(8, 44, 18, 54))
GROUP BY layer
ORDER BY layer;
3 more examples

Road kinds in the Protomaps Florence sample, read over HTTP range requests

SELECT properties->>'pmap:kind' AS kind, count(*) AS n
FROM mvt.main.read_tiles('https://raw.githubusercontent.com/protomaps/PMTiles/main/spec/v3/protomaps(vector)ODbL_firenze.pmtiles', zoom := 14)
WHERE layer = 'roads'
GROUP BY ALL
ORDER BY n DESC;
Result · first 5 rows
kind n
minor_road 2115
path 946
medium_road 306
major_road 166
other 17

US ZIP code areas touching a Manhattan bbox, from a remote PMTiles archive

SELECT DISTINCT ZCTA5CE10 AS zip
FROM mvt.main.read_tiles('https://r2-public.protomaps.com/protomaps-sample-datasets/cb_2018_us_zcta510_500k.pmtiles', zoom := 7, layer := 'zcta', west := -74.02, south := 40.70, east := -73.93, north := 40.80)
WHERE ST_Intersects(geometry, ST_MakeEnvelope(-74.02, 40.70, -73.93, 40.80))
ORDER BY zip;
SELECT layer, count(*) AS features
FROM mvt.main.read_tiles('https://raw.githubusercontent.com/Query-farm/vgi-mvt/v0.3.1/test/fixtures/sample.pmtiles', zoom := 2)
GROUP BY layer
ORDER BY layer;
Link
tileset_layers (source VARCHAR) 4 examples

List the layers of a vector tileset and the attributes (with types) of each

Arguments
source VARCHAR
Tileset: a .pmtiles file or http(s) URL, or a local .mbtiles file.
Returns columns
layer VARCHAR
Layer name.
field VARCHAR
Attribute name.
type VARCHAR
Declared attribute type.
description VARCHAR
Layer description.
minzoom INTEGER
Layer min zoom.
maxzoom INTEGER
Layer max zoom.
Examples (4)

Attribute names of each layer of the fixture PMTiles archive

SELECT layer, list(field ORDER BY field) AS attributes
FROM mvt.main.tileset_layers('https://raw.githubusercontent.com/Query-farm/vgi-mvt/v0.3.1/test/fixtures/sample.pmtiles')
GROUP BY layer
ORDER BY layer;

Check each declared attribute against the decoded features: how many of its layer's features carry it

SELECT l.layer, l.field, count(*) AS features,
  count(*) FILTER (WHERE t.properties ->> l.field IS NOT NULL) AS with_field
FROM mvt.main.tileset_layers('https://raw.githubusercontent.com/Query-farm/vgi-mvt/v0.3.1/test/fixtures/sample.pmtiles') AS l
JOIN mvt.main.read_tiles('https://raw.githubusercontent.com/Query-farm/vgi-mvt/v0.3.1/test/fixtures/sample.pmtiles') AS t USING (layer)
GROUP BY ALL
ORDER BY ALL;

How many attributes each layer of a remote PMTiles basemap declares

SELECT layer, count(field) AS attributes
FROM mvt.main.tileset_layers('https://raw.githubusercontent.com/protomaps/PMTiles/main/spec/v3/protomaps(vector)ODbL_firenze.pmtiles')
GROUP BY layer
ORDER BY layer;
1 more example
SELECT field, type
FROM mvt.main.tileset_layers('https://raw.githubusercontent.com/Query-farm/vgi-mvt/v0.3.1/test/fixtures/sample.pmtiles')
WHERE layer = 'cities'
ORDER BY field;
Link
tileset_metadata (source VARCHAR) 4 examples

Show a vector tileset's metadata (format, zoom range, bounds, attribution, ...) as key/value rows

Arguments
source VARCHAR
Tileset: a .pmtiles file or http(s) URL, or a local .mbtiles file.
Returns columns
key VARCHAR
Metadata key.
value VARCHAR
Metadata value.
Examples (4)

Name and bounds of the fixture PMTiles archive

SELECT key, value
FROM mvt.main.tileset_metadata('https://raw.githubusercontent.com/Query-farm/vgi-mvt/v0.3.1/test/fixtures/sample.pmtiles')
WHERE key IN ('name', 'bounds')
ORDER BY key;

Compare each layer's feature count in the tilestats metadata with the features read_tiles decodes at max zoom

SELECT s.layer, s.count AS declared, count(t.layer) AS decoded
FROM (SELECT unnest(from_json(value -> 'layers', '[{"layer": "VARCHAR", "count": "BIGINT"}]'), recursive := true) FROM mvt.main.tileset_metadata('https://raw.githubusercontent.com/Query-farm/vgi-mvt/v0.3.1/test/fixtures/sample.pmtiles') WHERE key = 'tilestats') AS s
LEFT JOIN mvt.main.read_tiles('https://raw.githubusercontent.com/Query-farm/vgi-mvt/v0.3.1/test/fixtures/sample.pmtiles') AS t USING (layer)
GROUP BY ALL
ORDER BY ALL;

Max zoom of a remote PMTiles archive (reads only its header)

SELECT value
FROM mvt.main.tileset_metadata('https://r2-public.protomaps.com/protomaps-sample-datasets/cb_2018_us_zcta510_500k.pmtiles')
WHERE key = 'maxzoom';
1 more example
SELECT key, value
FROM mvt.main.tileset_metadata('https://raw.githubusercontent.com/Query-farm/vgi-mvt/v0.3.1/test/fixtures/sample.pmtiles')
WHERE key IN ('format', 'minzoom', 'maxzoom')
ORDER BY key;
Link
write_pmtiles (attribution := VARCHAR, buffer := DOUBLE, description := VARCHAR, extent := BIGINT, geometry_column := VARCHAR, id_column := VARCHAR, layer := VARCHAR, layer_column := VARCHAR, max_tile_bytes := BIGINT, max_tiles := BIGINT, maxzoom := BIGINT, maxzoom_column := VARCHAR, minzoom := BIGINT, minzoom_column := VARCHAR, name := VARCHAR, properties_column := VARCHAR, simplify := DOUBLE) 2 examples

COPY ... TO format that encodes rows with a GEOMETRY column as a PMTiles vector tileset

Named arguments
attribution := VARCHAR
Attribution (HTML) in the metadata · default ""
buffer := DOUBLE
Margin around each tile, in pixels of a 256-pixel tile · default 4.0
description := VARCHAR
Tileset description in the metadata · default ""
extent := BIGINT
Tile coordinate grid size (units per tile edge) · default 4096
geometry_column := VARCHAR
GEOMETRY column to encode (default: the only GEOMETRY column) · default ""
id_column := VARCHAR
Column of non-negative feature ids (default: a column named feature_id) · default ""
layer := VARCHAR
Layer name for rows without a layer column (default: the file name without .pmtiles) · default ""
layer_column := VARCHAR
Column holding each row's layer name (default: a column named layer) · default ""
max_tile_bytes := BIGINT
Fail if a compressed tile is larger than this (0: no limit) · default 500000
max_tiles := BIGINT
Fail if the tileset would have more tiles than this · default 5000000
maxzoom := BIGINT
Highest zoom level to write · default 14
maxzoom_column := VARCHAR
Column giving the highest zoom each feature appears at · default ""
minzoom := BIGINT
Lowest zoom level to write · default 0
minzoom_column := VARCHAR
Column giving the lowest zoom each feature appears at · default ""
name := VARCHAR
Tileset name in the metadata (default: the file name) · default ""
properties_column := VARCHAR
Column of JSON objects whose keys become attributes (default: a column named properties) · default ""
simplify := DOUBLE
Line and polygon simplification tolerance, in pixels of a 256-pixel tile (0 turns it off) · default 1.0
Returns columns
Count BIGINT
Rows written (reported by COPY).
Examples (2)

Write two cities as a one-layer tileset (zoom 0 to 8)

COPY (SELECT 'cities' AS layer, name, population, ST_Point(lon, lat) AS geometry FROM (VALUES ('Rome', 2800000, 12.5, 41.9), ('Paris', 2100000, 2.35, 48.86)) t(name, population, lon, lat)) TO 'cities.pmtiles' (FORMAT 'mvt.pmtiles', maxzoom 8);

Re-tile every layer of an existing tileset into a new archive

COPY (SELECT layer, feature_id, properties, geometry FROM mvt.main.read_tiles('https://raw.githubusercontent.com/Query-farm/vgi-mvt/v0.3.1/test/fixtures/sample.pmtiles')) TO 'sample-copy.pmtiles' (FORMAT 'mvt.pmtiles', maxzoom 4, name 'sample copy');
Link
Table in-out functions
mvt_decode (data TABLE, coordinates := VARCHAR, layer := VARCHAR) 2 examples

Decode vector tile BLOBs from a query (tile, z, x, y) into one row per feature

Positional arguments
data TABLE
Rows holding one vector tile each in `tile` (or `tile_data`) with its XYZ address in z, x, y.
Named arguments
coordinates := VARCHAR
Output coordinates: 'wgs84' (default), 'web_mercator' or 'tile' (raw tile pixels). · default "wgs84"
layer := VARCHAR
Decode only this layer; NULL decodes every layer. · default null
Returns columns
layer VARCHAR
Vector tile layer name.
z INTEGER
Tile zoom level.
x INTEGER
Tile column (XYZ scheme).
y INTEGER
Tile row (XYZ scheme, 0 at the north).
feature_id UBIGINT
Feature id from the tile; NULL if absent.
geometry_type VARCHAR
OGC geometry type of the feature.
geometry GEOMETRY('OGC:CRS84')
Feature geometry: WGS84 lon/lat (OGC:CRS84) by default, EPSG:3857 with coordinates := 'web_mercator', no CRS with 'tile'.
properties JSON
All feature attributes as a JSON object.
Examples (2)

Decode XYZ tile files fetched with read_blob, taking each tile's z/x/y from its URL path

SELECT layer, z, count(*) AS n
FROM mvt.main.mvt_decode((SELECT content AS tile, p[-3]::INTEGER AS z, p[-2]::INTEGER AS x, split_part(p[-1], '.', 1)::INTEGER AS y FROM (SELECT content, string_split(filename, '/') AS p FROM read_blob(['https://raw.githubusercontent.com/Query-farm/vgi-mvt/v0.3.1/test/fixtures/xyz/0/0/0.mvt', 'https://raw.githubusercontent.com/Query-farm/vgi-mvt/v0.3.1/test/fixtures/xyz/1/0/0.mvt', 'https://raw.githubusercontent.com/Query-farm/vgi-mvt/v0.3.1/test/fixtures/xyz/1/1/0.mvt']))))
GROUP BY ALL
ORDER BY ALL;
SELECT layer, feature_id, ST_AsText(geometry) AS wkt
FROM mvt.main.mvt_decode((SELECT from_hex('1a2e78020a06706f696e7473120f0807120200001801220509802080201a046e616d6522080a066f726967696e288020') AS tile, 0 AS z, 0 AS x, 0 AS y))
ORDER BY layer;
Link

COPY formats

Attaching mvt also registers a file format for DuckDB's own COPY statement, so you can write query results out to files without a function call. The FORMAT name starts with the ATTACH alias — mvt with the Quick start above; use yours if you attach it under another name.

FORMAT 'mvt.pmtiles'

COPY … TO · export

Write PMTiles. Vector tiles (MVT) in a PMTiles v3 archive

copy.sql
-- Write two cities as a one-layer tileset (zoom 0 to 8)
COPY (SELECT 'cities' AS layer, name, population, ST_Point(lon, lat) AS geometry FROM (VALUES ('Rome', 2800000, 12.5, 41.9), ('Paris', 2100000, 2.35, 48.86)) t(name, population, lon, lat)) TO 'cities.pmtiles' (FORMAT 'mvt.pmtiles', maxzoom 8)
copy.sql
-- Re-tile every layer of an existing tileset into a new archive
COPY (SELECT layer, feature_id, properties, geometry FROM mvt.main.read_tiles('https://raw.githubusercontent.com/Query-farm/vgi-mvt/v0.3.1/test/fixtures/sample.pmtiles')) TO 'sample-copy.pmtiles' (FORMAT 'mvt.pmtiles', maxzoom 4, name 'sample copy')

Options

Pass these inside the COPY statement's parentheses, after FORMAT. Unknown options are rejected.

attributionVARCHAR
Attribution (HTML) in the metadata
bufferDOUBLE
Margin around each tile, in pixels of a 256-pixel tile
descriptionVARCHAR
Tileset description in the metadata
extentBIGINT
Tile coordinate grid size (units per tile edge)
geometry_columnVARCHAR
GEOMETRY column to encode (default: the only GEOMETRY column)
id_columnVARCHAR
Column of non-negative feature ids (default: a column named feature_id)
layerVARCHAR
Layer name for rows without a layer column (default: the file name without .pmtiles)
layer_columnVARCHAR
Column holding each row's layer name (default: a column named layer)
max_tile_bytesBIGINT
Fail if a compressed tile is larger than this (0: no limit)
max_tilesBIGINT
Fail if the tileset would have more tiles than this
maxzoomBIGINT
Highest zoom level to write
maxzoom_columnVARCHAR
Column giving the highest zoom each feature appears at
minzoomBIGINT
Lowest zoom level to write
minzoom_columnVARCHAR
Column giving the lowest zoom each feature appears at
nameVARCHAR
Tileset name in the metadata (default: the file name)
properties_columnVARCHAR
Column of JSON objects whose keys become attributes (default: a column named properties)
simplifyDOUBLE
Line and polygon simplification tolerance, in pixels of a 256-pixel tile (0 turns it off)
Full reference

Encode a query's rows as Mapbox Vector Tiles in a PMTiles archive. One row is one feature; a layer column puts rows in different layers of the same tiles, so a single COPY of a UNION ALL writes a whole multi-layer basemap.

Try these queries

Features per layer of the fixture tileset: one row each, joined across tile edges

SELECT layer, geometry_type, count(*) AS features,
  round(sum(ST_Area(geometry)), 1) AS area
FROM mvt.main.read_tiles('https://raw.githubusercontent.com/Query-farm/vgi-mvt/v0.3.1/test/fixtures/edges.pmtiles', zoom := 3)
GROUP BY ALL
ORDER BY ALL

Uses read_tiles

Cities from one tile with their attributes as typed columns

SELECT name, geometry
FROM mvt.main.read_mvt('https://raw.githubusercontent.com/Query-farm/vgi-mvt/v0.3.1/test/fixtures/0-0-0.mvt', 0, 0, 0, layer := 'cities')
ORDER BY population DESC

Uses read_mvt

Decode XYZ tile files fetched with read_blob, taking each tile's z/x/y from its URL path

SELECT layer, z, count(*) AS n
FROM mvt.main.mvt_decode((SELECT content AS tile, p[-3]::INTEGER AS z, p[-2]::INTEGER AS x, split_part(p[-1], '.', 1)::INTEGER AS y FROM (SELECT content, string_split(filename, '/') AS p FROM read_blob(['https://raw.githubusercontent.com/Query-farm/vgi-mvt/v0.3.1/test/fixtures/xyz/0/0/0.mvt', 'https://raw.githubusercontent.com/Query-farm/vgi-mvt/v0.3.1/test/fixtures/xyz/1/0/0.mvt', 'https://raw.githubusercontent.com/Query-farm/vgi-mvt/v0.3.1/test/fixtures/xyz/1/1/0.mvt']))))
GROUP BY ALL
ORDER BY ALL

Uses mvt_decode

Attribute names of each layer of the fixture PMTiles archive

SELECT layer, list(field ORDER BY field) AS attributes
FROM mvt.main.tileset_layers('https://raw.githubusercontent.com/Query-farm/vgi-mvt/v0.3.1/test/fixtures/sample.pmtiles')
GROUP BY layer
ORDER BY layer

Uses tileset_layers

Name and bounds of the fixture PMTiles archive

SELECT key, value
FROM mvt.main.tileset_metadata('https://raw.githubusercontent.com/Query-farm/vgi-mvt/v0.3.1/test/fixtures/sample.pmtiles')
WHERE key IN ('name', 'bounds')
ORDER BY key

Uses tileset_metadata

Write two cities as a one-layer tileset (zoom 0 to 8)

COPY (SELECT 'cities' AS layer, name, population, ST_Point(lon, lat) AS geometry FROM (VALUES ('Rome', 2800000, 12.5, 41.9), ('Paris', 2100000, 2.35, 48.86)) t(name, population, lon, lat)) TO 'cities.pmtiles' (FORMAT 'mvt.pmtiles', maxzoom 8)

14 more examples in the Catalog, each listed under the table or function it uses.

Vector tiles (MVT) is an independent open-source connector from Query.Farm. It is not affiliated with, endorsed by or sponsored by the services, projects or organisations it works with. Product names, logos and trademarks belong to their respective owners.