Returns a geometry or geography object as a GeoJSON element.
Syntax
text ST_AsGeoJSON(geometry geom, integer maxdecimaldigits, integer options);
text ST_AsGeoJSON(geography geog, integer maxdecimaldigits, integer options);
text ST_AsGeoJSON(integer gjVersion, geometry geom, integer maxdecimaldigits, integer options);
text ST_AsGeoJSON(integer gjVersion, geography geog, integer maxdecimaldigits, integer options);Parameters
| Parameter | Description |
|---|---|
geom | The geometry object to convert. |
geog | The geography object to convert. |
maxdecimaldigits | Maximum number of decimal places in the output. Default value: 15. |
options | Controls additional fields in the GeoJSON output. Default value: 0. See Options values. |
gjVersion | Version of the GeoJSON specification. If specified, set to 1. |
Options values
| Value | Description |
|---|---|
0 | No additional fields (default). |
1 | Include the bbox member. |
2 | Include the coordinate reference system (CRS) using a short expression, for example, EPSG:4326. |
4 | Include the CRS using a long expression, for example, urn:ogc:def:crs:EPSG::4326. |
Usage notes
ST_AsGeoJSON supports 3D objects and preserves the Z coordinate of the input geometry.
Examples
Basic usage: convert a geometry to GeoJSON
SELECT ST_AsGeoJSON('LINESTRING(1 2, 3 4)');Output:
st_asgeojson
---------------------------------------------------
{
"type":"LineString",
"coordinates":[[1,2],[3,4]]
}
(1 row)