]> granicus.if.org Git - postgis/commitdiff
ST_AsGeoJSON(record) implementation from Joe Conway
authorPaul Ramsey <pramsey@cleverelephant.ca>
Mon, 29 Apr 2019 16:04:10 +0000 (16:04 +0000)
committerPaul Ramsey <pramsey@cleverelephant.ca>
Mon, 29 Apr 2019 16:04:10 +0000 (16:04 +0000)
Closes #1833

git-svn-id: http://svn.osgeo.org/postgis/trunk@17415 b70326c6-7e19-0410-871a-916f4a2858ee

.gitignore
NEWS
doc/reference_output.xml
libpgcommon/lwgeom_pg.c
libpgcommon/lwgeom_pg.h
postgis/Makefile.in
postgis/postgis.sql.in

index d9393ef7edaab463b6639b8671ebaeb2f811a70d..eb947236c0c414f54471bad36cb9de6e360e6b57 100644 (file)
@@ -109,7 +109,10 @@ postgis/legacy_minimal.sql
 postgis/legacy.sql
 postgis/postgis.sql
 postgis/postgis_proc_set_search_path.sql
+postgis/postgis_for_extension.sql
 postgis/postgis_upgrade.sql*
+postgis/postgis_upgrade_for_extension.sql
+postgis/postgis_upgrade_for_extension.sql.in
 postgis/sfcgal_upgrade.sql*
 postgis/sqldefines.h
 postgis/uninstall_legacy.sql
@@ -126,8 +129,10 @@ raster/rt_pg/rtpostgis.sql
 raster/rt_pg/rtpostgis_drop.sql
 raster/rt_pg/rtpostgis_legacy.sql
 raster/rt_pg/rtpostgis_proc_set_search_path.sql
+raster/rt_pg/rtpostgis_for_extension.sql
 raster/rt_pg/rtpostgis_upgrade.sql.in
 raster/rt_pg/rtpostgis_upgrade*.sql
+raster/rt_pg/rtpostgis_upgrade_for_extension.sql.in
 raster/rt_pg/uninstall_rtpostgis.sql
 raster/scripts/Makefile
 raster/scripts/python/Makefile
diff --git a/NEWS b/NEWS
index 665e475527911e843b9568b2988ce119e083e3f7..a36d20b7a198acfd9ea87f59bdf533318b51f54b 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -31,6 +31,7 @@ PostGIS 3.0.0
            Libre de Bruxelles (ULB), Darafei Praliaskouski)
   - #4171, ST_3DLineInterpolatePoint (Julien Cabieces, Vincent Mora)
   - #4311, Introduce `--with-wagyu` as an option for MVT polygons (Raúl Marín)
+  - #1833, ST_AsGeoJSON(row) generates full GeoJSON Features (Joe Conway)
 
 * Enhancements and fixes *
   - #4342, Move deprecated functions into legacy.sql file
index eaeee04e1f4f9c2447c3ab617524985d8fd0b259..e585afba8aedf6ed27988523d3ded08af059a58e 100644 (file)
@@ -327,6 +327,13 @@ CIRCULARSTRING(220268 150415 1,220227 150505 2,220227 150406 3)
 
          <refsynopsisdiv>
                <funcsynopsis>
+                       <funcprototype>
+                               <funcdef>text <function>ST_AsGeoJSON</function></funcdef>
+                               <paramdef><type>record </type> <parameter>feature</parameter></paramdef>
+                               <paramdef choice="opt"><type>text </type> <parameter>geomcolumnname</parameter></paramdef>
+                               <paramdef choice="opt"><type>integer </type> <parameter>maxdecimaldigits=15</parameter></paramdef>
+                               <paramdef choice="opt"><type>boolean </type> <parameter>prettyprint=false</parameter></paramdef>
+                       </funcprototype>
                        <funcprototype>
                                <funcdef>text <function>ST_AsGeoJSON</function></funcdef>
                                <paramdef><type>geometry </type> <parameter>geom</parameter></paramdef>
@@ -345,15 +352,13 @@ CIRCULARSTRING(220268 150415 1,220227 150505 2,220227 150406 3)
          <refsection>
                <title>Description</title>
 
-                 <para>Return the geometry as a GeoJSON element. (Cf <ulink
-                       url="http://geojson.org/geojson-spec.html">GeoJSON specifications 1.0</ulink>). 2D and 3D Geometries are both supported. GeoJSON only support SFS 1.1 geometry type (no curve
+                 <para>Return the geometry as a GeoJSON "geometry" object, or the row as a GeoJSON "feature" object. (Cf <ulink
+                       url="https://tools.ietf.org/html/rfc7946">GeoJSON specifications RFC 7946</ulink>). 2D and 3D Geometries are both supported. GeoJSON only support SFS 1.1 geometry types (no curve
                        support for example).</para>
 
-                       <para>The <varname>gj_version</varname> parameter is the major version of the GeoJSON spec. If specified, must be 1.  This represents the spec version of GeoJSON.</para>
-
-                       <para>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, <varname>maxdecimaldigits</varname>=6 can be a good choice for many maps.</para>
+                       <para>The maxdecimaldigits 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, <varname>maxdecimaldigits</varname>=6 can be a good choice for many maps.</para>
 
-                       <para>The last <varname>options</varname> argument could be used to add BBOX or CRS in GeoJSON output:
+                       <para>The <varname>options</varname> argument could be used to add BBOX or CRS in GeoJSON output:
                          <itemizedlist>
                                <listitem>
                                  <para>0: means no option (default value)</para>
@@ -400,31 +405,9 @@ CIRCULARSTRING(220268 150415 1,220227 150505 2,220227 150406 3)
 
                <para>ST_AsGeoJSON only builds geometry. You need to build the rest of Feature from your Postgres table yourself:</para>
                <programlisting>
-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;
+SELECT planet_osm_point.*
+FROM planet_osm_point
+LIMIT 10;
                                           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"}}]}
@@ -433,17 +416,19 @@ from (
                <programlisting>SELECT ST_AsGeoJSON(geom) from fe_edges limit 1;
                                           st_asgeojson
 -----------------------------------------------------------------------------------------------------------
-
 {"type":"MultiLineString","coordinates":[[[-89.734634999999997,31.492072000000000],
 [-89.734955999999997,31.492237999999997]]]}
 (1 row)</programlisting>
-<para>You can also use it with 3D geometries:</para>
+
+<para>You can also use the function 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>
-         </refsection>
+
+  </refsection>
+
   <refsection>
     <title>See Also</title>
 
index 3f69b3b48bdfc6ca1a2f69f9a5bb1d63ded8287c..ee0cd5981bec9601e725f6d748ad4d2bf9643e48 100644 (file)
@@ -20,6 +20,7 @@
 #include <executor/spi.h>
 #include <utils/guc.h>
 #include <utils/guc_tables.h>
+#include <catalog/namespace.h>
 
 #include "../postgis_config.h"
 #include "liblwgeom.h"
 
 #define PGC_ERRMSG_MAXLEN 2048 //256
 
+/* Global cache to hold GEOMETRYOID */
+Oid GEOMETRYOID = InvalidOid;
+Oid GEOGRAPHYOID = InvalidOid;
+
+Oid postgis_geometry_oid(void)
+{
+       if (GEOMETRYOID == InvalidOid) {
+               Oid typoid = TypenameGetTypid("geometry");
+               if (OidIsValid(typoid) && get_typisdefined(typoid))
+               {
+                       GEOMETRYOID = typoid;
+               }
+       }
+       return GEOMETRYOID;
+}
+
+Oid postgis_geography_oid(void)
+{
+       if (GEOGRAPHYOID == InvalidOid) {
+               Oid typoid = TypenameGetTypid("geography");
+               if (OidIsValid(typoid) && get_typisdefined(typoid))
+               {
+                       GEOGRAPHYOID = typoid;
+               }
+       }
+       return GEOGRAPHYOID;
+}
+
 /*
  * Error message parsing functions
  *
index e12a8a28c71848e44c9e75db9dd8b1e99c74ca05..d5f032de92f1d91b74d72b6bd4404de3de89cb00 100644 (file)
 #include "liblwgeom.h"
 #include "pgsql_compat.h"
 
+/* Globals to hold GEOMETRYOID, GEOGRAPHYOID */
+extern Oid GEOMETRYOID;
+extern Oid GEOGRAPHYOID;
+Oid postgis_geometry_oid(void);
+Oid postgis_geography_oid(void);
+
 /* Install PosgreSQL handlers for liblwgeom use */
 void pg_install_lwgeom_handlers(void);
 
index bb7b77479ddb7753f3811d132d541cc855b4ac31..fe86ba273d365c47a490a3fdbb1c11bb3c16e443 100644 (file)
@@ -125,6 +125,7 @@ PG_OBJS= \
        lwgeom_out_mvt.o \
        geobuf.o \
        lwgeom_out_geobuf.o \
+       lwgeom_out_geojson.o \
        postgis_legacy.o
 
 # Objects to build using PGXS
index 2fe9ce60ce1ae34888872cc85595e12a03849706..c90158f967d8bc3610606780d5d7a0d20f65c3d9 100644 (file)
@@ -4587,9 +4587,16 @@ CREATE OR REPLACE FUNCTION ST_AsKML(geom geometry, maxdecimaldigits int4 DEFAULT
 -- ST_AsGeoJson(geom, precision, options) / version=1
 -- Changed 2.0.0 to use default args and named args
 CREATE OR REPLACE FUNCTION ST_AsGeoJson(geom geometry, maxdecimaldigits int4 DEFAULT 15, options int4 DEFAULT 0)
-       RETURNS TEXT
+       RETURNS text
        AS 'MODULE_PATHNAME','LWGEOM_asGeoJson'
-       LANGUAGE 'c' IMMUTABLE STRICT  _PARALLEL
+       LANGUAGE 'c' IMMUTABLE STRICT _PARALLEL
+       _COST_LOW;
+
+-- Availability: 3.0.0
+CREATE OR REPLACE FUNCTION ST_AsGeoJson(r record, geom_column text DEFAULT '', maxdecimaldigits int4 DEFAULT 15, pretty_print bool DEFAULT false)
+       RETURNS text
+       AS 'MODULE_PATHNAME','ST_AsGeoJsonRow'
+       LANGUAGE 'c' STABLE STRICT _PARALLEL
        _COST_LOW;
 
 -----------------------------------------------------------------------