PostGIS  2.3.8dev-r@@SVN_REVISION@@

◆ LWGEOM_GEOS_makeValidMultiLine()

static GEOSGeometry* LWGEOM_GEOS_makeValidMultiLine ( const GEOSGeometry *  gin)
static

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

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

Referenced by LWGEOM_GEOS_makeValid().

679 {
680  GEOSGeometry** lines;
681  GEOSGeometry** points;
682  GEOSGeometry* mline_out=0;
683  GEOSGeometry* mpoint_out=0;
684  GEOSGeometry* gout=0;
685  uint32_t nlines=0, nlines_alloc;
686  uint32_t npoints=0;
687  uint32_t ngeoms=0, nsubgeoms;
688  uint32_t i, j;
689 
690  ngeoms = GEOSGetNumGeometries(gin);
691 
692  nlines_alloc = ngeoms;
693  lines = lwalloc(sizeof(GEOSGeometry*)*nlines_alloc);
694  points = lwalloc(sizeof(GEOSGeometry*)*ngeoms);
695 
696  for (i=0; i<ngeoms; ++i)
697  {
698  const GEOSGeometry* g = GEOSGetGeometryN(gin, i);
699  GEOSGeometry* vg;
701  if ( GEOSisEmpty(vg) )
702  {
703  /* we don't care about this one */
704  GEOSGeom_destroy(vg);
705  }
706  if ( GEOSGeomTypeId(vg) == GEOS_POINT )
707  {
708  points[npoints++] = vg;
709  }
710  else if ( GEOSGeomTypeId(vg) == GEOS_LINESTRING )
711  {
712  lines[nlines++] = vg;
713  }
714  else if ( GEOSGeomTypeId(vg) == GEOS_MULTILINESTRING )
715  {
716  nsubgeoms=GEOSGetNumGeometries(vg);
717  nlines_alloc += nsubgeoms;
718  lines = lwrealloc(lines, sizeof(GEOSGeometry*)*nlines_alloc);
719  for (j=0; j<nsubgeoms; ++j)
720  {
721  const GEOSGeometry* gc = GEOSGetGeometryN(vg, j);
722  /* NOTE: ownership of the cloned geoms will be
723  * taken by final collection */
724  lines[nlines++] = GEOSGeom_clone(gc);
725  }
726  }
727  else
728  {
729  /* NOTE: return from GEOSGeomType will leak
730  * but we really don't expect this to happen */
731  lwerror("unexpected geom type returned "
732  "by LWGEOM_GEOS_makeValid: %s",
733  GEOSGeomType(vg));
734  }
735  }
736 
737  if ( npoints )
738  {
739  if ( npoints > 1 )
740  {
741  mpoint_out = GEOSGeom_createCollection(GEOS_MULTIPOINT,
742  points, npoints);
743  }
744  else
745  {
746  mpoint_out = points[0];
747  }
748  }
749 
750  if ( nlines )
751  {
752  if ( nlines > 1 )
753  {
754  mline_out = GEOSGeom_createCollection(
755  GEOS_MULTILINESTRING, lines, nlines);
756  }
757  else
758  {
759  mline_out = lines[0];
760  }
761  }
762 
763  lwfree(lines);
764 
765  if ( mline_out && mpoint_out )
766  {
767  points[0] = mline_out;
768  points[1] = mpoint_out;
769  gout = GEOSGeom_createCollection(GEOS_GEOMETRYCOLLECTION,
770  points, 2);
771  }
772  else if ( mline_out )
773  {
774  gout = mline_out;
775  }
776  else if ( mpoint_out )
777  {
778  gout = mpoint_out;
779  }
780 
781  lwfree(points);
782 
783  return gout;
784 }
void lwfree(void *mem)
Definition: lwutil.c:242
static GEOSGeometry * LWGEOM_GEOS_makeValidLine(const GEOSGeometry *gin)
void * lwrealloc(void *mem, size_t size)
Definition: lwutil.c:235
void * lwalloc(size_t size)
Definition: lwutil.c:227
void lwerror(const char *fmt,...)
Write a notice out to the error handler.
Definition: lwutil.c:102
Here is the call graph for this function:
Here is the caller graph for this function: