PostGIS  2.2.7dev-r@@SVN_REVISION@@
Datum LWGEOM_asKML ( PG_FUNCTION_ARGS  )

Definition at line 291 of file lwgeom_export.c.

References dumpnode::geom, lwfree(), lwgeom_free(), lwgeom_from_gserialized(), lwgeom_to_kml2(), and precision.

292 {
293  GSERIALIZED *geom;
294  LWGEOM *lwgeom;
295  char *kml;
296  text *result;
297  int version;
298  int precision = DBL_DIG;
299  static const char* default_prefix = ""; /* default prefix */
300  char *prefixbuf;
301  const char* prefix = default_prefix;
302  text *prefix_text;
303 
304 
305  /* Get the version */
306  version = PG_GETARG_INT32(0);
307  if ( version != 2)
308  {
309  elog(ERROR, "Only KML 2 is supported");
310  PG_RETURN_NULL();
311  }
312 
313  /* Get the geometry */
314  if ( PG_ARGISNULL(1) ) PG_RETURN_NULL();
315  geom = PG_GETARG_GSERIALIZED_P(1);
316 
317  /* Retrieve precision if any (default is max) */
318  if (PG_NARGS() >2 && !PG_ARGISNULL(2))
319  {
320  /* TODO: leave this to liblwgeom ? */
321  precision = PG_GETARG_INT32(2);
322  if ( precision > DBL_DIG )
323  precision = DBL_DIG;
324  else if ( precision < 0 ) precision = 0;
325  }
326 
327  /* retrieve prefix */
328  if (PG_NARGS() >3 && !PG_ARGISNULL(3))
329  {
330  prefix_text = PG_GETARG_TEXT_P(3);
331  if ( VARSIZE(prefix_text)-VARHDRSZ == 0 )
332  {
333  prefix = "";
334  }
335  else
336  {
337  /* +2 is one for the ':' and one for term null */
338  prefixbuf = palloc(VARSIZE(prefix_text)-VARHDRSZ+2);
339  memcpy(prefixbuf, VARDATA(prefix_text),
340  VARSIZE(prefix_text)-VARHDRSZ);
341  /* add colon and null terminate */
342  prefixbuf[VARSIZE(prefix_text)-VARHDRSZ] = ':';
343  prefixbuf[VARSIZE(prefix_text)-VARHDRSZ+1] = '\0';
344  prefix = prefixbuf;
345  }
346  }
347 
348  lwgeom = lwgeom_from_gserialized(geom);
349  kml = lwgeom_to_kml2(lwgeom, precision, prefix);
350  lwgeom_free(lwgeom);
351  PG_FREE_IF_COPY(geom, 1);
352 
353  if( ! kml )
354  PG_RETURN_NULL();
355 
356  result = cstring2text(kml);
357  lwfree(kml);
358 
359  PG_RETURN_POINTER(result);
360 }
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
LWGEOM * geom
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: