PostGIS  2.2.8dev-r@@SVN_REVISION@@

◆ LWGEOM_GEOS_makeValidMultiLine()

static GEOSGeometry* LWGEOM_GEOS_makeValidMultiLine ( const GEOSGeometry *  gin)
static

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

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

Referenced by LWGEOM_GEOS_makeValid().

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