From b9bc501e2b3dc0bca89e7654fb31b39d743786c7 Mon Sep 17 00:00:00 2001 From: Olivier Courtin Date: Mon, 28 Sep 2009 18:16:21 +0000 Subject: [PATCH] Add geography typmod support for export functions (ST_AsGML, ST_AsGeoJson, ST_KML, St_AsSVG). Create lwgeom_export.c and lwgeom_export.h to factorize common export functions routines. git-svn-id: http://svn.osgeo.org/postgis/trunk@4535 b70326c6-7e19-0410-871a-916f4a2858ee --- postgis/Makefile.in | 1 + postgis/geography.sql.in.c | 153 ++++++++++++++++++++++ postgis/geography_inout.c | 262 +++++++++++++++++++++++++++++++++++++ postgis/lwgeom_export.c | 86 ++++++++++++ postgis/lwgeom_export.h | 26 ++++ postgis/lwgeom_geojson.c | 76 +---------- postgis/lwgeom_gml.c | 79 +---------- postgis/lwgeom_kml.c | 5 +- postgis/lwgeom_svg.c | 9 +- 9 files changed, 539 insertions(+), 158 deletions(-) create mode 100644 postgis/lwgeom_export.c create mode 100644 postgis/lwgeom_export.h diff --git a/postgis/Makefile.in b/postgis/Makefile.in index beb90641a..3e2e1c9a1 100644 --- a/postgis/Makefile.in +++ b/postgis/Makefile.in @@ -38,6 +38,7 @@ PG_OBJS=lwgeom_pg.o \ lwgeom_chip.o \ lwgeom_geos.o \ lwgeom_geos_prepared.o \ + lwgeom_export.o \ lwgeom_svg.o \ lwgeom_gml.o \ lwgeom_kml.o \ diff --git a/postgis/geography.sql.in.c b/postgis/geography.sql.in.c index 63164f10a..a83fe62ab 100644 --- a/postgis/geography.sql.in.c +++ b/postgis/geography.sql.in.c @@ -238,4 +238,157 @@ CREATE OPERATOR CLASS gist_geography_ops FUNCTION 6 geography_gist_picksplit (internal, internal), FUNCTION 7 geography_gist_same (box2d, box2d, internal); +-- ---------- ---------- ---------- ---------- ---------- ---------- ---------- +-- Export Functions +-- Availability: 1.5.0 +-- ---------- ---------- ---------- ---------- ---------- ---------- ---------- + +-- +-- SVG OUTPUT +-- + +-- ST_AsSVG(geography, precision, rel) +CREATE OR REPLACE FUNCTION ST_AsSVG(geography,int4,int4) + RETURNS TEXT + AS 'MODULE_PATHNAME','geography_as_svg' + LANGUAGE 'C' IMMUTABLE STRICT; + +-- ST_AsSVG(geography, precision) / rel=0 +CREATE OR REPLACE FUNCTION ST_AsSVG(geography,int4) + RETURNS TEXT + AS 'MODULE_PATHNAME','geography_as_svg' + LANGUAGE 'C' IMMUTABLE STRICT; + +-- ST_AsSVG(geography) / precision=15, rel=0 +CREATE OR REPLACE FUNCTION ST_AsSVG(geography) + RETURNS TEXT + AS 'MODULE_PATHNAME','geography_as_svg' + LANGUAGE 'C' IMMUTABLE STRICT; + + +-- +-- GML OUTPUT +-- + +-- _ST_AsGML(version, geography, precision, option) +CREATE OR REPLACE FUNCTION _ST_AsGML(int4, geography, int4, int4) + RETURNS TEXT + AS 'MODULE_PATHNAME','geography_as_gml' + LANGUAGE 'C' IMMUTABLE STRICT; + +-- ST_AsGML(geography, precision) / version=2 options=0 +CREATE OR REPLACE FUNCTION ST_AsGML(geography, int4) + RETURNS TEXT + AS 'SELECT _ST_AsGML(2, $1, $2, 0)' + LANGUAGE 'SQL' IMMUTABLE STRICT; + +-- ST_AsGML(geography) / precision=15 version=2 options=0 +CREATE OR REPLACE FUNCTION ST_AsGML(geography) + RETURNS TEXT + AS 'SELECT _ST_AsGML(2, $1, 15, 0)' + LANGUAGE 'SQL' IMMUTABLE STRICT; + +-- ST_AsGML(version, geography) / precision=15 version=2 options=0 +CREATE OR REPLACE FUNCTION ST_AsGML(int4, geography) + RETURNS TEXT + AS 'SELECT _ST_AsGML($1, $2, 15, 0)' + LANGUAGE 'SQL' IMMUTABLE STRICT; + +-- ST_AsGML(version, geography, precision) / options = 0 +CREATE OR REPLACE FUNCTION ST_AsGML(int4, geography, int4) + RETURNS TEXT + AS 'SELECT _ST_AsGML($1, $2, $3, 0)' + LANGUAGE 'SQL' IMMUTABLE STRICT; + +-- ST_AsGML (geography, precision, option) / version=2 +CREATE OR REPLACE FUNCTION ST_AsGML(geography, int4, int4) + RETURNS TEXT + AS 'SELECT _ST_AsGML(2, $1, $2, $3)' + LANGUAGE 'SQL' IMMUTABLE STRICT; + +-- ST_AsGML(version, geography, precision, option) +CREATE OR REPLACE FUNCTION ST_AsGML(int4, geography, int4, int4) + RETURNS TEXT + AS 'SELECT _ST_AsGML($1, $2, $3, $4)' + LANGUAGE 'SQL' IMMUTABLE STRICT; + + + +-- +-- KML OUTPUT +-- + +-- _ST_AsKML(version, geography, precision) +CREATE OR REPLACE FUNCTION _ST_AsKML(int4, geography, int4) + RETURNS TEXT + AS 'MODULE_PATHNAME','geography_as_kml' + LANGUAGE 'C' IMMUTABLE STRICT; + +-- AsKML(geography,precision) / version=2 +CREATE OR REPLACE FUNCTION ST_AsKML(geography, int4) + RETURNS TEXT + AS 'SELECT _ST_AsKML(2, $1, $2)' + LANGUAGE 'SQL' IMMUTABLE STRICT; + +-- AsKML(geography) / precision=15 version=2 +CREATE OR REPLACE FUNCTION ST_AsKML(geography) + RETURNS TEXT + AS 'SELECT _ST_AsKML(2, $1, 15)' + LANGUAGE 'SQL' IMMUTABLE STRICT; + +-- ST_AsKML(version, geography) / precision=15 +CREATE OR REPLACE FUNCTION ST_AsKML(int4, geography) + RETURNS TEXT + AS 'SELECT _ST_AsKML($1, $2, 15)' + LANGUAGE 'SQL' IMMUTABLE STRICT; + +-- ST_AsKML(version, geography, precision) +CREATE OR REPLACE FUNCTION ST_AsKML(int4, geography, int4) + RETURNS TEXT + AS 'SELECT _ST_AsKML($1, $2, $3)' + LANGUAGE 'SQL' IMMUTABLE STRICT; + + + +-- +-- GeoJson Output +-- + +CREATE OR REPLACE FUNCTION _ST_AsGeoJson(int4, geography, int4, int4) + RETURNS text + AS 'MODULE_PATHNAME','geography_as_geojson' + LANGUAGE 'C' IMMUTABLE STRICT; + +-- ST_AsGeoJson(geography) / precision=15 version=1 options=0 +CREATE OR REPLACE FUNCTION ST_AsGeoJson(geography) + RETURNS TEXT + AS 'SELECT _ST_AsGeoJson(1, $1, 15, 0)' + LANGUAGE 'SQL' IMMUTABLE STRICT; + +-- ST_AsGeoJson(version, geography) / precision=15 options=0 +CREATE OR REPLACE FUNCTION ST_AsGeoJson(int4, geography) + RETURNS TEXT + AS 'SELECT _ST_AsGeoJson($1, $2, 15, 0)' + LANGUAGE 'SQL' IMMUTABLE STRICT; + +-- ST_AsGeoJson(version, geography, precision) / options=0 +CREATE OR REPLACE FUNCTION ST_AsGeoJson(int4, geography, int4) + RETURNS TEXT + AS 'SELECT _ST_AsGeoJson($1, $2, $3, 0)' + LANGUAGE 'SQL' IMMUTABLE STRICT; + +-- ST_AsGeoJson(geography, precision, options) / version=1 +CREATE OR REPLACE FUNCTION ST_AsGeoJson(geography, int4, int4) + RETURNS TEXT + AS 'SELECT _ST_AsGeoJson(1, $1, $2, $3)' + LANGUAGE 'SQL' IMMUTABLE STRICT; + +-- ST_AsGeoJson(version, geography, precision,options) +CREATE OR REPLACE FUNCTION ST_AsGeoJson(int4, geography, int4, int4) + RETURNS TEXT + AS 'SELECT _ST_AsGeoJson($1, $2, $3, $4)' + LANGUAGE 'SQL' IMMUTABLE STRICT; + + + COMMIT; diff --git a/postgis/geography_inout.c b/postgis/geography_inout.c index 3a941aada..1840a1782 100644 --- a/postgis/geography_inout.c +++ b/postgis/geography_inout.c @@ -28,6 +28,7 @@ #include "libgeom.h" /* For standard geometry types. */ #include "lwgeom_pg.h" /* For debugging macros. */ #include "geography.h" /* For utility functions. */ +#include "lwgeom_export.h" /* For exports functions. */ Datum geography_in(PG_FUNCTION_ARGS); Datum geography_out(PG_FUNCTION_ARGS); @@ -40,6 +41,10 @@ Datum geography_typmod_type(PG_FUNCTION_ARGS); Datum geography_enforce_typmod(PG_FUNCTION_ARGS); Datum geography_as_text(PG_FUNCTION_ARGS); Datum geography_from_text(PG_FUNCTION_ARGS); +Datum geography_as_geojson(PG_FUNCTION_ARGS); +Datum geography_as_gml(PG_FUNCTION_ARGS); +Datum geography_as_kml(PG_FUNCTION_ARGS); +Datum geography_as_svg(PG_FUNCTION_ARGS); Datum geography_as_binary(PG_FUNCTION_ARGS); Datum geography_from_binary(PG_FUNCTION_ARGS); Datum geography_from_geometry(PG_FUNCTION_ARGS); @@ -478,6 +483,263 @@ Datum geography_as_text(PG_FUNCTION_ARGS) PG_RETURN_POINTER(wkt); } + +/* +** geography_as_gml(*GSERIALIZED) returns cstring +*/ +PG_FUNCTION_INFO_V1(geography_as_gml); +Datum geography_as_gml(PG_FUNCTION_ARGS) +{ + LWGEOM *lwgeom = NULL; + GSERIALIZED *g = NULL; + char *gml; + text *result; + int len; + int version; + char *srs; + int SRID=4326; + int precision = MAX_DOUBLE_PRECISION; + int option=0; + + /* Get the version */ + version = PG_GETARG_INT32(0); + if ( version != 2 && version != 3 ) + { + elog(ERROR, "Only GML 2 and GML 3 are supported"); + PG_RETURN_NULL(); + } + + /* Get the geography */ + if ( PG_ARGISNULL(1) ) PG_RETURN_NULL(); + g = (GSERIALIZED*)PG_DETOAST_DATUM(PG_GETARG_DATUM(1)); + + /* Convert to lwgeom so we can run the old functions */ + lwgeom = lwgeom_from_gserialized(g); + + /* Retrieve precision if any (default is max) */ + if (PG_NARGS() >2 && !PG_ARGISNULL(2)) + { + precision = PG_GETARG_INT32(2); + if ( precision > MAX_DOUBLE_PRECISION ) + precision = MAX_DOUBLE_PRECISION; + else if ( precision < 0 ) precision = 0; + } + + /* retrieve option */ + if (PG_NARGS() >3 && !PG_ARGISNULL(3)) + option = PG_GETARG_INT32(3); + + if (option & 1) srs = getSRSbySRID(SRID, false); + else srs = getSRSbySRID(SRID, true); + if (!srs) + { + elog(ERROR, "SRID 4326 unknown in spatial_ref_sys table"); + PG_RETURN_NULL(); + } + + if (version == 2) + gml = geometry_to_gml2(lwgeom_serialize(lwgeom), srs, precision); + else + gml = geometry_to_gml3(lwgeom_serialize(lwgeom), srs, precision, true); + + PG_FREE_IF_COPY(lwgeom, 1); + + len = strlen(gml) + VARHDRSZ; + + result = palloc(len); + SET_VARSIZE(result, len); + + memcpy(VARDATA(result), gml, len-VARHDRSZ); + + pfree(gml); + + PG_RETURN_POINTER(result); +} + + +/* +** geography_as_kml(*GSERIALIZED) returns cstring +*/ +PG_FUNCTION_INFO_V1(geography_as_kml); +Datum geography_as_kml(PG_FUNCTION_ARGS) +{ + GSERIALIZED *g = NULL; + LWGEOM *lwgeom = NULL; + char *kml; + text *result; + int len; + int version; + int precision = MAX_DOUBLE_PRECISION; + + + /* Get the version */ + version = PG_GETARG_INT32(0); + if ( version != 2) + { + elog(ERROR, "Only KML 2 is supported"); + PG_RETURN_NULL(); + } + + /* Get the geometry */ + if ( PG_ARGISNULL(1) ) PG_RETURN_NULL(); + g = (GSERIALIZED*)PG_DETOAST_DATUM(PG_GETARG_DATUM(1)); + + /* Convert to lwgeom so we can run the old functions */ + lwgeom = lwgeom_from_gserialized(g); + + /* Retrieve precision if any (default is max) */ + if (PG_NARGS() >2 && !PG_ARGISNULL(2)) + { + precision = PG_GETARG_INT32(2); + if ( precision > MAX_DOUBLE_PRECISION ) + precision = MAX_DOUBLE_PRECISION; + else if ( precision < 0 ) precision = 0; + } + + kml = geometry_to_kml2(lwgeom_serialize(lwgeom), precision); + + PG_FREE_IF_COPY(lwgeom, 1); + + len = strlen(kml) + VARHDRSZ; + + result = palloc(len); + SET_VARSIZE(result, len); + + memcpy(VARDATA(result), kml, len-VARHDRSZ); + + pfree(kml); + + PG_RETURN_POINTER(result); +} + + +/* +** geography_as_svg(*GSERIALIZED) returns cstring +*/ +PG_FUNCTION_INFO_V1(geography_as_svg); +Datum geography_as_svg(PG_FUNCTION_ARGS) +{ + GSERIALIZED *g = NULL; + LWGEOM *lwgeom = NULL; + char *svg; + text *result; + int len; + bool relative = false; + int precision=MAX_DOUBLE_PRECISION; + + if ( PG_ARGISNULL(0) ) PG_RETURN_NULL(); + + g = (GSERIALIZED*)PG_DETOAST_DATUM(PG_GETARG_DATUM(0)); + + /* Convert to lwgeom so we can run the old functions */ + lwgeom = lwgeom_from_gserialized(g); + + /* check for relative path notation */ + if ( PG_NARGS() > 1 && ! PG_ARGISNULL(1) ) + relative = PG_GETARG_INT32(1) ? true:false; + + if ( PG_NARGS() > 2 && ! PG_ARGISNULL(2) ) + { + precision = PG_GETARG_INT32(2); + if ( precision > MAX_DOUBLE_PRECISION ) + precision = MAX_DOUBLE_PRECISION; + else if ( precision < 0 ) precision = 0; + } + + svg = geometry_to_svg(lwgeom_serialize(lwgeom), relative, precision); + PG_FREE_IF_COPY(lwgeom, 0); + + len = strlen(svg) + VARHDRSZ; + result = palloc(len); + SET_VARSIZE(result, len); + memcpy(VARDATA(result), svg, len-VARHDRSZ); + + pfree(svg); + + PG_RETURN_POINTER(result); +} + + +/* +** geography_as_geojson(*GSERIALIZED) returns cstring +*/ +PG_FUNCTION_INFO_V1(geography_as_geojson); +Datum geography_as_geojson(PG_FUNCTION_ARGS) +{ + LWGEOM *lwgeom = NULL; + GSERIALIZED *g = NULL; + char *geojson; + text *result; + int len; + int version; + int option = 0; + bool has_bbox = 0; + int precision = MAX_DOUBLE_PRECISION; + char * srs = NULL; + + /* Get the version */ + version = PG_GETARG_INT32(0); + if ( version != 1) + { + elog(ERROR, "Only GeoJSON 1 is supported"); + PG_RETURN_NULL(); + } + + /* Get the geography */ + if (PG_ARGISNULL(1) ) PG_RETURN_NULL(); + g = (GSERIALIZED*)PG_DETOAST_DATUM(PG_GETARG_DATUM(1)); + + /* Convert to lwgeom so we can run the old functions */ + lwgeom = lwgeom_from_gserialized(g); + + /* Retrieve precision if any (default is max) */ + if (PG_NARGS() >2 && !PG_ARGISNULL(2)) + { + precision = PG_GETARG_INT32(2); + if ( precision > MAX_DOUBLE_PRECISION ) + precision = MAX_DOUBLE_PRECISION; + else if ( precision < 0 ) precision = 0; + } + + /* Retrieve output option + * 0 = without option (default) + * 1 = bbox + * 2 = short crs + * 4 = long crs + */ + if (PG_NARGS() >3 && !PG_ARGISNULL(3)) + option = PG_GETARG_INT32(3); + + if (option & 2 || option & 4) + { + /* Geography only handle srid 4326 */ + if (option & 2) srs = getSRSbySRID(4326, true); + if (option & 4) srs = getSRSbySRID(4326, false); + + if (!srs) + { + elog(ERROR, "SRID 4326 unknown in spatial_ref_sys table"); + PG_RETURN_NULL(); + } + } + + if (option & 1) has_bbox = 1; + + geojson = geometry_to_geojson(lwgeom_serialize(lwgeom), srs, has_bbox, precision); + PG_FREE_IF_COPY(lwgeom, 1); + if (srs) pfree(srs); + + len = strlen(geojson) + VARHDRSZ; + result = palloc(len); + SET_VARSIZE(result, len); + memcpy(VARDATA(result), geojson, len-VARHDRSZ); + + pfree(geojson); + + PG_RETURN_POINTER(result); +} + + /* ** geography_from_text(*char) returns *GSERIALIZED ** diff --git a/postgis/lwgeom_export.c b/postgis/lwgeom_export.c new file mode 100644 index 000000000..492a2400d --- /dev/null +++ b/postgis/lwgeom_export.c @@ -0,0 +1,86 @@ +/********************************************************************** + * $Id:$ + * + * PostGIS - Export functions for PostgreSQL/PostGIS + * Copyright 2009 Olivier Courtin + * + * This is free software; you can redistribute and/or modify it under + * the terms of the GNU General Public Licence. See the COPYING file. + * + **********************************************************************/ + + +/** @file + * Commons functions for all export functions + */ + +#include "postgres.h" +#include "executor/spi.h" + +#include "lwgeom_pg.h" +#include "liblwgeom.h" + +#include "lwgeom_export.h" + +/* + * Retrieve an SRS from a given SRID + * Require valid spatial_ref_sys table entry + * + * Could return SRS as short one (i.e EPSG:4326) + * or as long one: (i.e urn:ogc:def:crs:EPSG:4326) + */ +char * getSRSbySRID(int SRID, bool short_crs) +{ + char query[256]; + char *srs, *srscopy; + int size, err; + + if (SPI_OK_CONNECT != SPI_connect ()) + { + elog(NOTICE, "getSRSbySRID: could not connect to SPI manager"); + SPI_finish(); + return NULL; + } + + if (short_crs) + sprintf(query, "SELECT auth_name||':'||auth_srid \ + FROM spatial_ref_sys WHERE srid='%d'", SRID); + else + sprintf(query, "SELECT 'urn:ogc:def:crs:'||auth_name||':'||auth_srid \ + FROM spatial_ref_sys WHERE srid='%d'", SRID); + + err = SPI_exec(query, 1); + if ( err < 0 ) + { + elog(NOTICE, "getSRSbySRID: error executing query %d", err); + SPI_finish(); + return NULL; + } + + /* no entry in spatial_ref_sys */ + if (SPI_processed <= 0) + { + SPI_finish(); + return NULL; + } + + /* get result */ + srs = SPI_getvalue(SPI_tuptable->vals[0], SPI_tuptable->tupdesc, 1); + + /* NULL result */ + if ( ! srs ) + { + SPI_finish(); + return NULL; + } + + /* copy result to upper executor context */ + size = strlen(srs)+1; + srscopy = SPI_palloc(size); + memcpy(srscopy, srs, size); + + /* disconnect from SPI */ + SPI_finish(); + + return srscopy; +} diff --git a/postgis/lwgeom_export.h b/postgis/lwgeom_export.h new file mode 100644 index 000000000..e63a246a0 --- /dev/null +++ b/postgis/lwgeom_export.h @@ -0,0 +1,26 @@ +/********************************************************************** + * $Id:$ + * + * PostGIS - Export functions for PostgreSQL/PostGIS + * Copyright 2009 Olivier Courtin + * + * This is free software; you can redistribute and/or modify it under + * the terms of the GNU General Public Licence. See the COPYING file. + * + **********************************************************************/ + +/** + * Commons define and prototype function for all export functions + */ + +#define SHOW_DIGS_DOUBLE 15 +#define MAX_DOUBLE_PRECISION 15 +#define MAX_DIGS_DOUBLE (SHOW_DIGS_DOUBLE + 2) /* +2 mean add dot and sign */ + +char * getSRSbySRID(int SRID, bool short_crs); + +char *geometry_to_geojson(uchar *srl, char *srs, bool has_bbox, int precision); +char *geometry_to_gml2(uchar *srl, char *srs, int precision); +char *geometry_to_gml3(uchar *srl, char *srs, int precision, bool is_deegree); +char *geometry_to_kml2(uchar *srl, int precision); +char *geometry_to_svg(uchar *srl, bool relative, int precision); diff --git a/postgis/lwgeom_geojson.c b/postgis/lwgeom_geojson.c index 0d46fb45b..e0ce03b12 100644 --- a/postgis/lwgeom_geojson.c +++ b/postgis/lwgeom_geojson.c @@ -16,15 +16,13 @@ **********************************************************************/ #include "postgres.h" -#include "executor/spi.h" #include "lwgeom_pg.h" #include "liblwgeom.h" +#include "lwgeom_export.h" Datum LWGEOM_asGeoJson(PG_FUNCTION_ARGS); -char *geometry_to_geojson(uchar *srl, char *srs, bool has_bbox, int precision); - static char *asgeojson_point(LWPOINT *point, char *srs, BOX3D *bbox, int precision); static char *asgeojson_line(LWLINE *line, char *srs, BOX3D *bbox, int precision); static char *asgeojson_poly(LWPOLY *poly, char *srs, BOX3D *bbox, int precision); @@ -37,11 +35,6 @@ static size_t asgeojson_inspected_buf(LWGEOM_INSPECTED *insp, char *output, BOX3 static size_t pointArray_to_geojson(POINTARRAY *pa, char *buf, int precision); static size_t pointArray_geojson_size(POINTARRAY *pa, int precision); -static char *getSRSbySRID(int SRID, bool short_crs); - -#define SHOW_DIGS_DOUBLE 15 -#define MAX_DOUBLE_PRECISION 15 -#define MAX_DIGS_DOUBLE (SHOW_DIGS_DOUBLE + 2) /* +2 mean add dot and sign */ /** @@ -123,8 +116,7 @@ Datum LWGEOM_asGeoJson(PG_FUNCTION_ARGS) } - -/* +/** * Takes a GEOMETRY and returns a GeoJson representation */ char * @@ -199,6 +191,7 @@ geometry_to_geojson(uchar *geom, char *srs, bool has_bbox, int precision) } + /** * Handle SRS */ @@ -226,13 +219,13 @@ asgeojson_srs_buf(char *output, char *srs) } + /** * Handle Bbox */ static size_t asgeojson_bbox_size(bool hasz, int precision) { - int size; if (!hasz) @@ -869,67 +862,6 @@ pointArray_to_geojson(POINTARRAY *pa, char *output, int precision) } -/* - * Common geojson routines - */ - -static char * -getSRSbySRID(int SRID, bool short_crs) -{ - char query[256]; - char *srs, *srscopy; - int size, err; - - if (SPI_OK_CONNECT != SPI_connect ()) - { - elog(NOTICE, "getSRSbySRID: could not connect to SPI manager"); - SPI_finish(); - return NULL; - } - - if (short_crs) - sprintf(query, "SELECT auth_name||':'||auth_srid \ - FROM spatial_ref_sys WHERE srid='%d'", SRID); - else - sprintf(query, "SELECT 'urn:ogc:def:crs:'||auth_name||':'||auth_srid \ - FROM spatial_ref_sys WHERE srid='%d'", SRID); - - err = SPI_exec(query, 1); - if ( err < 0 ) - { - elog(NOTICE, "getSRSbySRID: error executing query %d", err); - SPI_finish(); - return NULL; - } - - /* no entry in spatial_ref_sys */ - if (SPI_processed <= 0) - { - SPI_finish(); - return NULL; - } - - /* get result */ - srs = SPI_getvalue(SPI_tuptable->vals[0], SPI_tuptable->tupdesc, 1); - - /* NULL result */ - if ( ! srs ) - { - SPI_finish(); - return NULL; - } - - /* copy result to upper executor context */ - size = strlen(srs)+1; - srscopy = SPI_palloc(size); - memcpy(srscopy, srs, size); - - /* disconnect from SPI */ - SPI_finish(); - - return srscopy; -} - /** * Returns maximum size of rendered pointarray in bytes. diff --git a/postgis/lwgeom_gml.c b/postgis/lwgeom_gml.c index 97538e52c..68200d1d7 100644 --- a/postgis/lwgeom_gml.c +++ b/postgis/lwgeom_gml.c @@ -17,14 +17,13 @@ #include "postgres.h" -#include "executor/spi.h" #include "lwgeom_pg.h" #include "liblwgeom.h" +#include "lwgeom_export.h" -Datum LWGEOM_asGML(PG_FUNCTION_ARGS); -char *geometry_to_gml2(uchar *srl, char *srs, int precision); +Datum LWGEOM_asGML(PG_FUNCTION_ARGS); static size_t asgml2_point_size(LWPOINT *point, char *srs, int precision); static char *asgml2_point(LWPOINT *point, char *srs, int precision); @@ -36,8 +35,6 @@ static size_t asgml2_inspected_size(LWGEOM_INSPECTED *geom, char *srs, int preci static char *asgml2_inspected(LWGEOM_INSPECTED *geom, char *srs, int precision); static size_t pointArray_toGML2(POINTARRAY *pa, char *buf, int precision); -char *geometry_to_gml3(uchar *srl, char *srs, int precision, bool is_deegree); - static size_t asgml3_point_size(LWPOINT *point, char *srs, int precision); static char *asgml3_point(LWPOINT *point, char *srs, int precision, bool is_deegree); static size_t asgml3_line_size(LWLINE *line, char *srs, int precision); @@ -49,12 +46,6 @@ static char *asgml3_inspected(LWGEOM_INSPECTED *geom, char *srs, int precision, static size_t pointArray_toGML3(POINTARRAY *pa, char *buf, int precision, bool is_deegree); static size_t pointArray_GMLsize(POINTARRAY *pa, int precision); -static char *getSRSbySRID(int SRID, bool short_crs); - - -#define SHOW_DIGS_DOUBLE 15 -#define MAX_DOUBLE_PRECISION 15 -#define MAX_DIGS_DOUBLE (SHOW_DIGS_DOUBLE + 2) /* +2 mean add dot and sign */ /** @@ -884,72 +875,6 @@ pointArray_toGML3(POINTARRAY *pa, char *output, int precision, bool is_deegree) -/* - * Common GML routines - */ - -static char * -getSRSbySRID(int SRID, bool short_crs) -{ - char query[128]; - char *srs, *srscopy; - int size, err; - - /* connect to SPI */ - if (SPI_OK_CONNECT != SPI_connect ()) - { - elog(NOTICE, "getSRSbySRID: could not connect to SPI manager"); - SPI_finish(); - return NULL; - } - - if (short_crs) - sprintf(query, "SELECT auth_name||':'||auth_srid \ - FROM spatial_ref_sys WHERE srid='%d'", SRID); - else - sprintf(query, "SELECT 'urn:ogc:def:crs:'||auth_name||':'||auth_srid \ - FROM spatial_ref_sys WHERE srid='%d'", SRID); - - /* execute query */ - err = SPI_exec(query, 1); - if ( err < 0 ) - { - elog(NOTICE, "getSRSbySRID: error executing query %d", err); - SPI_finish(); - return NULL; - } - - /* no entry in spatial_ref_sys */ - if (SPI_processed <= 0) - { - /*elog(NOTICE, "getSRSbySRID: no record for SRID %d", SRID); */ - SPI_finish(); - return NULL; - } - - /* get result */ - srs = SPI_getvalue(SPI_tuptable->vals[0], - SPI_tuptable->tupdesc, 1); - - /* NULL result */ - if ( ! srs ) - { - /*elog(NOTICE, "getSRSbySRID: null result"); */ - SPI_finish(); - return NULL; - } - - /* copy result to upper executor context */ - size = strlen(srs)+1; - srscopy = SPI_palloc(size); - memcpy(srscopy, srs, size); - - /* disconnect from SPI */ - SPI_finish(); - - return srscopy; -} - /* * Returns maximum size of rendered pointarray in bytes. */ diff --git a/postgis/lwgeom_kml.c b/postgis/lwgeom_kml.c index df826f29d..552ad6936 100644 --- a/postgis/lwgeom_kml.c +++ b/postgis/lwgeom_kml.c @@ -24,6 +24,7 @@ #include "lwgeom_pg.h" #include "liblwgeom.h" +#include "lwgeom_export.h" Datum LWGEOM_asKML(PG_FUNCTION_ARGS); @@ -41,10 +42,6 @@ static size_t pointArray_toKML2(POINTARRAY *pa, char *buf, int precision); static size_t pointArray_KMLsize(POINTARRAY *pa, int precision); -#define SHOW_DIGS_DOUBLE 15 -#define MAX_DOUBLE_PRECISION 15 -#define MAX_DIGS_DOUBLE (SHOW_DIGS_DOUBLE + 2) /* +2 mean add dot and sign */ - /** * Encode feature in KML diff --git a/postgis/lwgeom_svg.c b/postgis/lwgeom_svg.c index 0fa63b7ea..ee60ab0a1 100644 --- a/postgis/lwgeom_svg.c +++ b/postgis/lwgeom_svg.c @@ -23,9 +23,9 @@ #include "postgres.h" #include "lwgeom_pg.h" #include "liblwgeom.h" +#include "lwgeom_export.h" Datum assvg_geometry(PG_FUNCTION_ARGS); -char *geometry_to_svg(uchar *srl, bool relative, int precision); static char * assvg_point(LWPOINT *point, bool relative, int precision); static char * assvg_line(LWLINE *line, bool relative, int precision); static char * assvg_polygon(LWPOLY *poly, bool relative, int precision); @@ -40,9 +40,6 @@ static size_t pointArray_svg_size(POINTARRAY *pa, int precision); static size_t pointArray_svg_rel(POINTARRAY *pa, char * output, bool close_ring, int precision); static size_t pointArray_svg_abs(POINTARRAY *pa, char * output, bool close_ring, int precision); -#define SHOW_DIGS_DOUBLE 15 -#define MAX_DOUBLE_PRECISION 15 -#define MAX_DIGS_DOUBLE (SHOW_DIGS_DOUBLE + 2) /* +2 mean add dot and sign */ /** * SVG features @@ -87,7 +84,9 @@ Datum assvg_geometry(PG_FUNCTION_ARGS) } -/** takes a GEOMETRY and returns a SVG representation */ +/** + * Takes a GEOMETRY and returns a SVG representation + */ char * geometry_to_svg(uchar *geom, bool relative, int precision) { -- 2.49.0