PostGIS  2.5.0beta2dev-r@@SVN_REVISION@@

◆ LWGEOM_asKML()

Datum LWGEOM_asKML ( PG_FUNCTION_ARGS  )

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

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