From d6d84ffa87dd25af74a78d9d9aedad27bd99a95a Mon Sep 17 00:00:00 2001 From: Paul Ramsey Date: Tue, 30 Jun 2015 18:42:40 +0000 Subject: [PATCH] AsGeoJSON, Make the C binding the frequently used signature, start pushing the "versioned" one into background git-svn-id: http://svn.osgeo.org/postgis/trunk@13768 b70326c6-7e19-0410-871a-916f4a2858ee --- postgis/lwgeom_export.c | 90 ++++++++++++++++++++++------------- postgis/postgis.sql.in | 15 +++--- regress/out_geometry_expected | 4 +- 3 files changed, 66 insertions(+), 43 deletions(-) diff --git a/postgis/lwgeom_export.c b/postgis/lwgeom_export.c index 56d1d5b00..96357b2a5 100644 --- a/postgis/lwgeom_export.c +++ b/postgis/lwgeom_export.c @@ -25,6 +25,7 @@ Datum LWGEOM_asGML(PG_FUNCTION_ARGS); Datum LWGEOM_asKML(PG_FUNCTION_ARGS); Datum LWGEOM_asGeoJson(PG_FUNCTION_ARGS); +Datum LWGEOM_asGeoJson_old(PG_FUNCTION_ARGS); Datum LWGEOM_asSVG(PG_FUNCTION_ARGS); Datum LWGEOM_asX3D(PG_FUNCTION_ARGS); Datum LWGEOM_asEncodedPolyline(PG_FUNCTION_ARGS); @@ -359,6 +360,29 @@ Datum LWGEOM_asKML(PG_FUNCTION_ARGS) } +/** + * Encode Feature in GeoJson (Old C Signature) + * ST_AsGeoJSON(version, geom, precision, options) + * why was this written with a version param when there + * is only one version? + */ +PG_FUNCTION_INFO_V1(LWGEOM_asGeoJson_old); +Datum LWGEOM_asGeoJson_old(PG_FUNCTION_ARGS) +{ + switch( PG_NARGS() ) + { + case 2: + return DirectFunctionCall1(LWGEOM_asGeoJson, PG_GETARG_DATUM(1)); + case 3: + return DirectFunctionCall2(LWGEOM_asGeoJson, PG_GETARG_DATUM(1), PG_GETARG_DATUM(2)); + case 4: + return DirectFunctionCall3(LWGEOM_asGeoJson, PG_GETARG_DATUM(1), PG_GETARG_DATUM(2), PG_GETARG_DATUM(3)); + default: + elog(ERROR, "bad call in %s", __func__); + } + PG_RETURN_NULL(); +} + /** * Encode Feature in GeoJson */ @@ -369,32 +393,24 @@ Datum LWGEOM_asGeoJson(PG_FUNCTION_ARGS) LWGEOM *lwgeom; char *geojson; text *result; - int srid; - int version; - int option = 0; int has_bbox = 0; int precision = DBL_DIG; - char * srs = NULL; - - /* Get the version */ - version = PG_GETARG_INT32(0); - if ( version != 1) - { - elog(ERROR, "Only GeoJSON 1 is supported"); - PG_RETURN_NULL(); - } + char *srs = NULL; /* Get the geometry */ - if (PG_ARGISNULL(1) ) PG_RETURN_NULL(); - geom = PG_GETARG_GSERIALIZED_P(1); + if ( PG_ARGISNULL(0) ) + PG_RETURN_NULL(); + + geom = PG_GETARG_GSERIALIZED_P(0); /* Retrieve precision if any (default is max) */ - if (PG_NARGS() >2 && !PG_ARGISNULL(2)) + if ( PG_NARGS() > 1 && !PG_ARGISNULL(1) ) { - precision = PG_GETARG_INT32(2); + precision = PG_GETARG_INT32(1); if ( precision > DBL_DIG ) precision = DBL_DIG; - else if ( precision < 0 ) precision = 0; + else if ( precision < 0 ) + precision = 0; } /* Retrieve output option @@ -403,39 +419,45 @@ Datum LWGEOM_asGeoJson(PG_FUNCTION_ARGS) * 2 = short crs * 4 = long crs */ - if (PG_NARGS() >3 && !PG_ARGISNULL(3)) - option = PG_GETARG_INT32(3); - - if (option & 2 || option & 4) + if ( PG_NARGS() > 2 && !PG_ARGISNULL(2) ) { - srid = gserialized_get_srid(geom); - if ( srid != SRID_UNKNOWN ) + int option = PG_GETARG_INT32(2); + + if ( option & 2 || option & 4 ) { - if (option & 2) srs = getSRSbySRID(srid, true); - if (option & 4) srs = getSRSbySRID(srid, false); - if (!srs) + int srid = gserialized_get_srid(geom); + if ( srid != SRID_UNKNOWN ) { - elog( ERROR, - "SRID %i unknown in spatial_ref_sys table", - srid); - PG_RETURN_NULL(); + if ( option & 2 ) + srs = getSRSbySRID(srid, true); + + if ( option & 4 ) + srs = getSRSbySRID(srid, false); + + if ( !srs ) + { + elog(ERROR, + "SRID %i unknown in spatial_ref_sys table", + srid); + PG_RETURN_NULL(); + } } } - } - if (option & 1) has_bbox = 1; + if (option & 1) + has_bbox = 1; + } lwgeom = lwgeom_from_gserialized(geom); geojson = lwgeom_to_geojson(lwgeom, srs, precision, has_bbox); lwgeom_free(lwgeom); - PG_FREE_IF_COPY(geom, 1); if (srs) pfree(srs); result = cstring2text(geojson); - lwfree(geojson); + PG_FREE_IF_COPY(geom, 0); PG_RETURN_TEXT_P(result); } diff --git a/postgis/postgis.sql.in b/postgis/postgis.sql.in index dacd13681..46eaa0362 100644 --- a/postgis/postgis.sql.in +++ b/postgis/postgis.sql.in @@ -4086,24 +4086,25 @@ CREATE OR REPLACE FUNCTION ST_AsKML(version int4, geom geometry, maxdecimaldigit -- GEOJSON OUTPUT -- Availability: 1.3.4 ----------------------------------------------------------------------- --- _ST_AsGeoJson(version, geom, precision, options) -CREATE OR REPLACE FUNCTION _ST_AsGeoJson(int4, geometry, int4, int4) - RETURNS TEXT - AS 'MODULE_PATHNAME','LWGEOM_asGeoJson' - LANGUAGE 'c' IMMUTABLE STRICT; -- 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 - AS $$ SELECT _ST_AsGeoJson(1, $1, $2, $3); $$ + AS 'MODULE_PATHNAME','LWGEOM_asGeoJson' + LANGUAGE 'c' IMMUTABLE STRICT; + +-- _ST_AsGeoJson(version, geom, precision, options) +CREATE OR REPLACE FUNCTION _ST_AsGeoJson(int4, geometry, int4, int4) + RETURNS TEXT + AS $$ SELECT ST_AsGeoJson($2::geometry, $3::int4, $4::int4); $$ LANGUAGE 'sql' IMMUTABLE STRICT; -- ST_AsGeoJson(version, geom, precision,options) -- Changed 2.0.0 to use default args and named args CREATE OR REPLACE FUNCTION ST_AsGeoJson(gj_version int4, geom geometry, maxdecimaldigits int4 DEFAULT 15, options int4 DEFAULT 0) RETURNS TEXT - AS $$ SELECT _ST_AsGeoJson($1, $2, $3, $4); $$ + AS $$ SELECT ST_AsGeoJson($2::geometry, $3::int4, $4::int4); $$ LANGUAGE 'sql' IMMUTABLE STRICT; ------------------------------------------------------------------------ diff --git a/regress/out_geometry_expected b/regress/out_geometry_expected index 845880db7..9e6b08469 100644 --- a/regress/out_geometry_expected +++ b/regress/out_geometry_expected @@ -53,8 +53,8 @@ geojson_empty_geom| geojson_precision_01|{"type":"Point","coordinates":[1,1]} geojson_precision_02|{"type":"Point","coordinates":[1.1111111,1.1111111]} geojson_version_01|{"type":"Point","coordinates":[1,1]} -ERROR: Only GeoJSON 1 is supported -ERROR: Only GeoJSON 1 is supported +geojson_version_02|{"type":"Point","coordinates":[1,1]} +geojson_version_03|{"type":"Point","coordinates":[1,1]} geojson_crs_01|{"type":"Point","crs":{"type":"name","properties":{"name":"EPSG:4326"}},"coordinates":[1,1]} geojson_crs_02|{"type":"Point","coordinates":[1,1]} geojson_crs_03|{"type":"Point","crs":{"type":"name","properties":{"name":"urn:ogc:def:crs:EPSG::4326"}},"coordinates":[1,1]} -- 2.50.1