ST_AsGeoJSON
Name
ST_AsGeoJSON — Return the geometry as a GeoJSON element.
Synopsis
text
ST_AsGeoJSON
(
record
feature
, text
geomcolumnname
, integer
maxdecimaldigits=9
, boolean
pretty_bool=false
)
;
text
ST_AsGeoJSON
(
geometry
geom
, integer
maxdecimaldigits=9
, integer
options=8
)
;
text
ST_AsGeoJSON
(
geography
geog
, integer
maxdecimaldigits=9
, integer
options=0
)
;
Description
Return the geometry as a GeoJSON "geometry" object, or the row as a GeoJSON "feature" object. (Cf GeoJSON specifications RFC 7946 ). 2D and 3D Geometries are both supported. GeoJSON only support SFS 1.1 geometry types (no curve support for example).
The
maxdecimaldigits
argument may be used to reduce the maximum number of decimal places used in output (defaults to 9). If you are using EPSG:4326 and are outputting the geometry only for display,
maxdecimaldigits
=6 can be a good choice for many maps.
The
options
argument could be used to add BBOX or CRS in GeoJSON output:
-
0: means no option
-
1: GeoJSON BBOX
-
2: GeoJSON Short CRS (e.g EPSG:4326)
-
4: GeoJSON Long CRS (e.g urn:ogc:def:crs:EPSG::4326)
-
8: GeoJSON Short CRS if not EPSG:4326 (default)
Availability: 1.3.4
Availability: 1.5.0 geography support was introduced.
Changed: 2.0.0 support default args and named args.
Changed: 3.0.0 support records as input
Changed: 3.0.0 output SRID if not EPSG:4326.
This function supports 3d and will not drop the z-index.
Examples
GeoJSON format is popular among web mapping frameworks.
You can test and view your GeoJSON data online on geojson.io .
To build FeatureCollection:
select json_build_object( 'type', 'FeatureCollection', 'features', json_agg(ST_AsGeoJSON(t.*)::json) ) from ( values (1, 'one', 'POINT(1 1)'::geometry), (2, 'two', 'POINT(2 2)'), (3, 'three', 'POINT(3 3)') ) as t(id, name, geom);
{"type" : "FeatureCollection", "features" : [{"type": "Feature", "geometry": {"type":"Point","coordinates":[1,1]}, "properties": {"id": 1, "name": "one"}}, {"type": "Feature", "geometry": {"type":"Point","coordinates":[2,2]}, "properties": {"id": 2, "name": "two"}}, {"type": "Feature", "geometry": {"type":"Point","coordinates":[3,3]}, "properties": {"id": 3, "name": "three"}}]}
To get Features as records:
SELECT ST_AsGeoJSON(t.*) FROM (VALUES (1, 'one', 'POINT(1 1)'::geometry), (2, 'two', 'POINT(2 2)'), (3, 'three', 'POINT(3 3)')) AS t(id, name, geom);
st_asgeojson ----------------------------------------------------------------------------------------------------------------- {"type": "Feature", "geometry": {"type":"Point","coordinates":[1,1]}, "properties": {"id": 1, "name": "one"}} {"type": "Feature", "geometry": {"type":"Point","coordinates":[2,2]}, "properties": {"id": 2, "name": "two"}} {"type": "Feature", "geometry": {"type":"Point","coordinates":[3,3]}, "properties": {"id": 3, "name": "three"}}
Don't forget to transform your data to WGS84 longitude, latitude to conform with RFC7946:
SELECT ST_AsGeoJSON(ST_Transform(geom,4326)) from fe_edges limit 1;
st_asgeojson ----------------------------------------------------------------------------------------------------------- {"type":"MultiLineString","coordinates":[[[-89.734634999999997,31.492072000000000], [-89.734955999999997,31.492237999999997]]]} (1 row)
You can also use it with 3D geometries:
SELECT ST_AsGeoJSON('LINESTRING(1 2 3, 4 5 6)');
{"type":"LineString","coordinates":[[1,2,3],[4,5,6]]}