PostGIS  2.1.10dev-r@@SVN_REVISION@@
static POINTARRAY* parse_kml_coordinates ( xmlNodePtr  xnode,
bool *  hasz 
)
static

Parse kml:coordinates.

Definition at line 267 of file lwgeom_in_kml.c.

References is_kml_namespace(), LW_TRUE, lwerror(), ptarray_append_point(), ptarray_clone_deep(), ptarray_construct_empty(), POINT4D::x, POINT4D::y, and POINT4D::z.

Referenced by parse_kml_line(), parse_kml_point(), and parse_kml_polygon().

268 {
269  xmlChar *kml_coord;
270  bool found;
271  POINTARRAY *dpa;
272  int kml_dims;
273  char *p, *q;
274  POINT4D pt;
275  double d;
276 
277  if (xnode == NULL) lwerror("invalid KML representation");
278 
279  for (found = false ; xnode != NULL ; xnode = xnode->next)
280  {
281  if (xnode->type != XML_ELEMENT_NODE) continue;
282  if (!is_kml_namespace(xnode, false)) continue;
283  if (strcmp((char *) xnode->name, "coordinates")) continue;
284 
285  found = true;
286  break;
287  }
288  if (!found) lwerror("invalid KML representation");
289 
290  /* We begin to retrieve coordinates string */
291  kml_coord = xmlNodeGetContent(xnode);
292  p = (char *) kml_coord;
293 
294  /* KML coordinates pattern: x1,y1 x2,y2
295  * x1,y1,z1 x2,y2,z2
296  */
297 
298  /* Now we create PointArray from coordinates values */
299  /* HasZ, !HasM, 1pt */
300  dpa = ptarray_construct_empty(1, 0, 1);
301 
302  while (*p && isspace(*p)) ++p;
303  for (kml_dims=0; *p ; p++)
304  {
305 //lwnotice("*p:%c, kml_dims:%d", *p, kml_dims);
306  if ( isdigit(*p) || *p == '+' || *p == '-' || *p == '.' ) {
307  kml_dims++;
308  errno = 0; d = strtod(p, &q);
309  if ( errno != 0 ) {
310  // TODO: destroy dpa, return NULL
311  lwerror("invalid KML representation"); /*: %s", strerror(errno));*/
312  }
313  if (kml_dims == 1) pt.x = d;
314  else if (kml_dims == 2) pt.y = d;
315  else if (kml_dims == 3) pt.z = d;
316  else {
317  lwerror("invalid KML representation"); /* (more than 3 dimensions)"); */
318  // TODO: destroy dpa, return NULL
319  }
320 
321 //lwnotice("after strtod d:%f, *q:%c, kml_dims:%d", d, *q, kml_dims);
322 
323  if ( *q && ! isspace(*q) && *q != ',' ) {
324  lwerror("invalid KML representation"); /* (invalid character %c follows ordinate value)", *q); */
325  }
326 
327  /* Look-ahead to see if we're done reading */
328  while (*q && isspace(*q)) ++q;
329  if ( isdigit(*q) || *q == '+' || *q == '-' || *q == '.' || ! *q ) {
330  if ( kml_dims < 2 ) lwerror("invalid KML representation"); /* (not enough ordinates)"); */
331  if ( kml_dims < 3 ) *hasz = false;
332  ptarray_append_point(dpa, &pt, LW_TRUE);
333  kml_dims = 0;
334  }
335  p = q-1; /* will be incrementedon next iteration */
336 //lwnotice("after look-ahead *p:%c, kml_dims:%d", *p, kml_dims);
337  } else if ( *p != ',' && ! isspace(*p) ) {
338  lwerror("invalid KML representation"); /* (unexpected character %c)", *p); */
339  }
340  }
341 
342  xmlFree(kml_coord);
343 
344  /* TODO: we shouldn't need to clone here */
345  return ptarray_clone_deep(dpa);
346 }
double x
Definition: liblwgeom.h:308
static bool is_kml_namespace(xmlNodePtr xnode, bool is_strict)
Return false if current element namespace is not a KML one Return true otherwise. ...
POINTARRAY * ptarray_construct_empty(char hasz, char hasm, uint32_t maxpoints)
Create a new POINTARRAY with no points.
Definition: ptarray.c:57
void lwerror(const char *fmt,...)
Write a notice out to the error handler.
Definition: lwutil.c:67
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_TRUE, then a duplicate point will not be added.
Definition: ptarray.c:141
#define LW_TRUE
Return types for functions with status returns.
Definition: liblwgeom.h:51
POINTARRAY * ptarray_clone_deep(const POINTARRAY *ptarray)
Deep clone a pointarray (also clones serialized pointlist)
Definition: ptarray.c:619
double z
Definition: liblwgeom.h:308
double y
Definition: liblwgeom.h:308

Here is the call graph for this function:

Here is the caller graph for this function: