]> granicus.if.org Git - postgis/commitdiff
[doc] ST_AsGeoJSON FeatureCollection example
authorDarafei Praliaskouski <me@komzpa.net>
Sat, 1 Jun 2019 17:32:20 +0000 (17:32 +0000)
committerDarafei Praliaskouski <me@komzpa.net>
Sat, 1 Jun 2019 17:32:20 +0000 (17:32 +0000)
git-svn-id: http://svn.osgeo.org/postgis/trunk@17455 b70326c6-7e19-0410-871a-916f4a2858ee

doc/reference_output.xml

index 279b9b7ddfb17d2eb4fe39f644ffd9a3291904a9..bc717f53f55a9170cd5803006a8f52dd8e5eb7bf 100644 (file)
@@ -613,32 +613,42 @@ POINT(111.11 1.11)
                </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>