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

Definition at line 320 of file geography_inout.c.

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

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

Here is the call graph for this function: