All Products
Search
Document Center

PolarDB:ST_AsGeoJSON

Last Updated:Mar 28, 2026

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

ParameterDescription
geomThe geometry object to convert.
geogThe geography object to convert.
maxdecimaldigitsMaximum number of decimal places in the output. Default value: 15.
optionsControls additional fields in the GeoJSON output. Default value: 0. See Options values.
gjVersionVersion of the GeoJSON specification. If specified, set to 1.

Options values

ValueDescription
0No additional fields (default).
1Include the bbox member.
2Include the coordinate reference system (CRS) using a short expression, for example, EPSG:4326.
4Include 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)