PostGIS  2.5.2dev-r@@SVN_REVISION@@

◆ LWGEOM_GEOS_makeValidMultiLine()

static GEOSGeometry* LWGEOM_GEOS_makeValidMultiLine ( const GEOSGeometry *  gin)
static

Definition at line 553 of file liblwgeom/lwgeom_geos_clean.c.

References lwalloc(), lwerror(), lwfree(), LWGEOM_GEOS_makeValidLine(), and lwrealloc().

Referenced by LWGEOM_GEOS_makeValid().

554 {
555  GEOSGeometry** lines;
556  GEOSGeometry** points;
557  GEOSGeometry* mline_out = 0;
558  GEOSGeometry* mpoint_out = 0;
559  GEOSGeometry* gout = 0;
560  uint32_t nlines = 0, nlines_alloc;
561  uint32_t npoints = 0;
562  uint32_t ngeoms = 0, nsubgeoms;
563  uint32_t i, j;
564 
565  ngeoms = GEOSGetNumGeometries(gin);
566 
567  nlines_alloc = ngeoms;
568  lines = lwalloc(sizeof(GEOSGeometry*) * nlines_alloc);
569  points = lwalloc(sizeof(GEOSGeometry*) * ngeoms);
570 
571  for (i = 0; i < ngeoms; ++i)
572  {
573  const GEOSGeometry* g = GEOSGetGeometryN(gin, i);
574  GEOSGeometry* vg;
576  if (GEOSisEmpty(vg))
577  {
578  /* we don't care about this one */
579  GEOSGeom_destroy(vg);
580  }
581  if (GEOSGeomTypeId(vg) == GEOS_POINT)
582  points[npoints++] = vg;
583  else if (GEOSGeomTypeId(vg) == GEOS_LINESTRING)
584  lines[nlines++] = vg;
585  else if (GEOSGeomTypeId(vg) == GEOS_MULTILINESTRING)
586  {
587  nsubgeoms = GEOSGetNumGeometries(vg);
588  nlines_alloc += nsubgeoms;
589  lines = lwrealloc(lines, sizeof(GEOSGeometry*) * nlines_alloc);
590  for (j = 0; j < nsubgeoms; ++j)
591  {
592  const GEOSGeometry* gc = GEOSGetGeometryN(vg, j);
593  /* NOTE: ownership of the cloned geoms will be
594  * taken by final collection */
595  lines[nlines++] = GEOSGeom_clone(gc);
596  }
597  }
598  else
599  {
600  /* NOTE: return from GEOSGeomType will leak
601  * but we really don't expect this to happen */
602  lwerror("unexpected geom type returned by LWGEOM_GEOS_makeValid: %s", GEOSGeomType(vg));
603  }
604  }
605 
606  if (npoints)
607  {
608  if (npoints > 1)
609  mpoint_out = GEOSGeom_createCollection(GEOS_MULTIPOINT, points, npoints);
610  else
611  mpoint_out = points[0];
612  }
613 
614  if (nlines)
615  {
616  if (nlines > 1)
617  mline_out = GEOSGeom_createCollection(GEOS_MULTILINESTRING, lines, nlines);
618  else
619  mline_out = lines[0];
620  }
621 
622  lwfree(lines);
623 
624  if (mline_out && mpoint_out)
625  {
626  points[0] = mline_out;
627  points[1] = mpoint_out;
628  gout = GEOSGeom_createCollection(GEOS_GEOMETRYCOLLECTION, points, 2);
629  }
630  else if (mline_out)
631  gout = mline_out;
632 
633  else if (mpoint_out)
634  gout = mpoint_out;
635 
636  lwfree(points);
637 
638  return gout;
639 }
void lwfree(void *mem)
Definition: lwutil.c:244
unsigned int uint32_t
Definition: uthash.h:78
static GEOSGeometry * LWGEOM_GEOS_makeValidLine(const GEOSGeometry *gin)
void * lwrealloc(void *mem, size_t size)
Definition: lwutil.c:237
void * lwalloc(size_t size)
Definition: lwutil.c:229
void lwerror(const char *fmt,...)
Write a notice out to the error handler.
Definition: lwutil.c:190
Here is the call graph for this function:
Here is the caller graph for this function: