How split multi-part polygons, and keep holes, without geoprocessing tools?

437
2
Jump to solution
01-12-2024 03:30 AM
Mikael-J
New Contributor II

Hi,

I'm trying to loop over the features in a feature class and split each mulit-part polygon into single-part polygons. I don't want to use any geoprocessing tools. The problem is that the code below gives me each polygon part, but without the holes. I want to split the multi-part polygons but keep any polygon holes. Any ideas?

IFeatureClass inFc = // Input feature class
IFeatureClass outFc = // Ouput feature class
 
IFeature ftr;
IFeatureCursor cur = inFc.Search(null, true);
 
while ((ftr = cur.NextFeature()) != null)
{
// Get spatial reference from orginal feature
ISpatialReference spRef = ftr.ShapeCopy.SpatialReference;
 
object obj = Type.Missing;
IGeometryBag geomBag = ((IPolygon4)ftr.ShapeCopy).ExteriorRingBag;
IGeometryCollection coll = geomBag as IGeometryCollection;
 
for (int i = 0; i < coll.GeometryCount; i++)
{
IFeature newFtr = outFc.CreateFeature();
IGeometryCollection geomColl = new PolygonClass();
IGeometry geom = coll.get_Geometry(i);
geom.SpatialReference = spRef;
geomColl.AddGeometry(geom, ref obj, ref obj);
geomColl.GeometriesChanged();
 
IGeometry geom2 = (IPolygon)geomColl;
newFtr.Shape = geom2;
newFtr.Store();
}
}
 

Best regards,

Mikael

0 Kudos
1 Solution

Accepted Solutions
BrentHoskisson
Occasional Contributor III

It is mostly there, you just need to do geomColl.AddGeometry on all of the interior rings of your original polygon.  I am fairly certain that any interior rings not in your exterior ring will be ignored.  However, if not, you might have to run an intersect first to see.

Good Luck

Brent 

View solution in original post

2 Replies
BrentHoskisson
Occasional Contributor III

It is mostly there, you just need to do geomColl.AddGeometry on all of the interior rings of your original polygon.  I am fairly certain that any interior rings not in your exterior ring will be ignored.  However, if not, you might have to run an intersect first to see.

Good Luck

Brent 

Mikael-J
New Contributor II

Hi Brent!

You are so right. It's worked like a charm to add the interior rings as well.

Many thanks!

/Mikael

0 Kudos