PostGIS  2.5.7dev-r@@SVN_REVISION@@

◆ geography_as_kml()

Datum geography_as_kml ( PG_FUNCTION_ARGS  )

Definition at line 352 of file geography_inout.c.

353 {
354  GSERIALIZED *g = NULL;
355  LWGEOM *lwgeom = NULL;
356  char *kml;
357  text *result;
358  int version;
359  int precision = DBL_DIG;
360  static const char *default_prefix = "";
361  char *prefixbuf;
362  const char* prefix = default_prefix;
363  text *prefix_text;
364 
365 
366  /* Get the version */
367  version = PG_GETARG_INT32(0);
368  if ( version != 2)
369  {
370  elog(ERROR, "Only KML 2 is supported");
371  PG_RETURN_NULL();
372  }
373 
374  /* Get the geometry */
375  if ( PG_ARGISNULL(1) ) PG_RETURN_NULL();
376  g = PG_GETARG_GSERIALIZED_P(1);
377 
378  /* Convert to lwgeom so we can run the old functions */
379  lwgeom = lwgeom_from_gserialized(g);
380 
381  /* Retrieve precision if any (default is max) */
382  if (PG_NARGS() >2 && !PG_ARGISNULL(2))
383  {
384  precision = PG_GETARG_INT32(2);
385  /* TODO: leave this to liblwgeom */
386  if ( precision > DBL_DIG )
387  precision = DBL_DIG;
388  else if ( precision < 0 ) precision = 0;
389  }
390 
391  /* retrieve prefix */
392  if (PG_NARGS() >3 && !PG_ARGISNULL(3))
393  {
394  prefix_text = PG_GETARG_TEXT_P(3);
395  if ( VARSIZE(prefix_text)-VARHDRSZ == 0 )
396  {
397  prefix = "";
398  }
399  else
400  {
401  /* +2 is one for the ':' and one for term null */
402  prefixbuf = palloc(VARSIZE(prefix_text)-VARHDRSZ+2);
403  memcpy(prefixbuf, VARDATA(prefix_text),
404  VARSIZE(prefix_text)-VARHDRSZ);
405  /* add colon and null terminate */
406  prefixbuf[VARSIZE(prefix_text)-VARHDRSZ] = ':';
407  prefixbuf[VARSIZE(prefix_text)-VARHDRSZ+1] = '\0';
408  prefix = prefixbuf;
409  }
410  }
411 
412  kml = lwgeom_to_kml2(lwgeom, precision, prefix);
413 
414  lwgeom_free(lwgeom);
415  PG_FREE_IF_COPY(g, 1);
416 
417  if ( ! kml )
418  PG_RETURN_NULL();
419 
420  result = cstring_to_text(kml);
421  lwfree(kml);
422 
423  PG_RETURN_TEXT_P(result);
424 }
static uint8_t precision
Definition: cu_in_twkb.c:25
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1144
char * lwgeom_to_kml2(const LWGEOM *geom, int precision, const char *prefix)
Definition: lwout_kml.c:43
void lwfree(void *mem)
Definition: lwutil.c:244

References lwfree(), lwgeom_free(), lwgeom_from_gserialized(), lwgeom_to_kml2(), and precision.

Here is the call graph for this function: