PostGIS  2.5.7dev-r@@SVN_REVISION@@

◆ geography_as_gml()

Datum geography_as_gml ( PG_FUNCTION_ARGS  )

Definition at line 210 of file geography_inout.c.

211 {
212  LWGEOM *lwgeom = NULL;
213  GSERIALIZED *g = NULL;
214  char *gml;
215  text *result;
216  int version;
217  char *srs;
218  int srid = SRID_DEFAULT;
219  int precision = DBL_DIG;
220  int option = 0;
221  int lwopts = LW_GML_IS_DIMS;
222  static const char *default_prefix = "gml:";
223  const char *prefix = default_prefix;
224  char *prefix_buf = "";
225  text *prefix_text, *id_text = NULL;
226  const char *id=NULL;
227  char *id_buf;
228 
229 
230  /* Get the version */
231  version = PG_GETARG_INT32(0);
232  if ( version != 2 && version != 3 )
233  {
234  elog(ERROR, "Only GML 2 and GML 3 are supported");
235  PG_RETURN_NULL();
236  }
237 
238  /* Get the geography */
239  if ( PG_ARGISNULL(1) ) PG_RETURN_NULL();
240  g = PG_GETARG_GSERIALIZED_P(1);
241 
242  /* Convert to lwgeom so we can run the old functions */
243  lwgeom = lwgeom_from_gserialized(g);
244 
245  /* Retrieve precision if any (default is max) */
246  if (PG_NARGS() >2 && !PG_ARGISNULL(2))
247  {
248  precision = PG_GETARG_INT32(2);
249  /* TODO: leave this to liblwgeom */
250  if ( precision > DBL_DIG )
251  precision = DBL_DIG;
252  else if ( precision < 0 ) precision = 0;
253  }
254 
255  /* retrieve option */
256  if (PG_NARGS() >3 && !PG_ARGISNULL(3))
257  option = PG_GETARG_INT32(3);
258 
259 
260  /* retrieve prefix */
261  if (PG_NARGS() >4 && !PG_ARGISNULL(4))
262  {
263  prefix_text = PG_GETARG_TEXT_P(4);
264  if ( VARSIZE(prefix_text)-VARHDRSZ == 0 )
265  {
266  prefix = "";
267  }
268  else
269  {
270  /* +2 is one for the ':' and one for term null */
271  prefix_buf = palloc(VARSIZE(prefix_text)-VARHDRSZ+2);
272  memcpy(prefix_buf, VARDATA(prefix_text),
273  VARSIZE(prefix_text)-VARHDRSZ);
274  /* add colon and null terminate */
275  prefix_buf[VARSIZE(prefix_text)-VARHDRSZ] = ':';
276  prefix_buf[VARSIZE(prefix_text)-VARHDRSZ+1] = '\0';
277  prefix = prefix_buf;
278  }
279  }
280 
281  /* retrieve id */
282  if (PG_NARGS() >5 && !PG_ARGISNULL(5))
283  {
284  id_text = PG_GETARG_TEXT_P(5);
285  if ( VARSIZE(id_text)-VARHDRSZ == 0 )
286  {
287  id = "";
288  }
289  else
290  {
291  id_buf = palloc(VARSIZE(id_text)-VARHDRSZ+1);
292  memcpy(id_buf, VARDATA(id_text), VARSIZE(id_text)-VARHDRSZ);
293  prefix_buf[VARSIZE(id_text)-VARHDRSZ+1] = '\0';
294  id = id_buf;
295  }
296  }
297 
298  if (option & 1)
299  srs = getSRSbySRID(fcinfo, srid, false);
300  else
301  srs = getSRSbySRID(fcinfo, srid, true);
302  if (!srs)
303  {
304  elog(ERROR, "SRID %d unknown in spatial_ref_sys table", SRID_DEFAULT);
305  PG_RETURN_NULL();
306  }
307 
308  /* Revert lat/lon only with long SRS */
309  if (option & 1) lwopts |= LW_GML_IS_DEGREE;
310  if (option & 2) lwopts &= ~LW_GML_IS_DIMS;
311  if (option & 8)
312  {
313  elog(ERROR,
314  "Options %d passed to ST_AsGML(geography) sets "
315  "unsupported value 8",
316  option);
317  PG_RETURN_NULL();
318  }
319  if ((option & 4) || (option & 16) || (option & 32))
320  {
321  elog(ERROR,
322  "Options %d passed to ST_AsGML(geography) but are only "
323  "applicable to ST_AsGML(geometry)",
324  option);
325  PG_RETURN_NULL();
326  }
327 
328  if (version == 2)
329  gml = lwgeom_to_gml2(lwgeom, srs, precision, prefix);
330  else
331  gml = lwgeom_to_gml3(lwgeom, srs, precision, lwopts, prefix, id);
332 
333  lwgeom_free(lwgeom);
334  PG_FREE_IF_COPY(g, 1);
335 
336  /* Return null on null */
337  if ( ! gml )
338  PG_RETURN_NULL();
339 
340  /* Turn string result into text for return */
341  result = cstring_to_text(gml);
342  lwfree(gml);
343 
344  PG_RETURN_TEXT_P(result);
345 }
static uint8_t precision
Definition: cu_in_twkb.c:25
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.
Definition: liblwgeom.h:1544
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1144
char * lwgeom_to_gml3(const LWGEOM *geom, const char *srs, int precision, int opts, const char *prefix, const char *id)
Definition: lwout_gml.c:717
void lwfree(void *mem)
Definition: lwutil.c:244
#define SRID_DEFAULT
Definition: liblwgeom.h:198
#define LW_GML_IS_DIMS
Macros for specifying GML options.
Definition: liblwgeom.h:1542
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.
Definition: lwout_gml.c:231
char * getSRSbySRID(FunctionCallInfo fcinfo, int srid, bool short_crs)
Definition: lwgeom_export.c:60

References getSRSbySRID(), LW_GML_IS_DEGREE, LW_GML_IS_DIMS, lwfree(), lwgeom_free(), lwgeom_from_gserialized(), lwgeom_to_gml2(), lwgeom_to_gml3(), precision, and SRID_DEFAULT.

Here is the call graph for this function: