Returns the geometry boundary of a grid cell as a PostGIS geometry object.
Syntax
geometry ST_AsGeometry(geomgrid grid);
geometry[] ST_AsGeometry(geomgrid[] grid);
geometry ST_AsGeometry(h3grid grid);Parameters
| Parameter | Description |
|---|---|
grid | The grid cell to convert. Supports GeomGrid and H3Grid data types. |
Examples
Example 1: Convert a GeomGrid cell to its polygon boundary
-- Convert a GeomGrid cell encoded as text to a WKT polygon
SELECT ST_AsText(
ST_AsGeometry(ST_GridFromText('G0013103220310313')));Output:
POLYGON((116.458888888889 39.3088888888889,116.458888888889 39.3166666666667,116.466666666667 39.3166666666667,116.466666666667 39.3088888888889,116.458888888889 39.3088888888889))Example 2: Convert 3D grid cells to their geometry boundaries
-- Convert a 3D polygon (with a hole) to GeomGrid cells at resolution 8,
-- then retrieve the geometry boundary of each cell as a GEOMETRYCOLLECTION Z
SELECT ST_AsText(unnest(ST_AsGeometry(ST_As3DGrid(
'srid=4490;POLYGON Z ((116 39 1000,116.12 39 2000,116.12 39.09 2000,116 39.09 2000,116 39 1000),(116.024 39.018 5000,116.024 39.045 4000,116.06 39.045 3000,116.06 39.018 3000,116.024 39.018 5000))'::geometry,8))));Output (one row per grid cell, each as a GEOMETRYCOLLECTION Z with six bounding faces):
GEOMETRYCOLLECTION Z (POLYGON Z ((114 38 0,114 40 0,116 40 0,116 38 0,114 38 0)),POLYGON Z ((114 38 224581.873222534,116 38 224581.873222534,116 40 224581.873222534,114 40 224581.873222534,114 38 224581.873222534)),POLYGON Z ((114 38 0,114 38 224581.873222534,114 40 224581.873222534,114 40 0,114 38 0)),POLYGON Z ((116 38 0,116 40 0,116 40 224581.873222534,116 38 224581.873222534,116 38 0)),POLYGON Z ((114 38 0,116 38 0,116 38 224581.873222534,114 38 224581.873222534,114 38 0)),POLYGON Z ((114 40 0,114 40 224581.873222534,116 40 224581.873222534,116 40 0,114 40 0)))
GEOMETRYCOLLECTION Z (POLYGON Z ((116 38 0,116 40 0,118 40 0,118 38 0,116 38 0)),POLYGON Z ((116 38 224581.873222534,118 38 224581.873222534,118 40 224581.873222534,116 40 224581.873222534,116 38 224581.873222534)),POLYGON Z ((116 38 0,116 38 224581.873222534,116 40 224581.873222534,116 40 0,116 38 0)),POLYGON Z ((118 38 0,118 40 0,118 40 224581.873222534,118 38 224581.873222534,118 38 0)),POLYGON Z ((116 38 0,118 38 0,118 38 224581.873222534,116 38 224581.873222534,116 38 0)),POLYGON Z ((116 40 0,116 40 224581.873222534,118 40 224581.873222534,118 40 0,116 40 0)))Example 3: Convert an H3 cell to its polygon boundary
-- Get the boundary of an H3 cell at resolution 8
SELECT ST_AsText(ST_AsGeometry(ST_H3FromLatLng(20.5, 128.2, 8)));Output:
POLYGON((128.191668807576 20.5021234642116,128.190846860853 20.4973493612535,128.194924841007 20.4945095494225,128.19982492147 20.4964439288771,128.200646976708 20.5012181556083,128.196568842975 20.5040578791202,128.191668807576 20.5021234642116))Related functions
ST_GridFromText— Creates a GeomGrid cell from its text encodingST_As3DGrid— Creates 3D GeomGrid cells from a 3D geometryST_H3FromLatLng— Creates an H3Grid cell from latitude, longitude, and resolutionST_AsText— Converts a geometry to Well-Known Text (WKT) format