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

Definition at line 317 of file lwgeom_export.c.

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

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

Here is the call graph for this function: