32 #include "catalog/pg_type.h"
33 #include "executor/spi.h"
34 #include "utils/builtins.h"
35 #include "utils/jsonb.h"
37 #include "../postgis_config.h"
38 #include "lwgeom_cache.h"
39 #include "lwgeom_pg.h"
67 static const char* default_prefix =
"gml:";
68 const char* prefix = default_prefix;
69 const char* gml_id = NULL;
71 char *gml_id_buf, *prefix_buf;
72 text *prefix_text, *gml_id_text;
80 Oid first_type = get_fn_expr_argtype(fcinfo->flinfo, 0);
82 if (first_type != INT4OID)
89 version = PG_GETARG_INT32(argnum++);
90 if (version != 2 && version != 3)
92 elog(ERROR,
"Only GML 2 and GML 3 are supported");
98 if (PG_ARGISNULL(argnum))
100 geom = PG_GETARG_GSERIALIZED_P(argnum++);
103 if (PG_NARGS() > argnum && !PG_ARGISNULL(argnum))
110 if (PG_NARGS() > argnum && !PG_ARGISNULL(argnum))
111 option = PG_GETARG_INT32(argnum);
115 if (PG_NARGS() > argnum && !PG_ARGISNULL(argnum))
117 prefix_text = PG_GETARG_TEXT_P(argnum);
118 if ( VARSIZE(prefix_text) == VARHDRSZ )
124 len = VARSIZE_ANY_EXHDR(prefix_text);
125 prefix_buf = palloc(len + 2);
126 memcpy(prefix_buf, VARDATA(prefix_text), len);
128 prefix_buf[len] =
':';
129 prefix_buf[len+1] =
'\0';
135 if (PG_NARGS() > argnum && !PG_ARGISNULL(argnum))
137 gml_id_text = PG_GETARG_TEXT_P(argnum);
138 if ( VARSIZE(gml_id_text) == VARHDRSZ )
144 len = VARSIZE_ANY_EXHDR(gml_id_text);
145 gml_id_buf = palloc(len+1);
146 memcpy(gml_id_buf, VARDATA(gml_id_text), len);
147 gml_id_buf[len] =
'\0';
156 srs = GetSRSCacheBySRID(fcinfo, srid,
false);
158 srs = GetSRSCacheBySRID(fcinfo, srid,
true);
165 "Options %d passed to ST_AsGML(geometry) sets "
166 "unsupported value 8",
208 int output_guess_short_srid =
LW_FALSE;
209 const char *srs = NULL;
216 geom = PG_GETARG_GSERIALIZED_P(0);
220 if ( PG_NARGS() > 1 && !PG_ARGISNULL(1) )
232 if (PG_NARGS() > 2 && !PG_ARGISNULL(2))
234 int option = PG_GETARG_INT32(2);
241 output_guess_short_srid =
LW_TRUE;
246 if (srid !=
SRID_UNKNOWN && (output_short_crs || output_long_crs))
248 srs = GetSRSCacheBySRID(fcinfo, srid, !output_long_crs);
252 elog(ERROR,
"SRID %i unknown in spatial_ref_sys table", srid);
272 PG_FREE_IF_COPY(geom, 0);
273 PG_RETURN_TEXT_P(geojson);
284 cstr = palloc0(VARSIZE_ANY_EXHDR(geojson) + 1);
285 memcpy(cstr, VARDATA(geojson), VARSIZE_ANY_EXHDR(geojson));
286 PG_RETURN_DATUM(DirectFunctionCall1(jsonb_in, PointerGetDatum(cstr)));
301 if ( PG_ARGISNULL(0) ) PG_RETURN_NULL();
303 geom = PG_GETARG_GSERIALIZED_P(0);
306 if ( PG_NARGS() > 1 && ! PG_ARGISNULL(1) )
307 relative = PG_GETARG_INT32(1) ? 1:0;
309 if ( PG_NARGS() > 2 && ! PG_ARGISNULL(2) )
329 static const char* default_defid =
"x3d:";
331 const char* defid = default_defid;
335 version = PG_GETARG_INT32(0);
338 elog(ERROR,
"Only X3D version 3 are supported");
343 if ( PG_ARGISNULL(1) ) PG_RETURN_NULL();
344 geom = PG_GETARG_GSERIALIZED_P(1);
347 if (PG_NARGS() >2 && !PG_ARGISNULL(2))
353 if (PG_NARGS() >3 && !PG_ARGISNULL(3))
354 option = PG_GETARG_INT32(3);
358 if (PG_NARGS() >4 && !PG_ARGISNULL(4))
360 defid_text = PG_GETARG_TEXT_P(4);
361 if ( VARSIZE_ANY_EXHDR(defid_text) == 0 )
368 defidbuf = palloc(VARSIZE_ANY_EXHDR(defid_text)+2);
369 memcpy(defidbuf, VARDATA(defid_text),
370 VARSIZE_ANY_EXHDR(defid_text));
372 defidbuf[VARSIZE_ANY_EXHDR(defid_text)] =
':';
373 defidbuf[VARSIZE_ANY_EXHDR(defid_text)+1] =
'\0';
381 if (lwgeom->
srid != 4326)
383 PG_FREE_IF_COPY(geom, 1);
386 elog(ERROR,
"Only SRID 4326 is supported for geocoordinates.");
404 if ( PG_ARGISNULL(0) ) PG_RETURN_NULL();
406 geom = PG_GETARG_GSERIALIZED_P(0);
408 PG_FREE_IF_COPY(geom, 0);
409 elog(ERROR,
"Only SRID 4326 is supported.");
414 if (PG_NARGS() > 1 && !PG_ARGISNULL(1))
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.
#define LW_GML_IS_DEGREE
For GML3 only, declare that datas are lat/lon.
lwvarlena_t * lwgeom_to_svg(const LWGEOM *geom, int precision, int relative)
Takes a GEOMETRY and returns a SVG representation.
void lwgeom_free(LWGEOM *geom)
lwvarlena_t * lwgeom_to_geojson(const LWGEOM *geo, const char *srs, int precision, int has_bbox)
Takes a GEOMETRY and returns a GeoJson representation.
#define LW_GML_SHORTLINE
For GML3, use <LineString> rather than <Curve> for lines.
lwvarlena_t * lwgeom_to_encoded_polyline(const LWGEOM *geom, int precision)
lwvarlena_t * 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 LW_GML_EXTENT
For GML2 and GML3, output only extent of geometry.
lwvarlena_t * lwgeom_extent_to_gml3(const LWGEOM *geom, const char *srs, int precision, int opts, const char *prefix)
lwvarlena_t * lwgeom_extent_to_gml2(const LWGEOM *geom, const char *srs, int precision, const char *prefix)
#define LW_GML_IS_DIMS
Macros for specifying GML options.
#define LW_TRUE
Return types for functions with status returns.
#define SRID_UNKNOWN
Unknown SRID value.
#define LW_X3D_USE_GEOCOORDS
lwvarlena_t * lwgeom_to_x3d3(const LWGEOM *geom, int precision, int opts, const char *defid)
lwvarlena_t * lwgeom_to_gml3(const LWGEOM *geom, const char *srs, int precision, int opts, const char *prefix, const char *id)
This library is the generic geometry handling section of PostGIS.
#define OUT_DEFAULT_DECIMAL_DIGITS
Datum geometry_to_json(PG_FUNCTION_ARGS)
PG_FUNCTION_INFO_V1(LWGEOM_asGML)
Encode feature in GML.
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)
Datum LWGEOM_asX3D(PG_FUNCTION_ARGS)