PostGIS  3.4.0dev-r@@SVN_REVISION@@

◆ geography_as_gml()

Datum geography_as_gml ( PG_FUNCTION_ARGS  )

Definition at line 204 of file geography_inout.c.

205 {
206  LWGEOM *lwgeom = NULL;
207  GSERIALIZED *g = NULL;
208  lwvarlena_t *v;
209  int version;
210  const char *srs;
211  int32_t srid = SRID_DEFAULT;
212  int precision = -1;
213  int option = 0;
214  int lwopts = LW_GML_IS_DIMS;
215  static const char *default_prefix = "gml:";
216  const char *prefix = default_prefix;
217  char *prefix_buf = "";
218  text *prefix_text, *id_text = NULL;
219  const char *id = NULL;
220  char *id_buf;
221 
222  /*
223  * Two potential callers, one starts with GML version,
224  * one starts with geography, and we check for initial
225  * argument type and then dynamically change what args
226  * we read based on presence/absence
227  */
228  Oid first_type = get_fn_expr_argtype(fcinfo->flinfo, 0);
229  int argnum = 0;
230  if (first_type != INT4OID)
231  {
232  version = 2;
233  }
234  else
235  {
236  /* Get the version */
237  version = PG_GETARG_INT32(argnum++);
238  if (version != 2 && version != 3)
239  {
240  elog(ERROR, "Only GML 2 and GML 3 are supported");
241  PG_RETURN_NULL();
242  }
243  }
244 
245  /* Get the parameters, both callers have same order */
246  g = PG_GETARG_GSERIALIZED_P(argnum++);
247  precision = PG_GETARG_INT32(argnum++);
248  option = PG_GETARG_INT32(argnum++);
249  prefix_text = PG_GETARG_TEXT_P(argnum++);
250  id_text = PG_GETARG_TEXT_P(argnum++);
251 
252  /* Convert to lwgeom so we can run the old functions */
253  lwgeom = lwgeom_from_gserialized(g);
254 
255  /* Condition the prefix argument */
256  if (VARSIZE_ANY_EXHDR(prefix_text) > 0)
257  {
258  /* +2 is one for the ':' and one for term null */
259  prefix_buf = palloc(VARSIZE_ANY_EXHDR(prefix_text)+2);
260  memcpy(prefix_buf, VARDATA_ANY(prefix_text),
261  VARSIZE_ANY_EXHDR(prefix_text));
262  /* add colon and null terminate */
263  prefix_buf[VARSIZE_ANY_EXHDR(prefix_text)] = ':';
264  prefix_buf[VARSIZE_ANY_EXHDR(prefix_text)+1] = '\0';
265  prefix = prefix_buf;
266  }
267  else
268  {
269  prefix = "";
270  }
271 
272  if (VARSIZE_ANY_EXHDR(id_text) > 0)
273  {
274  id_buf = palloc(VARSIZE_ANY_EXHDR(id_text)+2);
275  memcpy(id_buf, VARDATA(id_text), VARSIZE_ANY_EXHDR(id_text));
276  id_buf[VARSIZE_ANY_EXHDR(id_text)+1] = '\0';
277  id = id_buf;
278  }
279 
280  if (option & 1)
281  srs = GetSRSCacheBySRID(fcinfo, srid, false);
282  else
283  srs = GetSRSCacheBySRID(fcinfo, srid, true);
284  if (!srs)
285  {
286  elog(ERROR, "SRID %d unknown in spatial_ref_sys table", SRID_DEFAULT);
287  PG_RETURN_NULL();
288  }
289 
290  /* Revert lat/lon only with long SRS */
291  if (option & 1) lwopts |= LW_GML_IS_DEGREE;
292  if (option & 2) lwopts &= ~LW_GML_IS_DIMS;
293  if (option & 8)
294  {
295  elog(ERROR,
296  "Options %d passed to ST_AsGML(geography) sets "
297  "unsupported value 8",
298  option);
299  PG_RETURN_NULL();
300  }
301  if ((option & 4) || (option & 16) || (option & 32))
302  {
303  elog(ERROR,
304  "Options %d passed to ST_AsGML(geography) but are only "
305  "applicable to ST_AsGML(geometry)",
306  option);
307  PG_RETURN_NULL();
308  }
309 
310  if (version == 2)
311  v = lwgeom_to_gml2(lwgeom, srs, precision, prefix);
312  else
313  v = lwgeom_to_gml3(lwgeom, srs, precision, lwopts, prefix, id);
314 
315  if (!v)
316  PG_RETURN_NULL();
317  else
318  PG_RETURN_TEXT_P(v);
319 }
static uint8_t precision
Definition: cu_in_twkb.c:25
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
Definition: gserialized.c:239
#define LW_GML_IS_DEGREE
For GML3 only, declare that datas are lat/lon.
Definition: liblwgeom.h:1684
lwvarlena_t * lwgeom_to_gml2(const LWGEOM *geom, const char *srs, int precision, const char *prefix)
Definition: lwout_gml.c:920
#define SRID_DEFAULT
Definition: liblwgeom.h:225
#define LW_GML_IS_DIMS
Macros for specifying GML options.
Definition: liblwgeom.h:1682
lwvarlena_t * lwgeom_to_gml3(const LWGEOM *geom, const char *srs, int precision, int opts, const char *prefix, const char *id)
Definition: lwout_gml.c:978

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

Here is the call graph for this function: