Converts a geographic grid object (geomgrid or H3) into a geometry in the tile coordinate system, ready for use in 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 geometry object in the tile coordinate system.
Parameters
| Parameter | Type | Description | Default |
|---|---|---|---|
grid | geomgrid | h3grid | The geographic grid object to convert for MVT output. | — |
bounds | geometry | The rectangular boundary of the tile, excluding the buffer. Unlike the standard ST_AsMVTGeom (which takes a Box2D), this variant accepts a geometry. | — |
extent | int4 | The tile size in the tile coordinate system. | 4096 |
buffer | int4 | The buffer size in the tile coordinate system. | 256 |
clip_geom | boolean | Specifies whether to clip the grid geometry at the tile boundary. | true |
Usage notes
The output coordinate system is determined by the
boundsparameter. UseST_TileEnvelopeto generatebounds.If you generate
boundsby another method, specify the Spatial Reference Identifier (SRID) explicitly (see examples 2 and 5 below).
Examples
Example 1: Convert a geomgrid object using ST_TileEnvelope as bounds.
SELECT st_asmvtgeom('010200040000'::geomgrid, st_tileenvelope(0, 0, 0));Output:
0103000020110F000001000000050000000000000000D8A2400000000000FC99400000000000D8A240000000000000A040000000000000A040000000000000A040000000000000A0400000000000FC99400000000000D8A2400000000000FC9940Example 2: Convert a geomgrid object with bounds transformed to SRID 4326.
SELECT st_asmvtgeom('010200040000'::geomgrid, st_transform(st_tileenvelope(0, 0, 0), 4326));Output:
0103000020E610000001000000050000000000000000D8A2400000000000F493400000000000D8A240000000000000A040000000000000A040000000000000A040000000000000A0400000000000F493400000000000D8A2400000000000F49340Example 3: Convert a geomgrid object with custom extent, buffer, and clipping disabled.
SELECT st_asmvtgeom('010200040000'::geomgrid, st_transform(st_tileenvelope(0, 0, 0), 4326), 1024, 128, false);Output:
0103000020E610000001000000050000000000000000D882400000000000F073400000000000D8824000000000000080400000000000008040000000000000804000000000000080400000000000F073400000000000D882400000000000F07340Example 4: Convert an H3 grid object using ST_TileEnvelope as bounds.
SELECT st_asmvtgeom(st_h3fromlatlng(20.5, 128.2, 5), st_tileenvelope(0, 0, 0));Output:
0103000020110F00000100000006000000000000000064AB400000000000449C40000000000066AB400000000000489C40000000000066AB4000000000004C9C40000000000062AB4000000000004C9C40000000000062AB400000000000489C40000000000064AB400000000000449C40Example 5: Convert an H3 grid object with an explicit SRID polygon as bounds.
SELECT st_asmvtgeom(st_h3fromlatlng(20.5, 128.2, 5), 'SRID=4326;POLYGON((-180 -85,-180 85, 180 85, 180 -85, -180 -85))');Output:
0103000020E61000000100000007000000000000000064AB400000000000449840000000000066AB400000000000489840000000000066AB400000000000509840000000000064AB400000000000549840000000000062AB400000000000549840000000000062AB4000000000004C9840000000000064AB400000000044984