This document is archived and information here might be outdated.  Recommended version.


How to create a union of several polygons (ArcObjects .NET 10.8 SDK)
ArcObjects Help for .NET developers > ArcObjects Help for .NET developers > Developing with ArcGIS > Learning ArcObjects > Managing data > Working with geometry > Creating geometries > How to create a union of several polygons

How to create a union of several polygons


Creating a union of several polygons

The following code example constructs a polygon representing the topological union of several polygons. The source polygons come from a feature class. References to the polygons are inserted into a geometry bag. The geometry bag is then used as the input parameter to the ConstructUnion method.
Define the spatial reference of the geometry bag before adding geometries to it.
[C#]
private IPolygon GeometryBag_Example(IFeatureClass featureClass)
{

    //Check input objects.
    if (featureClass == null)
    {
        return null;
    }

    IGeoDataset geoDataset=featureClass as IGeoDataset;

    //You can use a spatial filter to create a subset of features to union together. 
    //To do that, uncomment the next line, and set the properties of the spatial filter here.
    //Also, change the first parameter in the IFeatureCursor.Seach method.
    //ISpatialFilter queryFilter=new SpatialFilterClass();

    IGeometry geometryBag=new GeometryBagClass();

    //Define the spatial reference of the bag before adding geometries to it.
    geometryBag.SpatialReference=geoDataset.SpatialReference;

    //Use a nonrecycling cursor so each returned geometry is a separate object. 
    IFeatureCursor featureCursor=featureClass.Search(null, false);

    IGeometryCollection geometryCollection=geometryBag as IGeometryCollection;
    IFeature currentFeature=featureCursor.NextFeature();

    while (currentFeature != null)
    {
        //Add a reference to this feature's geometry to the bag.
        //Since you don't specify the before or after geometry (missing),
        //the currentFeature.Shape IGeometry is added to the end of the geometryCollection.
        object missing=Type.Missing;
        geometryCollection.AddGeometry(currentFeature.Shape, ref missing, ref
            missing);

        currentFeature=featureCursor.NextFeature();
    }

    // Create the polygon that will be the union of the features returned from the search cursor.
    // The spatial reference of this feature does not need to be set ahead of time. The 
    // ConstructUnion method defines the constructed polygon's spatial reference to be the 
    // same as the input geometry bag.
    ITopologicalOperator unionedPolygon=new PolygonClass();
    unionedPolygon.ConstructUnion(geometryBag as IEnumGeometry);

    return unionedPolygon as IPolygon;
}
[VB.NET]
Private Function GeometryBag_Example(ByVal featureClass As IFeatureClass) As IPolygon
    'Check input objects.
    If featureClass Is Nothing Then
        Return Nothing
    End If
    
    Dim geoDataset As IGeoDataset=CType(featureClass, IGeoDataset)
    
    'You can use a spatial filter to create a subset of features to union together.
    'To do that, uncomment the next line, and set the properties of the spatial filter here.
    'Dim queryFilter As ISpatialFilter=New SpatialFilterClass()
    
    Dim geometryBag As IGeometry=New GeometryBagClass()
    
    'Define the spatial reference of the bag before adding geometries to it.
    geometryBag.SpatialReference=geoDataset.SpatialReference
    
    'Use a nonrecycling cursor so each returned geometry is a separate object.
    Dim featureCursor As IFeatureCursor=featureClass.Search(Nothing, False)
    Dim geometryCollection As IGeometryCollection=CType(geometryBag, IGeometryCollection)
    Dim currentFeature As IFeature=featureCursor.NextFeature()
    
    While Not currentFeature Is Nothing
        'Add a reference to this feature's geometry to the bag.
        'Since you don't specify the before or after geometry (missing),
        'the currentFeature.Shape IGeometry is added to the end of the geometryCollection.
        Dim missing As Object=Type.Missing
        geometryCollection.AddGeometry(currentFeature.Shape, missing, missing)
        currentFeature=featureCursor.NextFeature()
    End While
    
    'Create the polygon that will be the union of the features returned from the search cursor.
    'The spatial reference of this feature does not need to be set ahead of time. The
    'ConstructUnion method defines the constructed polygon's spatial reference to be the
    'same as the input geometry bag.
    Dim unionedPolygon As ITopologicalOperator=New PolygonClass()
    unionedPolygon.ConstructUnion(geometryBag)
    Dim outPoly As IPolygon=CType(unionedPolygon, IPolygon)
    
    Return outPoly
End Function






To use the code in this topic, reference the following assemblies in your Visual Studio project. In the code files, you will need using (C#) or Imports (VB .NET) directives for the corresponding namespaces (given in parenthesis below if different from the assembly name):
Development licensing Deployment licensing
ArcGIS Desktop Basic ArcGIS Desktop Basic
ArcGIS Desktop Standard ArcGIS Desktop Standard
ArcGIS Desktop Advanced ArcGIS Desktop Advanced
Engine Developer Kit Engine