PostGIS  2.2.8dev-r@@SVN_REVISION@@

◆ geography_as_kml()

Datum geography_as_kml ( PG_FUNCTION_ARGS  )

Definition at line 319 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().

320 {
321  GSERIALIZED *g = NULL;
322  LWGEOM *lwgeom = NULL;
323  char *kml;
324  text *result;
325  int version;
326  int precision = DBL_DIG;
327  static const char *default_prefix = "";
328  char *prefixbuf;
329  const char* prefix = default_prefix;
330  text *prefix_text;
331 
332 
333  /* Get the version */
334  version = PG_GETARG_INT32(0);
335  if ( version != 2)
336  {
337  elog(ERROR, "Only KML 2 is supported");
338  PG_RETURN_NULL();
339  }
340 
341  /* Get the geometry */
342  if ( PG_ARGISNULL(1) ) PG_RETURN_NULL();
343  g = PG_GETARG_GSERIALIZED_P(1);
344 
345  /* Convert to lwgeom so we can run the old functions */
346  lwgeom = lwgeom_from_gserialized(g);
347 
348  /* Retrieve precision if any (default is max) */
349  if (PG_NARGS() >2 && !PG_ARGISNULL(2))
350  {
351  precision = PG_GETARG_INT32(2);
352  /* TODO: leave this to liblwgeom */
353  if ( precision > DBL_DIG )
354  precision = DBL_DIG;
355  else if ( precision < 0 ) precision = 0;
356  }
357 
358  /* retrieve prefix */
359  if (PG_NARGS() >3 && !PG_ARGISNULL(3))
360  {
361  prefix_text = PG_GETARG_TEXT_P(3);
362  if ( VARSIZE(prefix_text)-VARHDRSZ == 0 )
363  {
364  prefix = "";
365  }
366  else
367  {
368  /* +2 is one for the ':' and one for term null */
369  prefixbuf = palloc(VARSIZE(prefix_text)-VARHDRSZ+2);
370  memcpy(prefixbuf, VARDATA(prefix_text),
371  VARSIZE(prefix_text)-VARHDRSZ);
372  /* add colon and null terminate */
373  prefixbuf[VARSIZE(prefix_text)-VARHDRSZ] = ':';
374  prefixbuf[VARSIZE(prefix_text)-VARHDRSZ+1] = '\0';
375  prefix = prefixbuf;
376  }
377  }
378 
379  kml = lwgeom_to_kml2(lwgeom, precision, prefix);
380 
381  lwgeom_free(lwgeom);
382  PG_FREE_IF_COPY(g, 1);
383 
384  if ( ! kml )
385  PG_RETURN_NULL();
386 
387  result = cstring2text(kml);
388  lwfree(kml);
389 
390  PG_RETURN_TEXT_P(result);
391 }
void lwfree(void *mem)
Definition: lwutil.c:214
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1050
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:31
Here is the call graph for this function:
Here is the caller graph for this function: