PostGIS  3.3.9dev-r@@SVN_REVISION@@

◆ LWGEOM_asKML()

Datum LWGEOM_asKML ( PG_FUNCTION_ARGS  )

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

171 {
172  LWGEOM *lwgeom;
173  lwvarlena_t *kml;
174  const char *default_prefix = ""; /* default prefix */
175  char *prefixbuf;
176  const char *prefix = default_prefix;
177  int32_t srid_from;
178  const int32_t srid_to = 4326;
179 
180  /* Get the geometry */
181  GSERIALIZED *geom = PG_GETARG_GSERIALIZED_P_COPY(0);
182  int precision = PG_GETARG_INT32(1);
183  text *prefix_text = PG_GETARG_TEXT_P(2);
184  srid_from = gserialized_get_srid(geom);
185 
186  if ( srid_from == SRID_UNKNOWN )
187  {
188  PG_FREE_IF_COPY(geom, 0);
189  elog(ERROR, "ST_AsKML: Input geometry has unknown (%d) SRID", SRID_UNKNOWN);
190  PG_RETURN_NULL();
191  }
192 
193  /* Condition precision */
194  if (precision < 0)
195  precision = 0;
196 
197  if (VARSIZE_ANY_EXHDR(prefix_text) > 0)
198  {
199  /* +2 is one for the ':' and one for term null */
200  prefixbuf = palloc(VARSIZE_ANY_EXHDR(prefix_text)+2);
201  memcpy(prefixbuf, VARDATA(prefix_text),
202  VARSIZE_ANY_EXHDR(prefix_text));
203  /* add colon and null terminate */
204  prefixbuf[VARSIZE_ANY_EXHDR(prefix_text)] = ':';
205  prefixbuf[VARSIZE_ANY_EXHDR(prefix_text)+1] = '\0';
206  prefix = prefixbuf;
207  }
208 
209  lwgeom = lwgeom_from_gserialized(geom);
210 
211  if (srid_from != srid_to)
212  {
213  LWPROJ *pj;
214  if (lwproj_lookup(srid_from, srid_to, &pj) == LW_FAILURE)
215  {
216  PG_FREE_IF_COPY(geom, 0);
217  elog(ERROR, "ST_AsKML: Failure reading projections from spatial_ref_sys.");
218  PG_RETURN_NULL();
219  }
220  lwgeom_transform(lwgeom, pj);
221  }
222 
223  kml = lwgeom_to_kml2(lwgeom, precision, prefix);
224  if (kml)
225  PG_RETURN_TEXT_P(kml);
226  PG_RETURN_NULL();
227 }
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:126
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
Definition: gserialized.c:239
lwvarlena_t * lwgeom_to_kml2(const LWGEOM *geom, int precision, const char *prefix)
Definition: lwout_kml.c:44
#define LW_FAILURE
Definition: liblwgeom.h:111
int lwgeom_transform(LWGEOM *geom, LWPROJ *pj)
Transform (reproject) a geometry in-place.
#define SRID_UNKNOWN
Unknown SRID value.
Definition: liblwgeom.h:230

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: