Returns the GeoJSON representation of a geometry or geography object.
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 to retain in the output coordinates. Default value: 15. |
options | Controls what additional members to include in the GeoJSON output. Valid values: 0 (default), 1, 2, 4. See the options table below for details. |
gjVersion | The GeoJSON specification version. If specified, set to 1. |
Options values
| Value | Description |
|---|---|
0 | No additional members. This is the default. |
1 | Include the bbox (bounding box) 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
Supports 3D objects. The Z coordinate of the input geometry is preserved in the output.
Examples
Convert a geometry object to GeoJSON
SELECT ST_AsGeoJSON('LINESTRING(1 2, 3 4)');
st_asgeojson
---------------------------------------------------
{
"type":"LineString",
"coordinates":[[1,2],[3,4]]
}
(1 row)Related topics
ST_GeomFromGeoJSON: Parse a GeoJSON string back into a geometry object.