210{
213 char *gml = NULL;
214 text *result;
215 int version;
216 char *srs;
217 int32_t srid;
218 int option = 0;
221 static const char* default_prefix = "gml:";
222 const char* prefix = default_prefix;
223 const char* gml_id = NULL;
224 size_t len;
225 char *gml_id_buf, *prefix_buf;
226 text *prefix_text, *gml_id_text;
227
228
229
230 version = PG_GETARG_INT32(0);
231 if ( version != 2 && version != 3 )
232 {
233 elog(ERROR, "Only GML 2 and GML 3 are supported");
234 PG_RETURN_NULL();
235 }
236
237
238 if ( PG_ARGISNULL(1) ) PG_RETURN_NULL();
239 geom = PG_GETARG_GSERIALIZED_P(1);
240
241
242 if (PG_NARGS() > 2 && !PG_ARGISNULL(2))
243 {
245
250 }
251
252
253 if (PG_NARGS() > 3 && !PG_ARGISNULL(3)) option = PG_GETARG_INT32(3);
254
255
256 if (PG_NARGS() >4 && !PG_ARGISNULL(4))
257 {
258 prefix_text = PG_GETARG_TEXT_P(4);
259 if ( VARSIZE(prefix_text) == VARHDRSZ )
260 {
261 prefix = "";
262 }
263 else
264 {
265 len = VARSIZE_ANY_EXHDR(prefix_text);
266 prefix_buf = palloc(len + 2);
267 memcpy(prefix_buf, VARDATA(prefix_text), len);
268
269 prefix_buf[len] = ':';
270 prefix_buf[len+1] = '\0';
271 prefix = prefix_buf;
272 }
273 }
274
275 if (PG_NARGS() >5 && !PG_ARGISNULL(5))
276 {
277 gml_id_text = PG_GETARG_TEXT_P(5);
278 if ( VARSIZE(gml_id_text) == VARHDRSZ )
279 {
280 gml_id = "";
281 }
282 else
283 {
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';
288 gml_id = gml_id_buf;
289 }
290 }
291
294 else if (option & 1)
296 else
298
299 if (option & 2) lwopts &= ~LW_GML_IS_DIMS;
301 if (option & 8)
302 {
303 elog(ERROR,
304 "Options %d passed to ST_AsGML(geography) sets "
305 "unsupported value 8",
306 option);
307 PG_RETURN_NULL();
308 }
311
313
314 if (version == 2)
315 {
319 else
321 }
322 if (version == 3)
323 {
327 else
329 lwgeom, srs,
precision, lwopts, prefix, gml_id);
330 }
331
333 PG_FREE_IF_COPY(geom, 1);
334
335
336 if ( ! gml )
337 PG_RETURN_NULL();
338
339 result = cstring_to_text(gml);
341 PG_RETURN_TEXT_P(result);
342}
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.
void lwgeom_free(LWGEOM *geom)
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 LW_GML_SHORTLINE
For GML3, use <LineString> rather than <Curve> for lines.
#define LW_GML_EXTENT
For GML2 and GML3, output only extent of geometry.
char * 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.
char * lwgeom_to_gml3(const LWGEOM *geom, const char *srs, int precision, int opts, const char *prefix, const char *id)
#define SRID_UNKNOWN
Unknown SRID value.
char * lwgeom_extent_to_gml3(const LWGEOM *geom, const char *srs, int precision, int opts, const char *prefix)
char * getSRSbySRID(FunctionCallInfo fcinfo, int32_t srid, bool short_crs)