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.
The spatial reference of the geometry bag is defined before adding geometries to it.
[C#]
private IPolygon GeometryBag_Example(IFeatureClass featureClass)
{
//Check input objects.if (featureClass == null)
{
returnnull;
}
IGeoDataset geoDataset = featureClass as IGeoDataset;
//You can use a spatial filter to create a subset of features to union together, //in order to do that uncomment the next line, 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 into the bag.//You don't specify the before or after geometry (missing),//so 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]
PrivateFunction GeometryBag_Example(ByVal featureClass As IFeatureClass) As IPolygon
'Check input objects.If featureClass IsNothingThenReturnNothingEndIfDim geoDataset As IGeoDataset = CType(featureClass, IGeoDataset)
'You can use a spatial filter to create a subset of features to union together, 'in order 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()
WhileNot currentFeature IsNothing'Add a reference to this feature's geometry into the bag.'You don't specify the before or after geometry (missing),'so the currentFeature.Shape IGeometry is added to the end of the geometryCollection.Dim missing AsObject = Type.Missing
geometryCollection.AddGeometry(currentFeature.Shape, missing, missing)
currentFeature = featureCursor.NextFeature()
EndWhile'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
EndFunction