[postgis-tickets] r17455 - [doc] ST_AsGeoJSON FeatureCollection example
Darafei
komzpa at gmail.com
Sat Jun 1 10:32:21 PDT 2019
Author: komzpa
Date: 2019-06-01 10:32:20 -0700 (Sat, 01 Jun 2019)
New Revision: 17455
Modified:
trunk/doc/reference_output.xml
Log:
[doc] ST_AsGeoJSON FeatureCollection example
Modified: trunk/doc/reference_output.xml
===================================================================
--- trunk/doc/reference_output.xml 2019-06-01 16:24:53 UTC (rev 17454)
+++ trunk/doc/reference_output.xml 2019-06-01 17:32:20 UTC (rev 17455)
@@ -613,32 +613,42 @@
</para>
<para>You can test and view your GeoJSON data online on <ulink url="http://geojson.io/">geojson.io</ulink>.</para>
+<para>To build FeatureCollection:</para>
+<programlisting>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);</programlisting>
+<screen>{"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"}}]}</screen>
+
+<para>To get Features as records:</para>
<programlisting>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
+AS t(id, name, geom);</programlisting>
+<screen> 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"}}
-</programlisting>
+</screen>
- <programlisting>SELECT ST_AsGeoJSON(geom) from fe_edges limit 1;
- st_asgeojson
+<para>Don't forget to transform your data to WGS84 longitude, latitude to conform with RFC7946:</para>
+ <programlisting>SELECT ST_AsGeoJSON(ST_Transform(geom,4326)) from fe_edges limit 1;</programlisting>
+<screen> st_asgeojson
-----------------------------------------------------------------------------------------------------------
{"type":"MultiLineString","coordinates":[[[-89.734634999999997,31.492072000000000],
[-89.734955999999997,31.492237999999997]]]}
-(1 row)</programlisting>
+(1 row)</screen>
<para>You can also use it with 3D geometries:</para>
-<programlisting>SELECT ST_AsGeoJSON('LINESTRING(1 2 3, 4 5 6)');
-
-st_asgeojson
------------------------------------------------------------------------------------------
- {"type":"LineString","coordinates":[[1,2,3],[4,5,6]]}</programlisting>
+<programlisting>SELECT ST_AsGeoJSON('LINESTRING(1 2 3, 4 5 6)');</programlisting>
+<screen>{"type":"LineString","coordinates":[[1,2,3],[4,5,6]]}</screen>
</refsection>
<refsection>
<title>See Also</title>
More information about the postgis-tickets
mailing list