34 #include "catalog/pg_type.h"
35 #include "executor/spi.h"
36 #include "utils/builtins.h"
38 #if POSTGIS_PGSQL_VERSION > 95
39 #include "utils/fmgrprotos.h"
41 #include "utils/jsonb.h"
44 #include "../postgis_config.h"
45 #include "lwgeom_pg.h"
66 getSRSbySRID(FunctionCallInfo fcinfo, int32_t srid,
bool short_crs)
68 static const uint16_t max_query_size = 512;
72 postgis_initialize_cache(fcinfo);
74 if (SPI_OK_CONNECT != SPI_connect ())
76 elog(NOTICE,
"getSRSbySRID: could not connect to SPI manager");
84 "SELECT auth_name||':'||auth_srid \
85 FROM %s WHERE srid='%d'",
86 postgis_spatial_ref_sys(),
91 "SELECT 'urn:ogc:def:crs:'||auth_name||'::'||auth_srid \
92 FROM %s WHERE srid='%d'",
93 postgis_spatial_ref_sys(),
96 err = SPI_exec(query, 1);
99 elog(NOTICE,
"getSRSbySRID: error executing query %d", err);
105 if (SPI_processed <= 0)
112 srs = SPI_getvalue(SPI_tuptable->vals[0], SPI_tuptable->tupdesc, 1);
122 size = strlen(srs)+1;
123 srscopy = SPI_palloc(size);
124 memcpy(srscopy, srs, size);
141 static const int16_t max_query_size = 512;
143 Oid argtypes[] = {CSTRINGOID};
144 Datum values[] = {CStringGetDatum(srs)};
147 postgis_initialize_cache(fcinfo);
152 "regexp_matches($1::text, E'([a-z]+):([0-9]+)', 'gi') AS re "
153 "WHERE re[1] ILIKE auth_name AND int4(re[2]) = auth_srid",
154 postgis_spatial_ref_sys());
158 if (SPI_OK_CONNECT != SPI_connect())
160 elog(NOTICE,
"getSRIDbySRS: could not connect to SPI manager");
164 err = SPI_execute_with_args(query, 1, argtypes, values, NULL,
true, 1);
167 elog(NOTICE,
"getSRIDbySRS: error executing query %d", err);
173 if (SPI_processed <= 0)
179 "regexp_matches($1::text, E'urn:ogc:def:crs:([a-z]+):.*:([0-9]+)', 'gi') AS re "
180 "WHERE re[1] ILIKE auth_name AND int4(re[2]) = auth_srid",
181 postgis_spatial_ref_sys());
183 err = SPI_execute_with_args(query, 1, argtypes, values, NULL,
true, 1);
186 elog(NOTICE,
"getSRIDbySRS: error executing query %d", err);
191 if (SPI_processed <= 0)
198 srid = atoi(SPI_getvalue(SPI_tuptable->vals[0], SPI_tuptable->tupdesc, 1));
221 static const char* default_prefix =
"gml:";
222 const char* prefix = default_prefix;
223 const char* gml_id = NULL;
225 char *gml_id_buf, *prefix_buf;
226 text *prefix_text, *gml_id_text;
230 version = PG_GETARG_INT32(0);
231 if ( version != 2 && version != 3 )
233 elog(ERROR,
"Only GML 2 and GML 3 are supported");
238 if ( PG_ARGISNULL(1) ) PG_RETURN_NULL();
239 geom = PG_GETARG_GSERIALIZED_P(1);
242 if (PG_NARGS() > 2 && !PG_ARGISNULL(2))
253 if (PG_NARGS() > 3 && !PG_ARGISNULL(3)) option = PG_GETARG_INT32(3);
256 if (PG_NARGS() >4 && !PG_ARGISNULL(4))
258 prefix_text = PG_GETARG_TEXT_P(4);
259 if ( VARSIZE(prefix_text) == VARHDRSZ )
265 len = VARSIZE_ANY_EXHDR(prefix_text);
266 prefix_buf = palloc(len + 2);
267 memcpy(prefix_buf, VARDATA(prefix_text), len);
269 prefix_buf[len] =
':';
270 prefix_buf[len+1] =
'\0';
275 if (PG_NARGS() >5 && !PG_ARGISNULL(5))
277 gml_id_text = PG_GETARG_TEXT_P(5);
278 if ( VARSIZE(gml_id_text) == VARHDRSZ )
284 len = VARSIZE_ANY_EXHDR(gml_id_text);
285 gml_id_buf = palloc(len+1);
286 memcpy(gml_id_buf, VARDATA(gml_id_text), len);
287 gml_id_buf[len] =
'\0';
304 "Options %d passed to ST_AsGML(geography) sets "
305 "unsupported value 8",
329 lwgeom, srs,
precision, lwopts, prefix, gml_id);
333 PG_FREE_IF_COPY(geom, 1);
339 result = cstring_to_text(gml);
341 PG_RETURN_TEXT_P(result);
361 return DirectFunctionCall2(
LWGEOM_asGeoJson, PG_GETARG_DATUM(1), PG_GETARG_DATUM(2));
363 return DirectFunctionCall3(
LWGEOM_asGeoJson, PG_GETARG_DATUM(1), PG_GETARG_DATUM(2), PG_GETARG_DATUM(3));
365 elog(ERROR,
"bad call in %s", __func__);
384 int output_guess_short_srid =
LW_FALSE;
392 geom = PG_GETARG_GSERIALIZED_P(0);
396 if ( PG_NARGS() > 1 && !PG_ARGISNULL(1) )
412 if (PG_NARGS() > 2 && !PG_ARGISNULL(2))
414 int option = PG_GETARG_INT32(2);
421 output_guess_short_srid =
LW_TRUE;
426 if (srid !=
SRID_UNKNOWN && (output_short_crs || output_long_crs))
432 elog(ERROR,
"SRID %i unknown in spatial_ref_sys table", srid);
443 result = cstring_to_text(geojson);
446 PG_FREE_IF_COPY(geom, 0);
447 PG_RETURN_TEXT_P(result);
460 text *result = cstring_to_text(geojson);
463 PG_FREE_IF_COPY(geom, 0);
464 PG_RETURN_TEXT_P(result);
474 PG_RETURN_DATUM(DirectFunctionCall1(jsonb_in, PointerGetDatum(geojson)));
491 if ( PG_ARGISNULL(0) ) PG_RETURN_NULL();
493 geom = PG_GETARG_GSERIALIZED_P(0);
496 if ( PG_NARGS() > 1 && ! PG_ARGISNULL(1) )
497 relative = PG_GETARG_INT32(1) ? 1:0;
499 if ( PG_NARGS() > 2 && ! PG_ARGISNULL(2) )
510 result = cstring_to_text(svg);
513 PG_FREE_IF_COPY(geom, 0);
515 PG_RETURN_TEXT_P(result);
533 static const char* default_defid =
"x3d:";
535 const char* defid = default_defid;
539 version = PG_GETARG_INT32(0);
542 elog(ERROR,
"Only X3D version 3 are supported");
547 if ( PG_ARGISNULL(1) ) PG_RETURN_NULL();
548 geom = PG_GETARG_GSERIALIZED_P(1);
551 if (PG_NARGS() >2 && !PG_ARGISNULL(2))
561 if (PG_NARGS() >3 && !PG_ARGISNULL(3))
562 option = PG_GETARG_INT32(3);
567 if (PG_NARGS() >4 && !PG_ARGISNULL(4))
569 defid_text = PG_GETARG_TEXT_P(4);
570 if ( VARSIZE_ANY_EXHDR(defid_text) == 0 )
577 defidbuf = palloc(VARSIZE_ANY_EXHDR(defid_text)+2);
578 memcpy(defidbuf, VARDATA(defid_text),
579 VARSIZE_ANY_EXHDR(defid_text));
581 defidbuf[VARSIZE_ANY_EXHDR(defid_text)] =
':';
582 defidbuf[VARSIZE_ANY_EXHDR(defid_text)+1] =
'\0';
597 PG_FREE_IF_COPY(geom, 0);
600 elog(ERROR,
"Only SRID 4326 is supported for geocoordinates.");
609 PG_FREE_IF_COPY(geom, 1);
611 result = cstring_to_text(x3d);
614 PG_RETURN_TEXT_P(result);
625 char *encodedpolyline;
629 if ( PG_ARGISNULL(0) ) PG_RETURN_NULL();
631 geom = PG_GETARG_GSERIALIZED_P(0);
633 PG_FREE_IF_COPY(geom, 0);
634 elog(ERROR,
"Only SRID 4326 is supported.");
639 if (PG_NARGS() > 1 && !PG_ARGISNULL(1))
647 PG_FREE_IF_COPY(geom, 0);
649 result = cstring_to_text(encodedpolyline);
652 PG_RETURN_TEXT_P(result);
int32_t gserialized_get_srid(const GSERIALIZED *g)
Extract the SRID from the serialized form (it is packed into three bytes so this is a handy function)...
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
char * lwgeom_to_svg(const LWGEOM *geom, int precision, int relative)
Takes a GEOMETRY and returns a SVG representation.
#define LW_GML_IS_DEGREE
For GML3 only, declare that datas are lat/lon.
void lwgeom_free(LWGEOM *geom)
#define LW_GML_SHORTLINE
For GML3, use <LineString> rather than <Curve> for lines.
char * lwgeom_to_x3d3(const LWGEOM *geom, char *srs, int precision, int opts, const char *defid)
#define LW_GML_EXTENT
For GML2 and GML3, output only extent of geometry.
char * lwgeom_to_gml3(const LWGEOM *geom, const char *srs, int precision, int opts, const char *prefix, const char *id)
char * lwgeom_extent_to_gml3(const LWGEOM *geom, const char *srs, int precision, int opts, const char *prefix)
#define LW_GML_IS_DIMS
Macros for specifying GML options.
char * lwgeom_extent_to_gml2(const LWGEOM *geom, const char *srs, int precision, const char *prefix)
#define LW_TRUE
Return types for functions with status returns.
char * lwgeom_to_gml2(const LWGEOM *geom, const char *srs, int precision, const char *prefix)
VERSION GML 2 takes a GEOMETRY and returns a GML2 representation.
#define SRID_UNKNOWN
Unknown SRID value.
char * lwgeom_to_geojson(const LWGEOM *geo, char *srs, int precision, int has_bbox)
Takes a GEOMETRY and returns a GeoJson representation.
#define LW_X3D_USE_GEOCOORDS
char * lwgeom_to_encoded_polyline(const LWGEOM *geom, int precision)
This library is the generic geometry handling section of PostGIS.
Datum geometry_to_json(PG_FUNCTION_ARGS)
PG_FUNCTION_INFO_V1(LWGEOM_asGML)
Encode feature in GML.
int getSRIDbySRS(FunctionCallInfo fcinfo, const char *srs)
Datum LWGEOM_asGeoJson_old(PG_FUNCTION_ARGS)
Datum LWGEOM_asEncodedPolyline(PG_FUNCTION_ARGS)
Datum LWGEOM_asGML(PG_FUNCTION_ARGS)
Datum LWGEOM_asSVG(PG_FUNCTION_ARGS)
Datum geometry_to_jsonb(PG_FUNCTION_ARGS)
Datum LWGEOM_asGeoJson(PG_FUNCTION_ARGS)
char * getSRSbySRID(FunctionCallInfo fcinfo, int32_t srid, bool short_crs)
Datum LWGEOM_asX3D(PG_FUNCTION_ARGS)