171{
173 char *kml;
174 text *result;
175 const char *default_prefix = "";
176 char *prefixbuf;
177 const char *prefix = default_prefix;
178 int32_t srid_from;
179 const int32_t srid_to = 4326;
180
181
182 GSERIALIZED *geom = PG_GETARG_GSERIALIZED_P_COPY(0);
184 text *prefix_text = PG_GETARG_TEXT_P(2);
186
188 {
189 PG_FREE_IF_COPY(geom, 0);
190 elog(ERROR,
"ST_AsKML: Input geometry has unknown (%d) SRID",
SRID_UNKNOWN);
191 PG_RETURN_NULL();
192 }
193
194
199
200 if (VARSIZE_ANY_EXHDR(prefix_text) > 0)
201 {
202
203 prefixbuf = palloc(VARSIZE_ANY_EXHDR(prefix_text)+2);
204 memcpy(prefixbuf, VARDATA(prefix_text),
205 VARSIZE_ANY_EXHDR(prefix_text));
206
207 prefixbuf[VARSIZE_ANY_EXHDR(prefix_text)] = ':';
208 prefixbuf[VARSIZE_ANY_EXHDR(prefix_text)+1] = '\0';
209 prefix = prefixbuf;
210 }
211
213
214 if (srid_from != srid_to)
215 {
217 if (GetPJUsingFCInfo(fcinfo, srid_from, srid_to, &pj) ==
LW_FAILURE)
218 {
219 PG_FREE_IF_COPY(geom, 0);
220 elog(ERROR, "ST_AsKML: Failure reading projections from spatial_ref_sys.");
221 PG_RETURN_NULL();
222 }
224 }
225
228 PG_FREE_IF_COPY(geom, 0);
229
230 if (!kml)
231 PG_RETURN_NULL();
232
233 result = cstring_to_text(kml);
235
236 PG_RETURN_POINTER(result);
237}
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)...
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
int lwgeom_transform(LWGEOM *geom, LWPROJ *pj)
Transform (reproject) a geometry in-place.
void lwgeom_free(LWGEOM *geom)
char * lwgeom_to_kml2(const LWGEOM *geom, int precision, const char *prefix)
#define SRID_UNKNOWN
Unknown SRID value.