2

I have a third party tool that is creating a color filled area by placing many polygons side by side. I need to combine these polygons into 1 polygon. Unfortunately changing the 3rd party tool is not an option.

In the below image each color is made up of approximately 150 polygons, and I am trying to combine the polygons so I have 1 polygon for each color.

enter image description here

I have tried to use the ITopologicalOperator.ConstructUnion() but that does not seem to work.

private void MoveContourFillsToDrawingLayer()
{
    string polygonFeatureLayerBaseName = "Contour polygons";
    IGraphicsContainer graphicsContainer = GetGraphicsContainer();
    IActiveView activeView = (IActiveView)MapController.Map;

    IEnumLayer layerEnumerator = MapController.Map.Layers;
    if (layerEnumerator != null)
    {
        try
        {
            ILayer layer = layerEnumerator.Next();
            while (null != layer)
            {
                if (layer.Name == polygonFeatureLayerBaseName)
                {
                    var messOfFeaturePolygons = new List<Tuple<SimpleFillSymbol, IFeature, System.Drawing.Color>>();
                    IFeatureLayer featureLayer = layer as IFeatureLayer;
                    if (featureLayer != null)
                    {
                        if (featureLayer.Visible)
                        {
                            IFeatureCursor cursor = featureLayer.Search(null, false);
                            IFeature feature = cursor.NextFeature();
                            while (feature != null)
                            {
                                // Gather all the features together.
                                if (feature.Shape.GeometryType == esriGeometryType.esriGeometryPolygon)
                                {
                                    SimpleFillSymbol originalSymbol = (SimpleFillSymbol)GetFeatureSymbol(feature, featureLayer);
                                    originalSymbol.Style = esriSimpleFillStyle.esriSFSSolid;

                                    messOfFeaturePolygons.Add(
                                        new Tuple<SimpleFillSymbol, IFeature, System.Drawing.Color>(
                                            originalSymbol, feature,
                                            EsriConversionUtility.ConvertToSystemColor(originalSymbol.Color)));

                                    feature = cursor.NextFeature();
                                }
                            }
                        }
                    }

                    // Group the features together by color.
                    var polygonsByGroup = messOfFeaturePolygons.GroupBy(a => a.Item3);
                    foreach (var polygons in polygonsByGroup)
                    {
                        ITopologicalOperator resultPolygon = new Polygon() as ITopologicalOperator;
                        GeometryBag geometriesToUnion = new GeometryBag();
                        geometriesToUnion.SpatialReference = activeView.FocusMap.SpatialReference;                                

                        foreach (Tuple<SimpleFillSymbol, IFeature, System.Drawing.Color> polygon in polygons)
                        {
                            object missing = Type.Missing;
                            IGeometry geometry = polygon.Item2.ShapeCopy;
                            geometry.Project(activeView.FocusMap.SpatialReference);                                    
                            ((IGeometryCollection)geometriesToUnion).AddGeometry(geometry, ref missing, ref missing);
                        }

                        resultPolygon.ConstructUnion(geometriesToUnion as IEnumGeometry);


                        Polygon result = resultPolygon as Polygon;

                        var polygonElement = new PolygonElementClass();
                        //polygonElement.Symbol = polygons.First().Item1;
                        SimpleFillSymbolClass sym = new SimpleFillSymbolClass();
                        sym.Color = EsriConversionUtility.ConvertToRgbColor(Colors.Yellow);

                        ACSimplePolygonSymbolClass symbol = new ACSimplePolygonSymbolClass();
                        symbol.FillColor = EsriConversionUtility.ConvertToRgbColor(Colors.Yellow);

                        polygonElement.Symbol = sym;

                        IElement elementToAdd = polygonElement;
                        elementToAdd.Geometry = (IGeometry)result;
                        graphicsContainer.AddElement(elementToAdd, 0);
                    }
                }

                layer = layerEnumerator.Next();
            }
        }
        finally
        {
            Marshal.ReleaseComObject(layerEnumerator);
            RefreshView();
        }
    }
}
  • 4
    Would the Dissolve tool work? – briennakh Oct 15 '18 at 19:45
  • 1
    Do you need to call (geometriesToUnion as IEnumGeometry).Reset() before ConstructUnion but after all geometries have been added to the collection? – danielm Oct 15 '18 at 20:20
  • 2
    Are the polygons simple? Try to call the 'Simplify' method on the resultPolygon variable after the ConstructUnion. Also simplify the geometries before adding them to the geometry bag line this. ((ITopologicalOperator2)geometry).IsKnownSimple_2 = false; ITopologicalOperator2)geometry).Simplify(); Also, are the polygons correctly snapped? Maybe you have to snap them before making the union. – cag Oct 16 '18 at 5:52
  • @cag 7 It turns out that calling simplify fixed the issue. If you post it as an answer I will accept it. Thank you, everyone for the help here. – scott lafoy Oct 16 '18 at 13:42
  • Great! I'm glad you had your problem solved. – cag Oct 17 '18 at 5:29
2

Try to call the 'Simplify' method on the resultPolygon variable after the ConstructUnion. Also simplify the geometries before adding them to the geometry bag line this:

((ITopologicalOperator2)geometry).IsKnownSimple_2 = false;    
ITopologicalOperator2)geometry).Simplify();

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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