PostGIS  2.4.9dev-r@@SVN_REVISION@@

◆ geography_as_kml()

Datum geography_as_kml ( PG_FUNCTION_ARGS  )

Definition at line 334 of file geography_inout.c.

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

Referenced by geography_as_gml().

335 {
336  GSERIALIZED *g = NULL;
337  LWGEOM *lwgeom = NULL;
338  char *kml;
339  text *result;
340  int version;
341  int precision = DBL_DIG;
342  static const char *default_prefix = "";
343  char *prefixbuf;
344  const char* prefix = default_prefix;
345  text *prefix_text;
346 
347 
348  /* Get the version */
349  version = PG_GETARG_INT32(0);
350  if ( version != 2)
351  {
352  elog(ERROR, "Only KML 2 is supported");
353  PG_RETURN_NULL();
354  }
355 
356  /* Get the geometry */
357  if ( PG_ARGISNULL(1) ) PG_RETURN_NULL();
358  g = PG_GETARG_GSERIALIZED_P(1);
359 
360  /* Convert to lwgeom so we can run the old functions */
361  lwgeom = lwgeom_from_gserialized(g);
362 
363  /* Retrieve precision if any (default is max) */
364  if (PG_NARGS() >2 && !PG_ARGISNULL(2))
365  {
366  precision = PG_GETARG_INT32(2);
367  /* TODO: leave this to liblwgeom */
368  if ( precision > DBL_DIG )
369  precision = DBL_DIG;
370  else if ( precision < 0 ) precision = 0;
371  }
372 
373  /* retrieve prefix */
374  if (PG_NARGS() >3 && !PG_ARGISNULL(3))
375  {
376  prefix_text = PG_GETARG_TEXT_P(3);
377  if ( VARSIZE(prefix_text)-VARHDRSZ == 0 )
378  {
379  prefix = "";
380  }
381  else
382  {
383  /* +2 is one for the ':' and one for term null */
384  prefixbuf = palloc(VARSIZE(prefix_text)-VARHDRSZ+2);
385  memcpy(prefixbuf, VARDATA(prefix_text),
386  VARSIZE(prefix_text)-VARHDRSZ);
387  /* add colon and null terminate */
388  prefixbuf[VARSIZE(prefix_text)-VARHDRSZ] = ':';
389  prefixbuf[VARSIZE(prefix_text)-VARHDRSZ+1] = '\0';
390  prefix = prefixbuf;
391  }
392  }
393 
394  kml = lwgeom_to_kml2(lwgeom, precision, prefix);
395 
396  lwgeom_free(lwgeom);
397  PG_FREE_IF_COPY(g, 1);
398 
399  if ( ! kml )
400  PG_RETURN_NULL();
401 
402  result = cstring2text(kml);
403  lwfree(kml);
404 
405  PG_RETURN_TEXT_P(result);
406 }
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:1099
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:
Here is the caller graph for this function: