PostGIS  2.5.7dev-r@@SVN_REVISION@@

◆ LWGEOM_asKML()

Datum LWGEOM_asKML ( PG_FUNCTION_ARGS  )

Definition at line 343 of file lwgeom_export.c.

344 {
345  GSERIALIZED *geom;
346  LWGEOM *lwgeom;
347  char *kml;
348  text *result;
349  int version;
350  int precision = DBL_DIG;
351  static const char* default_prefix = ""; /* default prefix */
352  char *prefixbuf;
353  const char* prefix = default_prefix;
354  text *prefix_text;
355 
356 
357  /* Get the version */
358  version = PG_GETARG_INT32(0);
359  if ( version != 2)
360  {
361  elog(ERROR, "Only KML 2 is supported");
362  PG_RETURN_NULL();
363  }
364 
365  /* Get the geometry */
366  if ( PG_ARGISNULL(1) ) PG_RETURN_NULL();
367  geom = PG_GETARG_GSERIALIZED_P(1);
368 
369  /* Retrieve precision if any (default is max) */
370  if (PG_NARGS() >2 && !PG_ARGISNULL(2))
371  {
372  /* TODO: leave this to liblwgeom ? */
373  precision = PG_GETARG_INT32(2);
374  if ( precision > DBL_DIG )
375  precision = DBL_DIG;
376  else if ( precision < 0 ) precision = 0;
377  }
378 
379  /* retrieve prefix */
380  if (PG_NARGS() >3 && !PG_ARGISNULL(3))
381  {
382  prefix_text = PG_GETARG_TEXT_P(3);
383  if ( VARSIZE(prefix_text)-VARHDRSZ == 0 )
384  {
385  prefix = "";
386  }
387  else
388  {
389  /* +2 is one for the ':' and one for term null */
390  prefixbuf = palloc(VARSIZE(prefix_text)-VARHDRSZ+2);
391  memcpy(prefixbuf, VARDATA(prefix_text),
392  VARSIZE(prefix_text)-VARHDRSZ);
393  /* add colon and null terminate */
394  prefixbuf[VARSIZE(prefix_text)-VARHDRSZ] = ':';
395  prefixbuf[VARSIZE(prefix_text)-VARHDRSZ+1] = '\0';
396  prefix = prefixbuf;
397  }
398  }
399 
400  lwgeom = lwgeom_from_gserialized(geom);
401  kml = lwgeom_to_kml2(lwgeom, precision, prefix);
402  lwgeom_free(lwgeom);
403  PG_FREE_IF_COPY(geom, 1);
404 
405  if( ! kml )
406  PG_RETURN_NULL();
407 
408  result = cstring_to_text(kml);
409  lwfree(kml);
410 
411  PG_RETURN_POINTER(result);
412 }
static uint8_t precision
Definition: cu_in_twkb.c:25
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1144
char * lwgeom_to_kml2(const LWGEOM *geom, int precision, const char *prefix)
Definition: lwout_kml.c:43
void lwfree(void *mem)
Definition: lwutil.c:244

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

Here is the call graph for this function: