PostGIS  2.5.7dev-r@@SVN_REVISION@@

◆ parse_kml_coordinates()

static POINTARRAY* parse_kml_coordinates ( xmlNodePtr  xnode,
bool *  hasz 
)
static

Parse kml:coordinates.

Definition at line 280 of file lwgeom_in_kml.c.

281 {
282  xmlChar *kml_coord;
283  bool found;
284  POINTARRAY *dpa;
285  int seen_kml_dims = 0;
286  int kml_dims;
287  char *p, *q;
288  POINT4D pt;
289  double d;
290 
291  if (xnode == NULL) lwpgerror("invalid KML representation");
292 
293  for (found = false ; xnode != NULL ; xnode = xnode->next)
294  {
295  if (xnode->type != XML_ELEMENT_NODE) continue;
296  if (!is_kml_namespace(xnode, false)) continue;
297  if (strcmp((char *) xnode->name, "coordinates")) continue;
298 
299  found = true;
300  break;
301  }
302  if (!found) lwpgerror("invalid KML representation");
303 
304  /* We begin to retrieve coordinates string */
305  kml_coord = xmlNodeGetContent(xnode);
306  p = (char *) kml_coord;
307 
308  /* KML coordinates pattern: x1,y1 x2,y2
309  * x1,y1,z1 x2,y2,z2
310  */
311 
312  /* Now we create PointArray from coordinates values */
313  /* HasZ, !HasM, 1pt */
314  dpa = ptarray_construct_empty(1, 0, 1);
315 
316  while (*p && isspace(*p)) ++p;
317  for (kml_dims=0; *p ; p++)
318  {
319 //lwpgnotice("*p:%c, kml_dims:%d", *p, kml_dims);
320  if ( isdigit(*p) || *p == '+' || *p == '-' || *p == '.' ) {
321  kml_dims++;
322  errno = 0; d = strtod(p, &q);
323  if ( errno != 0 ) {
324  // TODO: destroy dpa, return NULL
325  lwpgerror("invalid KML representation"); /*: %s", strerror(errno));*/
326  }
327  if (kml_dims == 1) pt.x = d;
328  else if (kml_dims == 2) pt.y = d;
329  else if (kml_dims == 3) pt.z = d;
330  else {
331  lwpgerror("invalid KML representation"); /* (more than 3 dimensions)"); */
332  // TODO: destroy dpa, return NULL
333  }
334 
335 //lwpgnotice("after strtod d:%f, *q:%c, kml_dims:%d", d, *q, kml_dims);
336 
337  if ( *q && ! isspace(*q) && *q != ',' ) {
338  lwpgerror("invalid KML representation"); /* (invalid character %c follows ordinate value)", *q); */
339  }
340 
341  /* Look-ahead to see if we're done reading */
342  while (*q && isspace(*q)) ++q;
343  if ( isdigit(*q) || *q == '+' || *q == '-' || *q == '.' || ! *q ) {
344  if ( kml_dims < 2 ) lwpgerror("invalid KML representation"); /* (not enough ordinates)"); */
345  else if ( kml_dims < 3 ) *hasz = false;
346  if ( ! seen_kml_dims ) seen_kml_dims = kml_dims;
347  else if ( seen_kml_dims != kml_dims ) {
348  lwpgerror("invalid KML representation: mixed coordinates dimension");
349  }
350  ptarray_append_point(dpa, &pt, LW_TRUE);
351  kml_dims = 0;
352  }
353  p = q-1; /* will be incremented on next iteration */
354 //lwpgnotice("after look-ahead *p:%c, kml_dims:%d", *p, kml_dims);
355  } else if ( *p != ',' && ! isspace(*p) ) {
356  lwpgerror("invalid KML representation"); /* (unexpected character %c)", *p); */
357  }
358  }
359 
360  xmlFree(kml_coord);
361 
362  /* TODO: we shouldn't need to clone here */
363  return ptarray_clone_deep(dpa);
364 }
POINTARRAY * ptarray_clone_deep(const POINTARRAY *ptarray)
Deep clone a pointarray (also clones serialized pointlist)
Definition: ptarray.c:628
POINTARRAY * ptarray_construct_empty(char hasz, char hasm, uint32_t maxpoints)
Create a new POINTARRAY with no points.
Definition: ptarray.c:70
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,...
Definition: ptarray.c:156
#define LW_TRUE
Return types for functions with status returns.
Definition: liblwgeom.h:76
static bool is_kml_namespace(xmlNodePtr xnode, bool is_strict)
Return false if current element namespace is not a KML one Return true otherwise.
double x
Definition: liblwgeom.h:355
double z
Definition: liblwgeom.h:355
double y
Definition: liblwgeom.h:355

References is_kml_namespace(), LW_TRUE, 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().

Here is the call graph for this function:
Here is the caller graph for this function: