42 #include <libxml/tree.h>
43 #include <libxml/parser.h>
47 #include "utils/builtins.h"
49 #include "../postgis_config.h"
50 #include "lwgeom_pg.h"
66 #define KML_NS ((char *) "http://www.opengis.net/kml/2.2")
76 if (!xn || xn->type != XML_ELEMENT_NODE)
82 node_name = (
char*)xn->name;
83 colon_pos = strchr(node_name,
':');
85 node_name = colon_pos + 1;
87 return strcmp(node_name, kml_name) == 0;
105 xmlNodePtr xmlroot=NULL;
108 if (PG_ARGISNULL(0)) PG_RETURN_NULL();
109 xml_input = PG_GETARG_TEXT_P(0);
111 xml_size = VARSIZE_ANY_EXHDR(xml_input);
115 xmldoc = xmlReadMemory(xml, xml_size, NULL, NULL, 0);
117 if (!xmldoc || (xmlroot = xmlDocGetRootElement(xmldoc)) == NULL)
121 lwpgerror(
"invalid KML representation");
155 PG_RETURN_POINTER(geom);
167 ns = xmlGetNsList(xnode->doc, xnode);
173 if (ns == NULL)
return !is_strict;
175 for (p=ns ; *p ; p++)
177 if ((*p)->href == NULL || (*p)->prefix == NULL ||
178 xnode->ns == NULL || xnode->ns->prefix == NULL)
continue;
180 if (!xmlStrcmp(xnode->ns->prefix, (*p)->prefix))
182 if (!strcmp((
char *) (*p)->href,
KML_NS))
204 static xmlChar *kmlGetProp(xmlNodePtr xnode, xmlChar *prop)
209 return xmlGetProp(xnode, prop);
211 value = xmlGetNsProp(xnode, prop, (xmlChar *)
KML_NS);
214 if (
value == NULL)
value = xmlGetNoNsProp(xnode, prop);
225 static double parse_kml_double(
char *d,
bool space_before,
bool space_after)
249 if (space_before)
while (isspace(*d)) d++;
250 for (st = INIT, p = d ; *p ; p++)
253 lwpgnotice(
"State: %d, *p=%c", st, *p);
257 if (st == INIT || st == NEED_DIG) st = DIG;
258 else if (st == NEED_DIG_DEC) st = DIG_DEC;
259 else if (st == NEED_DIG_EXP || st == EXP) st = DIG_EXP;
260 else if (st == DIG || st == DIG_DEC || st == DIG_EXP);
261 else lwpgerror(
"invalid KML representation");
265 if (st == DIG) st = NEED_DIG_DEC;
266 else lwpgerror(
"invalid KML representation");
268 else if (*p ==
'-' || *p ==
'+')
270 if (st == INIT) st = NEED_DIG;
271 else if (st == EXP) st = NEED_DIG_EXP;
272 else lwpgerror(
"invalid KML representation");
274 else if (*p ==
'e' || *p ==
'E')
276 if (st == DIG || st == DIG_DEC) st = EXP;
277 else lwpgerror(
"invalid KML representation");
279 else if (isspace(*p))
281 if (!space_after) lwpgerror(
"invalid KML representation");
282 if (st == DIG || st == DIG_DEC || st == DIG_EXP)st = END;
283 else if (st == NEED_DIG_DEC) st = END;
285 else lwpgerror(
"invalid KML representation");
287 else lwpgerror(
"invalid KML representation");
290 if (st != DIG && st != NEED_DIG_DEC && st != DIG_DEC && st != DIG_EXP && st != END)
291 lwpgerror(
"invalid KML representation");
306 int seen_kml_dims = 0;
312 if (xnode == NULL) lwpgerror(
"invalid KML representation");
314 for (found =
false ; xnode != NULL ; xnode = xnode->next)
316 if (xnode->type != XML_ELEMENT_NODE)
continue;
323 if (!found) lwpgerror(
"invalid KML representation");
326 kml_coord = xmlNodeGetContent(xnode);
327 p = (
char *) kml_coord;
337 while (*p && isspace(*p)) ++p;
338 for (kml_dims=0; *p ; p++)
341 if ( isdigit(*p) || *p ==
'+' || *p ==
'-' || *p ==
'.' ) {
343 errno = 0; d = strtod(p, &q);
346 lwpgerror(
"invalid KML representation");
348 if (kml_dims == 1) pt.
x = d;
349 else if (kml_dims == 2) pt.
y = d;
350 else if (kml_dims == 3) pt.
z = d;
352 lwpgerror(
"invalid KML representation");
358 if ( *q && ! isspace(*q) && *q !=
',' ) {
359 lwpgerror(
"invalid KML representation");
363 while (*q && isspace(*q)) ++q;
364 if ( isdigit(*q) || *q ==
'+' || *q ==
'-' || *q ==
'.' || ! *q ) {
365 if ( kml_dims < 2 ) lwpgerror(
"invalid KML representation");
366 else if ( kml_dims < 3 ) *hasz =
false;
367 if ( ! seen_kml_dims ) seen_kml_dims = kml_dims;
368 else if ( seen_kml_dims != kml_dims ) {
369 lwpgerror(
"invalid KML representation: mixed coordinates dimension");
376 }
else if ( *p !=
',' && ! isspace(*p) ) {
377 lwpgerror(
"invalid KML representation");
395 if (xnode->children == NULL) lwpgerror(
"invalid KML representation");
397 if (pa->
npoints != 1) lwpgerror(
"invalid KML representation");
410 if (xnode->children == NULL) lwpgerror(
"invalid KML representation");
412 if (pa->
npoints < 2) lwpgerror(
"invalid KML representation");
428 for (xa = xnode->children ; xa != NULL ; xa = xa->next)
432 if (xa->type != XML_ELEMENT_NODE)
continue;
436 for (xb = xa->children ; xb != NULL ; xb = xb->next)
439 if (xb->type != XML_ELEMENT_NODE)
continue;
446 if (ppa[0]->npoints < 4)
447 lwpgerror(
"invalid KML representation");
455 lwpgnotice(
"forced closure on an un-closed KML polygon");
461 if (outer_rings != 1)
462 lwpgerror(
"invalid KML representation");
464 for (ring=1, xa = xnode->children ; xa != NULL ; xa = xa->next)
468 if (xa->type != XML_ELEMENT_NODE)
continue;
472 for (xb = xa->children ; xb != NULL ; xb = xb->next)
475 if (xb->type != XML_ELEMENT_NODE)
continue;
482 if (ppa[ring]->npoints < 4)
483 lwpgerror(
"invalid KML representation");
491 lwpgnotice(
"forced closure on an un-closed KML polygon");
499 if (ppa == NULL || ppa[0] == NULL) lwpgerror(
"invalid KML representation");
515 for (xa = xnode->children ; xa != NULL ; xa = xa->next)
518 if (xa->type != XML_ELEMENT_NODE)
continue;
527 if (xa->children == NULL)
break;
541 xmlNodePtr xa = xnode;
543 while (xa != NULL && (xa->type != XML_ELEMENT_NODE
546 if (xa == NULL) lwpgerror(
"invalid KML representation");
560 lwpgerror(
"invalid KML representation");
void lwgeom_free(LWGEOM *geom)
int ptarray_is_closed_3d(const POINTARRAY *pa)
POINTARRAY * ptarray_clone_deep(const POINTARRAY *ptarray)
Deep clone a pointarray (also clones serialized pointlist)
LWLINE * lwline_construct(int32_t srid, GBOX *bbox, POINTARRAY *points)
void * lwrealloc(void *mem, size_t size)
LWGEOM * lwgeom_homogenize(const LWGEOM *geom)
LWPOINT * lwpoint_construct(int32_t srid, GBOX *bbox, POINTARRAY *point)
POINTARRAY * ptarray_construct_empty(char hasz, char hasm, uint32_t maxpoints)
Create a new POINTARRAY with no points.
LWCOLLECTION * lwcollection_construct_empty(uint8_t type, int32_t srid, char hasz, char hasm)
int getPoint4d_p(const POINTARRAY *pa, uint32_t n, POINT4D *point)
int ptarray_append_point(POINTARRAY *pa, const POINT4D *pt, int allow_duplicates)
Append a point to the end of an existing POINTARRAY If allow_duplicate is LW_FALSE,...
LWCOLLECTION * lwcollection_add_lwgeom(LWCOLLECTION *col, const LWGEOM *geom)
Appends geom to the collection managed by col.
void * lwalloc(size_t size)
int ptarray_is_closed_2d(const POINTARRAY *pa)
#define LW_TRUE
Return types for functions with status returns.
void lwgeom_release(LWGEOM *lwgeom)
Free the containing LWGEOM and the associated BOX.
LWPOLY * lwpoly_construct(int32_t srid, GBOX *bbox, uint32_t nrings, POINTARRAY **points)
void lwgeom_add_bbox(LWGEOM *lwgeom)
Compute a bbox if not already computed.
LWGEOM * lwgeom_force_2d(const LWGEOM *geom)
Strip out the Z/M components of an LWGEOM.
This library is the generic geometry handling section of PostGIS.
static LWGEOM * parse_kml(xmlNodePtr xnode, bool *hasz)
Parse KML.
static LWGEOM * parse_kml_line(xmlNodePtr xnode, bool *hasz)
Parse KML lineString.
static POINTARRAY * parse_kml_coordinates(xmlNodePtr xnode, bool *hasz)
Parse kml:coordinates.
PG_FUNCTION_INFO_V1(geom_from_kml)
Ability to parse KML geometry fragment and to return an LWGEOM or an error message.
static LWGEOM * parse_kml_multi(xmlNodePtr xnode, bool *hasz)
Parse KML MultiGeometry.
static LWGEOM * parse_kml_polygon(xmlNodePtr xnode, bool *hasz)
Parse KML Polygon.
static LWGEOM * parse_kml_point(xmlNodePtr xnode, bool *hasz)
Parse KML point.
Datum geom_from_kml(PG_FUNCTION_ARGS)
static bool is_kml_namespace(xmlNodePtr xnode, bool is_strict)
Return false if current element namespace is not a KML one Return true otherwise.
static bool is_kml_element(xmlNodePtr xn, const char *kml_name)
char * text_to_cstring(const text *textptr)
GSERIALIZED * geometry_serialize(LWGEOM *lwgeom)