Transforms a 3D geometry into the tile coordinate system required by ST_AsMVT3D to generate Mapbox Vector Tile (MVT) output. This function extends ST_AsMVTGeom with z-axis support, preserving z-coordinate values during the coordinate transformation.
Syntax
geometry ST_AsMVTGeom3D(
geometry geom,
box2d bounds,
integer extent = 4096,
integer buffer = 256,
boolean clip_geom = true
);Parameters
| Parameter | Type | Description | Default | Valid values |
|---|---|---|---|---|
geom | geometry | The 3D geometry object to transform. | — | — |
bounds | box2d | The rectangular boundaries of the tile in the target coordinate reference system (CRS), excluding the buffer. Use ST_TileEnvelope to generate this value. | — | — |
extent | integer | The tile size in tile coordinate space, as defined by the MVT specification. | 4096 | 256–8192 |
buffer | integer | The buffer size in tile coordinate space for geometry clipping. | 256 | 1–4096 |
clip_geom | boolean | Specifies whether to clip the 3D geometry to tile boundaries. | true | — |
Description
ST_AsMVTGeom3D prepares 3D geometry for use with ST_AsMVT3D. It transforms geometry coordinates into tile space and clips geometries to tile boundaries when clip_geom is true.
Key behaviors:
Transforms z-axis data along with x and y coordinates, preserving the full 3D geometry through the tile conversion process.
Requires tile boundaries in the same CRS as the input geometry so the geometry can be correctly transformed and clipped. Use
ST_TileEnvelopeto generate boundaries.Does not support 3D polygon objects that contain interior rings.
Examples
Transform a 3D line string into tile coordinate space
The following example transforms a 3D line string from SRID 4326 to Web Mercator (SRID 3857) and converts it into tile coordinate space for tile (1, 0, 0).
SELECT ST_AsText(
ST_AsMVTGeom3D(
ST_Transform('SRID=4326; LINESTRING(-10 -10 30, -10 -20 30)'::geometry, 3857),
ST_TileEnvelope(1, 0, 0)
)
) AS geom;Expected output:
geom
------------------------------------------------------------------------------------
MULTILINESTRING Z ((3868.44444444444 4324.7197219642 30,3868.44444444444 4352 30))
(1 row)Generate a 3D MVT tile using ST_AsMVT3D
The following example shows a complete tile-generation query using a CTE. It transforms geometries from a table into tile coordinate space and aggregates them into binary MVT format for tile (12, 513, 412).
WITH mvtgeom AS (
SELECT ST_AsMVTGeom3D(
ST_Transform(geom, 3857),
ST_TileEnvelope(12, 513, 412),
extent => 4096,
buffer => 256
) AS geom
FROM your_3d_table
WHERE geom && ST_Transform(ST_TileEnvelope(12, 513, 412), 4326)
)
SELECT ST_AsMVT3D(mvtgeom.*)
FROM mvtgeom;Replace your_3d_table with the name of your table containing 3D geometry data.
See also
ST_AsMVT3D — aggregates tile geometries into binary MVT format
ST_AsMVTGeom — the 2D PostGIS function this extends
ST_TileEnvelope — generates tile boundary boxes