PostGIS  2.5.0dev-r@@SVN_REVISION@@
Datum geography_as_kml ( PG_FUNCTION_ARGS  )

Definition at line 350 of file geography_inout.c.

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

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

Here is the call graph for this function: