42 #include <libxml/tree.h> 43 #include <libxml/parser.h> 49 #include "../postgis_config.h" 50 #include "lwgeom_pg.h" 66 #define KML_NS ((char *) "http://www.opengis.net/kml/2.2") 83 xmlNodePtr xmlroot=NULL;
87 if (PG_ARGISNULL(0)) PG_RETURN_NULL();
88 xml_input = PG_GETARG_TEXT_P(0);
90 xml_size = VARSIZE(xml_input) - VARHDRSZ;
94 xmldoc = xmlReadMemory(xml, xml_size, NULL, NULL, XML_PARSE_SAX1);
95 if (!xmldoc || (xmlroot = xmlDocGetRootElement(xmldoc)) == NULL)
99 lwpgerror(
"invalid KML representation");
133 PG_RETURN_POINTER(geom);
145 ns = xmlGetNsList(xnode->doc, xnode);
151 if (ns == NULL)
return !is_strict;
153 for (p=ns ; *p ; p++)
155 if ((*p)->href == NULL || (*p)->prefix == NULL ||
156 xnode->ns == NULL || xnode->ns->prefix == NULL)
continue;
158 if (!xmlStrcmp(xnode->ns->prefix, (*p)->prefix))
160 if (!strcmp((
char *) (*p)->href,
KML_NS))
182 static xmlChar *kmlGetProp(xmlNodePtr xnode, xmlChar *prop)
187 return xmlGetProp(xnode, prop);
189 value = xmlGetNsProp(xnode, prop, (xmlChar *)
KML_NS);
192 if (value == NULL) value = xmlGetNoNsProp(xnode, prop);
203 static double parse_kml_double(
char *d,
bool space_before,
bool space_after)
227 if (space_before)
while (isspace(*d)) d++;
228 for (st = INIT, p = d ; *p ; p++)
231 lwpgnotice(
"State: %d, *p=%c", st, *p);
235 if (st == INIT || st == NEED_DIG) st = DIG;
236 else if (st == NEED_DIG_DEC) st = DIG_DEC;
237 else if (st == NEED_DIG_EXP || st == EXP) st = DIG_EXP;
238 else if (st == DIG || st == DIG_DEC || st == DIG_EXP);
239 else lwpgerror(
"invalid KML representation");
243 if (st == DIG) st = NEED_DIG_DEC;
244 else lwpgerror(
"invalid KML representation");
246 else if (*p ==
'-' || *p ==
'+')
248 if (st == INIT) st = NEED_DIG;
249 else if (st == EXP) st = NEED_DIG_EXP;
250 else lwpgerror(
"invalid KML representation");
252 else if (*p ==
'e' || *p ==
'E')
254 if (st == DIG || st == DIG_DEC) st = EXP;
255 else lwpgerror(
"invalid KML representation");
257 else if (isspace(*p))
259 if (!space_after) lwpgerror(
"invalid KML representation");
260 if (st == DIG || st == DIG_DEC || st == DIG_EXP)st = END;
261 else if (st == NEED_DIG_DEC) st = END;
263 else lwpgerror(
"invalid KML representation");
265 else lwpgerror(
"invalid KML representation");
268 if (st != DIG && st != NEED_DIG_DEC && st != DIG_DEC && st != DIG_EXP && st != END)
269 lwpgerror(
"invalid KML representation");
284 int seen_kml_dims = 0;
290 if (xnode == NULL) lwpgerror(
"invalid KML representation");
292 for (found =
false ; xnode != NULL ; xnode = xnode->next)
294 if (xnode->type != XML_ELEMENT_NODE)
continue;
296 if (strcmp((
char *) xnode->name,
"coordinates"))
continue;
301 if (!found) lwpgerror(
"invalid KML representation");
304 kml_coord = xmlNodeGetContent(xnode);
305 p = (
char *) kml_coord;
315 while (*p && isspace(*p)) ++p;
316 for (kml_dims=0; *p ; p++)
319 if ( isdigit(*p) || *p ==
'+' || *p ==
'-' || *p ==
'.' ) {
321 errno = 0; d = strtod(p, &q);
324 lwpgerror(
"invalid KML representation");
326 if (kml_dims == 1) pt.
x = d;
327 else if (kml_dims == 2) pt.
y = d;
328 else if (kml_dims == 3) pt.
z = d;
330 lwpgerror(
"invalid KML representation");
336 if ( *q && ! isspace(*q) && *q !=
',' ) {
337 lwpgerror(
"invalid KML representation");
341 while (*q && isspace(*q)) ++q;
342 if ( isdigit(*q) || *q ==
'+' || *q ==
'-' || *q ==
'.' || ! *q ) {
343 if ( kml_dims < 2 ) lwpgerror(
"invalid KML representation");
344 else if ( kml_dims < 3 ) *hasz =
false;
345 if ( ! seen_kml_dims ) seen_kml_dims = kml_dims;
346 else if ( seen_kml_dims != kml_dims ) {
347 lwpgerror(
"invalid KML representation: mixed coordinates dimension");
354 }
else if ( *p !=
',' && ! isspace(*p) ) {
355 lwpgerror(
"invalid KML representation");
373 if (xnode->children == NULL) lwpgerror(
"invalid KML representation");
375 if (pa->
npoints != 1) lwpgerror(
"invalid KML representation");
388 if (xnode->children == NULL) lwpgerror(
"invalid KML representation");
390 if (pa->
npoints < 2) lwpgerror(
"invalid KML representation");
406 for (xa = xnode->children ; xa != NULL ; xa = xa->next)
410 if (xa->type != XML_ELEMENT_NODE)
continue;
412 if (strcmp((
char *) xa->name,
"outerBoundaryIs"))
continue;
414 for (xb = xa->children ; xb != NULL ; xb = xb->next)
417 if (xb->type != XML_ELEMENT_NODE)
continue;
419 if (strcmp((
char *) xb->name,
"LinearRing"))
continue;
424 if (ppa[0]->npoints < 4)
425 lwpgerror(
"invalid KML representation");
433 lwpgnotice(
"forced closure on an un-closed KML polygon");
439 if (outer_rings != 1)
440 lwpgerror(
"invalid KML representation");
442 for (ring=1, xa = xnode->children ; xa != NULL ; xa = xa->next)
446 if (xa->type != XML_ELEMENT_NODE)
continue;
448 if (strcmp((
char *) xa->name,
"innerBoundaryIs"))
continue;
450 for (xb = xa->children ; xb != NULL ; xb = xb->next)
453 if (xb->type != XML_ELEMENT_NODE)
continue;
455 if (strcmp((
char *) xb->name,
"LinearRing"))
continue;
460 if (ppa[ring]->npoints < 4)
461 lwpgerror(
"invalid KML representation");
469 lwpgnotice(
"forced closure on an un-closed KML polygon");
477 if (ppa == NULL || ppa[0] == NULL) lwpgerror(
"invalid KML representation");
493 for (xa = xnode->children ; xa != NULL ; xa = xa->next)
496 if (xa->type != XML_ELEMENT_NODE)
continue;
499 if ( !strcmp((
char *) xa->name,
"Point")
500 || !strcmp((
char *) xa->name,
"LineString")
501 || !strcmp((
char *) xa->name,
"Polygon")
502 || !strcmp((
char *) xa->name,
"MultiGeometry"))
505 if (xa->children == NULL)
break;
519 xmlNodePtr xa = xnode;
521 while (xa != NULL && (xa->type != XML_ELEMENT_NODE
524 if (xa == NULL) lwpgerror(
"invalid KML representation");
526 if (!strcmp((
char *) xa->name,
"Point"))
529 if (!strcmp((
char *) xa->name,
"LineString"))
532 if (!strcmp((
char *) xa->name,
"Polygon"))
535 if (!strcmp((
char *) xa->name,
"MultiGeometry"))
538 lwpgerror(
"invalid KML representation");
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. ...
int ptarray_is_closed_3d(const POINTARRAY *pa)
POINTARRAY * ptarray_construct_empty(char hasz, char hasm, uint32_t maxpoints)
Create a new POINTARRAY with no points.
void lwgeom_free(LWGEOM *geom)
PG_FUNCTION_INFO_V1(geom_from_kml)
Ability to parse KML geometry fragment and to return an LWGEOM or an error message.
static POINTARRAY * parse_kml_coordinates(xmlNodePtr xnode, bool *hasz)
Parse kml:coordinates.
static LWGEOM * parse_kml_multi(xmlNodePtr xnode, bool *hasz)
Parse KML MultiGeometry.
static LWGEOM * parse_kml_polygon(xmlNodePtr xnode, bool *hasz)
Parse KML Polygon.
int ptarray_is_closed_2d(const POINTARRAY *pa)
static LWGEOM * parse_kml(xmlNodePtr xnode, bool *hasz)
Parse KML.
LWGEOM * lwgeom_homogenize(const LWGEOM *geom)
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, then a duplicate point will not be added.
LWPOLY * lwpoly_construct(int srid, GBOX *bbox, uint32_t nrings, POINTARRAY **points)
#define LW_TRUE
Return types for functions with status returns.
LWLINE * lwline_construct(int srid, GBOX *bbox, POINTARRAY *points)
LWGEOM * lwgeom_force_2d(const LWGEOM *geom)
Strip out the Z/M components of an LWGEOM.
char * text2cstring(const text *textptr)
POINTARRAY * ptarray_clone_deep(const POINTARRAY *ptarray)
Deep clone a pointarray (also clones serialized pointlist)
static LWGEOM * parse_kml_point(xmlNodePtr xnode, bool *hasz)
Parse KML point.
GSERIALIZED * geometry_serialize(LWGEOM *lwgeom)
void * lwrealloc(void *mem, size_t size)
void lwgeom_add_bbox(LWGEOM *lwgeom)
Compute a bbox if not already computed.
void lwgeom_release(LWGEOM *lwgeom)
Free the containing LWGEOM and the associated BOX.
LWPOINT * lwpoint_construct(int srid, GBOX *bbox, POINTARRAY *point)
void * lwalloc(size_t size)
LWCOLLECTION * lwcollection_construct_empty(uint8_t type, int srid, char hasz, char hasm)
LWCOLLECTION * lwcollection_add_lwgeom(LWCOLLECTION *col, const LWGEOM *geom)
Appends geom to the collection managed by col.
static LWGEOM * parse_kml_line(xmlNodePtr xnode, bool *hasz)
Parse KML lineString.
int getPoint4d_p(const POINTARRAY *pa, int n, POINT4D *point)
This library is the generic geometry handling section of PostGIS.