2

I have a polygon layer which contains some overlapping polygons.

enter image description here

I want Holes instead of overlapping polygons.

i tried some code

                  ESRI.ArcGIS.ArcMapUI.IMxDocument mxd = ArcMap.Application.Document 
         as ESRI.ArcGIS.ArcMapUI.IMxDocument;
            if (mxd.SelectedItem is ESRI.ArcGIS.Carto.IFeatureLayer)
            {
                ESRI.ArcGIS.Carto.IFeatureLayer flay = mxd.SelectedItem as ESRI.ArcGIS.Carto.IFeatureLayer;
                ESRI.ArcGIS.Geodatabase.IFeatureClass fcls = flay.FeatureClass;
                ESRI.ArcGIS.Geodatabase.ISpatialFilter spatialFilter = new ESRI.ArcGIS.Geodatabase.SpatialFilterClass();

                spatialFilter.SpatialRel = ESRI.ArcGIS.Geodatabase.esriSpatialRelEnum.esriSpatialRelOverlaps;
                ESRI.ArcGIS.Geodatabase.IFeatureCursor fcur = flay.Search(spatialFilter, true);
                ESRI.ArcGIS.Geodatabase.IFeature fea = fcur.NextFeature();
                while (fea!=null)
                {
                    ESRI.ArcGIS.Geometry.IGeometry geo= fea.Shape;
                    ESRI.ArcGIS.Geometry.IGeometryCollection gcoll = geo as ESRI.ArcGIS.Geometry.IGeometryCollection;

                    fea = fcur.NextFeature();
                }

How can I do this?

1

2 Answers 2

1

I can sort of see what you're trying to do (I hope).. you want to erase the smaller polygons out of the larger polygons. This code should help:

// waay up the top
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geodatabase;


// later on... seach each polygon and if it completely contains another polygon cut 
// the inside polygon out of the outside polygon to make a doughnut polygon
if (mxd.SelectedItem is ESRI.ArcGIS.Carto.IFeatureLayer)
{
    IFeatureLayer flay = mxd.SelectedItem as IFeatureLayer;
    IFeatureClass fcls = flay.FeatureClass;
    ISpatialFilter spatialFilter = new SpatialFilterClass();

    spatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelContains; // the polygon 'within'
    IFeatureCursor fcur = flay.Search(spatialFilter,false); // non-recycling cursor because you're changing the geometry
    IFeature fea = fcur.NextFeature();
    while (fea!=null)
    {
        IGeometry geo= fea.ShapeCopy; // don't use 'Shape' here
        spatialFilter.Geometry = geo;
        IFeatureCursor ConCur = fcls.Search(spatialFilter,false); // non-recyling cursor
        IFeature OtherFea = ConCur.NextFeature(); // get the first feature
        if (OtherFea != null)
        {
            ITopologicalOperator pTopOp = (ITopologicalOperator)geo;
            IGeometry DiffGeom = pTopOp.Difference(OtherFea.ShapeCopy()); // cut the other feature out of this feature
            fea.Shape = DiffGeom; // Now use 'Shape'
            fea.Store(); // update the feature

        }

        fea = fcur.NextFeature();
    }
}

In the section up the top of your code if you include the using statements you don't need to express objects from their full lineage which saves a bunch of typing and makes the code a little easier to read.

You need two cursors here, one to loop through all features, as you have done, and a second one for the contained polygons; in this case I'm only clipping the first returned feature out but you might like to add another loop here for instances where more than one polygon is contained.

In this instance it is important not to use a recycling cursor. Only use a recycling cursor if you're only concerned with attributes and not changing the rows.. Read about it in the IFeatureClass.Search docs.

I have used ITopologicalOperator to cut out (Difference) the smaller polygons completely contained within. Sometimes though the returned geometry is a little broken, you can fix this with Simplify, but I haven't added that code to keep this a little simple.

1

Select by location, select from M all polygons completely contained with a dataset L, L and M being two copies of the same dataset. That should give you only the overlapping contained polygons. (If not, buffer L by -1 iota, and repeat). Then, export the contained polygons, do a table join to M, delete all matches.That will get rid of all the inner polygons in M. Then use ERASE on the contained polgyons shapefile to cut 'holes' in the dataset.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.