PostGIS  3.7.0dev-r@@SVN_REVISION@@

◆ LWGEOM_asKML()

Datum LWGEOM_asKML ( PG_FUNCTION_ARGS  )

Definition at line 252 of file postgis/lwgeom_transform.c.

253 {
254  LWGEOM *lwgeom;
255  lwvarlena_t *kml;
256  const char *default_prefix = ""; /* default prefix */
257  char *prefixbuf;
258  const char *prefix = default_prefix;
259  int32_t srid_from;
260  const int32_t srid_to = 4326;
261 
262  /* Get the geometry */
263  GSERIALIZED *geom = PG_GETARG_GSERIALIZED_P_COPY(0);
264  int precision = PG_GETARG_INT32(1);
265  text *prefix_text = PG_GETARG_TEXT_P(2);
266  srid_from = gserialized_get_srid(geom);
267 
268  if ( srid_from == SRID_UNKNOWN )
269  {
270  PG_FREE_IF_COPY(geom, 0);
271  elog(ERROR, "ST_AsKML: Input geometry has unknown (%d) SRID", SRID_UNKNOWN);
272  PG_RETURN_NULL();
273  }
274 
275  /* Condition precision */
276  if (precision < 0)
277  precision = 0;
278 
279  if (VARSIZE_ANY_EXHDR(prefix_text) > 0)
280  {
281  /* +2 is one for the ':' and one for term null */
282  prefixbuf = palloc(VARSIZE_ANY_EXHDR(prefix_text)+2);
283  memcpy(prefixbuf, VARDATA(prefix_text),
284  VARSIZE_ANY_EXHDR(prefix_text));
285  /* add colon and null terminate */
286  prefixbuf[VARSIZE_ANY_EXHDR(prefix_text)] = ':';
287  prefixbuf[VARSIZE_ANY_EXHDR(prefix_text)+1] = '\0';
288  prefix = prefixbuf;
289  }
290 
291  lwgeom = lwgeom_from_gserialized(geom);
292 
293  if (srid_from != srid_to)
294  {
295  LWPROJ *pj;
296  if (lwproj_lookup(srid_from, srid_to, &pj) == LW_FAILURE)
297  {
298  PG_FREE_IF_COPY(geom, 0);
299  elog(ERROR, "ST_AsKML: Failure reading projections from spatial_ref_sys.");
300  PG_RETURN_NULL();
301  }
302  lwgeom_transform(lwgeom, pj);
303  }
304 
305  kml = lwgeom_to_kml2(lwgeom, precision, prefix);
306  if (kml)
307  PG_RETURN_TEXT_P(kml);
308  PG_RETURN_NULL();
309 }
static uint8_t precision
Definition: cu_in_twkb.c:25
int32_t gserialized_get_srid(const GSERIALIZED *g)
Extract the SRID from the serialized form (it is packed into three bytes so this is a handy function)...
Definition: gserialized.c:155
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
Definition: gserialized.c:268
lwvarlena_t * lwgeom_to_kml2(const LWGEOM *geom, int precision, const char *prefix)
Definition: lwout_kml.c:44
#define LW_FAILURE
Definition: liblwgeom.h:96
int lwgeom_transform(LWGEOM *geom, LWPROJ *pj)
Transform (reproject) a geometry in-place.
#define SRID_UNKNOWN
Unknown SRID value.
Definition: liblwgeom.h:215

References gserialized_get_srid(), LW_FAILURE, lwgeom_from_gserialized(), lwgeom_to_kml2(), lwgeom_transform(), precision, and SRID_UNKNOWN.

Here is the call graph for this function: