ST_AsGeoJSON
Name
ST_AsGeoJSON — Return the geometry as a GeoJSON element.
Synopsis
text
ST_AsGeoJSON
(
geometry
geom
, integer
maxdecimaldigits=15
, integer
options=0
)
;
text
ST_AsGeoJSON
(
geography
geog
, integer
maxdecimaldigits=15
, integer
options=0
)
;
text
ST_AsGeoJSON
(
integer
gj_version
, geometry
geom
, integer
maxdecimaldigits=15
, integer
options=0
)
;
text
ST_AsGeoJSON
(
integer
gj_version
, geography
geog
, integer
maxdecimaldigits=15
, integer
options=0
)
;
Description
Return the geometry as a 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
gj_version
parameter is the major version of the GeoJSON spec. If specified, must be 1. This represents the spec version of GeoJSON.
The third argument may be used to reduce the maximum number of decimal places used in output (defaults to 15). 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 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) / maxdecimaldigits=15 version=1 options=0
Version 2: ST_AsGeoJSON(geom, maxdecimaldigits) / version=1 options=0
Version 3: ST_AsGeoJSON(geom, maxdecimaldigits, options) / version=1
Version 4: ST_AsGeoJSON(gj_version, geom) / maxdecimaldigits=15 options=0
Version 5: ST_AsGeoJSON(gj_version, geom, maxdecimaldigits) / options=0
Version 6: ST_AsGeoJSON(gj_version, geom, maxdecimaldigits, options)
Availability: 1.3.4
Availability: 1.5.0 geography support was introduced.
Changed: 2.0.0 support default args and named args.
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 .
ST_AsGeoJSON only builds geometry. You need to build the rest of Feature from your Postgres table yourself:
select row_to_json(fc) from ( select 'FeatureCollection' as "type", array_to_json(array_agg(f)) as "features" from ( select 'Feature' as "type", ST_AsGeoJSON(ST_Transform(way, 4326), 6) :: json as "geometry", ( select json_strip_nulls(row_to_json(t)) from ( select osm_id, "natural", place ) t ) as "properties" from planet_osm_point where "natural" is not null or place is not null limit 10 ) as f ) as fc; st_asgeojson ----------------------------------------------------------------------------------------------------------- {"type":"FeatureCollection","features":[{"type":"Feature","geometry":{"type":"Point","coordinates":[23.569251,51.541599]},"properties":{"osm_id":3424148658,"place":"locality"}},{"type":"Feature","geometry":{"type":"Point","coordinates":[23.625174,51.511718]},"properties":{"osm_id":4322036818,"place":"locality"}},{"type":"Feature","geometry":{"type":"Point","coordinates":[23.613928,51.5417]},"properties":{"osm_id":242979330,"place":"hamlet"}},{"type":"Feature","geometry":{"type":"Point","coordinates":[23.586361,51.563272]},"properties":{"osm_id":3424148656,"place":"locality"}},{"type":"Feature","geometry":{"type":"Point","coordinates":[23.605488,51.553886]},"properties":{"osm_id":242979323,"place":"village"}},{"type":"Feature","geometry":{"type":"Point","coordinates":[23.6067,51.57609]},"properties":{"osm_id":242979327,"place":"village"}},{"type":"Feature","geometry":{"type":"Point","coordinates":[23.636533,51.575683]},"properties":{"osm_id":5737800420,"place":"locality"}},{"type":"Feature","geometry":{"type":"Point","coordinates":[23.656733,51.518733]},"properties":{"osm_id":5737802397,"place":"locality"}},{"type":"Feature","geometry":{"type":"Point","coordinates":[23.672542,51.504584]},"properties":{"osm_id":242979320,"place":"hamlet"}},{"type":"Feature","geometry":{"type":"Point","coordinates":[23.574094,51.63389]},"properties":{"osm_id":242979333,"place":"village"}}]}
SELECT ST_AsGeoJSON(geom) 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)'); st_asgeojson ----------------------------------------------------------------------------------------- {"type":"LineString","coordinates":[[1,2,3],[4,5,6]]}