Converts a geographic grid object (geomgrid or H3) into a geometry in the tile coordinate system, ready for use with ST_AsMVT to generate Mapbox Vector Tile (MVT) output.
Syntax
geometry ST_AsMVTGeom(geomgrid grid, geometry bounds, int4 extent, int4 buffer, boolean clip_geom);
geometry ST_AsMVTGeom(h3grid grid, geometry bounds, int4 extent, int4 buffer, boolean clip_geom);Return value
Returns a geometric object in a coordinate system.
Parameters
| Parameter | Description | Default |
|---|---|---|
grid | The geomgrid or H3 geographic grid object to output to MVT. | — |
bounds | The rectangular boundary of the tile, excluding the buffer. Must be a geometry type (not Box2D). | — |
extent | The tile size in the tile coordinate system. | 4096 |
buffer | The buffer size in the tile coordinate system. | 256 |
clip_geom | Specifies whether to clip the geometry at the tile boundary. | true |
Usage notes
The
boundsparameter determines the output coordinate system. Use ST_TileEnvelope to generatebounds— it sets the correct coordinate system automatically. If you generateboundsby another method, specify the Spatial Reference Identifier (SRID) explicitly.Unlike the standard PostGIS ST_AsMVTGeom (which takes a geometry input), this variant accepts a geomgrid or h3grid as the first argument. The
boundsparameter type is geometry, not Box2D.
Examples
Convert a geomgrid to tile geometry
Use ST_TileEnvelope to generate the tile boundary (SRID is set automatically):
SELECT st_asmvtgeom('010200040000'::geomgrid, st_tileenvelope(0, 0, 0));Output:
0103000020110F000001000000050000000000000000D8A2400000000000FC99400000000000D8A240000000000000A040000000000000A040000000000000A040000000000000A0400000000000FC99400000000000D8A2400000000000FC9940Pass a transformed tile envelope when working in SRID 4326:
SELECT st_asmvtgeom('010200040000'::geomgrid, st_transform(st_tileenvelope(0, 0, 0), 4326));Output:
0103000020E610000001000000050000000000000000D8A2400000000000F493400000000000D8A240000000000000A040000000000000A040000000000000A040000000000000A0400000000000F493400000000000D8A2400000000000F49340Override the default extent, buffer, and clipping behavior:
SELECT st_asmvtgeom('010200040000'::geomgrid, st_transform(st_tileenvelope(0, 0, 0), 4326), 1024, 128, false);Output:
0103000020E610000001000000050000000000000000D882400000000000F073400000000000D8824000000000000080400000000000008040000000000000804000000000000080400000000000F073400000000000D882400000000000F07340Convert an H3 grid cell to tile geometry
Use ST_TileEnvelope as the tile boundary:
SELECT st_asmvtgeom(st_h3fromlatlng(20.5, 128.2, 5), st_tileenvelope(0, 0, 0));Output:
0103000020110F00000100000006000000000000000064AB400000000000449C40000000000066AB400000000000489C40000000000066AB4000000000004C9C40000000000062AB4000000000004C9C40000000000062AB400000000000489C40000000000064AB400000000000449C40Specify the SRID explicitly when the bounds geometry is not generated by ST_TileEnvelope:
SELECT st_asmvtgeom(st_h3fromlatlng(20.5, 128.2, 5), 'SRID=4326;POLYGON((-180 -85,-180 85, 180 85, 180 -85, -180 -85))');Output:
0103000020E61000000100000007000000000000000064AB400000000000449840000000000066AB400000000000489840000000000066AB400000000000509840000000000064AB400000000000549840000000000062AB400000000000549840000000000062AB4000000000004C9840000000000064AB40000000000044984