PostGIS  2.3.8dev-r@@SVN_REVISION@@

◆ LWGEOM_asKML()

Datum LWGEOM_asKML ( PG_FUNCTION_ARGS  )

Definition at line 306 of file lwgeom_export.c.

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

Referenced by LWGEOM_asGML().

307 {
308  GSERIALIZED *geom;
309  LWGEOM *lwgeom;
310  char *kml;
311  text *result;
312  int version;
313  int precision = DBL_DIG;
314  static const char* default_prefix = ""; /* default prefix */
315  char *prefixbuf;
316  const char* prefix = default_prefix;
317  text *prefix_text;
318 
319 
320  /* Get the version */
321  version = PG_GETARG_INT32(0);
322  if ( version != 2)
323  {
324  elog(ERROR, "Only KML 2 is supported");
325  PG_RETURN_NULL();
326  }
327 
328  /* Get the geometry */
329  if ( PG_ARGISNULL(1) ) PG_RETURN_NULL();
330  geom = PG_GETARG_GSERIALIZED_P(1);
331 
332  /* Retrieve precision if any (default is max) */
333  if (PG_NARGS() >2 && !PG_ARGISNULL(2))
334  {
335  /* TODO: leave this to liblwgeom ? */
336  precision = PG_GETARG_INT32(2);
337  if ( precision > DBL_DIG )
338  precision = DBL_DIG;
339  else if ( precision < 0 ) precision = 0;
340  }
341 
342  /* retrieve prefix */
343  if (PG_NARGS() >3 && !PG_ARGISNULL(3))
344  {
345  prefix_text = PG_GETARG_TEXT_P(3);
346  if ( VARSIZE(prefix_text)-VARHDRSZ == 0 )
347  {
348  prefix = "";
349  }
350  else
351  {
352  /* +2 is one for the ':' and one for term null */
353  prefixbuf = palloc(VARSIZE(prefix_text)-VARHDRSZ+2);
354  memcpy(prefixbuf, VARDATA(prefix_text),
355  VARSIZE(prefix_text)-VARHDRSZ);
356  /* add colon and null terminate */
357  prefixbuf[VARSIZE(prefix_text)-VARHDRSZ] = ':';
358  prefixbuf[VARSIZE(prefix_text)-VARHDRSZ+1] = '\0';
359  prefix = prefixbuf;
360  }
361  }
362 
363  lwgeom = lwgeom_from_gserialized(geom);
364  kml = lwgeom_to_kml2(lwgeom, precision, prefix);
365  lwgeom_free(lwgeom);
366  PG_FREE_IF_COPY(geom, 1);
367 
368  if( ! kml )
369  PG_RETURN_NULL();
370 
371  result = cstring2text(kml);
372  lwfree(kml);
373 
374  PG_RETURN_POINTER(result);
375 }
void lwfree(void *mem)
Definition: lwutil.c:242
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1063
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:
Here is the caller graph for this function: