Search code examples
c#arcobjects

ArcGIS Convert IGeometryCollection to IEnumGeometry


I have a group of polylines mistakenly created with a geometry for each vertex pair. Typically they have thousands of geometries. I would like to merge the geometries (note: NOT merge features) in each feature into one geometry. To do this I am using ITopologicalOperator5.ConstructUnion. ConstructUnion takes an IEnumGeometry parameter and I have an IGeometryCollection of geometries. I can create an IEnumGeometry using a GeometryBag but ConstructUnion does not accept GeometryBags. If I use IEnumGeometry pEnum = (IEnumGeometry)pGeomCollection it throws an exception. If I use IEnumGeometry pEnum = pGeomCollection as IEnumGeometry then pEnum is null. In VB.NET Ctype(pGeomCollection, IEnumGeometry) works fine. Can anyone tell me how to convert a (C#) Geometry Collection into an IEnumGeometry?

Thanks,

John


Solution

  • The solution is to link the Geometry Bag and the Geometry Collection when they are initiated:

    IGeometryBag pGeomBag = new GeometryBagClass();
    pGeomBag.SpatialReference = .......
    
    IGeometryCollection pGeomColl = new PolylineClass();
    pGeomColl = (IGeometryCollection)pGeomBag;
    
    IEnumGeometry pEnum = new EnumFeatureGeometryClass();
    pEnum = (IEnumGeometry)pGeomBag;