ST_AsGeoJSON — Return the geometry as a GeoJSON element.


text ST_AsGeoJSON(geometry g1);

text ST_AsGeoJSON(geometry g1, integer max_decimal_digits);

text ST_AsGeoJSON(geometry g1, integer max_decimal_digits, integer options);

text ST_AsGeoJSON(integer version, geometry g1);

text ST_AsGeoJSON(integer version, geometry g1, integer max_decimal_digits);

text ST_AsGeoJSON(integer version, geometry g1, integer max_decimal_digits, integer options);


Return the geometry as a Geometry Javascript Object Notation (GeoJSON) element. (Cf GeoJSON specifications 1.0). 2D and 3D Geometries are both supported. GeoJSON only support SFS 1.1 geometry type (no curve support for example).

The version parameter, if specified, must be 1.

The third argument may be used to reduce the maximum number of decimal places used in output (defaults to 15).

The last 'options' argument could be used to add Bbox or Crs in GeoJSON output:

  • 0: means no option (default value)

  • 1: GeoJSON Bbox

  • 2: GeoJSON Short CRS (e.g EPSG:4326)

  • 4: GeoJSON Long CRS (e.g urn:ogc:def:crs:EPSG::4326)

Version 1: ST_AsGeoJSON(geom) / precision=15 version=1 options=0

Version 2: ST_AsGeoJSON(geom, precision) / version=1 options=0

Version 3: ST_AsGeoJSON(geom, precision, options) / version=1

Version 4: ST_AsGeoJSON(version, geom) / precision=15 options=0

Version 5: ST_AsGeoJSON(version, geom, precision) /options=0

Version 6: ST_AsGeoJSON(version, geom, precision,options)

Availability: 1.3.4

This function supports 3d and will not drop the z-index.


GeoJSON format is generally more efficient than other formats for use in ajax mapping. One popular javascript client that supports this is Open Layers. Example of its use is OpenLayers GeoJSON Example

SELECT ST_AsGeoJSON(the_geom) from fe_edges limit 1;

(1 row)
--3d point
SELECT ST_AsGeoJSON('LINESTRING(1 2 3, 4 5 6)');