0

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

John M
  • 53
  • 7

2 Answers2

1

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;
John M
  • 53
  • 7
0

Have you tried

IEnumGeometry enumGeom = (IEnumGeometry)((IGeometryBag)geometryCollection);

IGeometryBag implements IGeometryCollection, and IGeometryBag implements IEnumGeometry.

But GeometryCollection doesn't implement IEnumGeometry directly, so you'll have to cast it to GeometryBag first, and afterwards to IEnumGeometry.

Gwen Royakkers
  • 431
  • 2
  • 11
  • Thanks Gwen. Unfortunately it didn't work. It crashes with the error: 'Unable to cast COM object of type PolylineClass to interface type IGeometryBag'. – John M Dec 07 '17 at 22:13
  • Have you initialized your polyline object as IPolyLine? Like: IPolyLine pl = new PolyLineClass(); The class object of PolyLine doesn't implement IGeometryBag, but the interface does. – Gwen Royakkers Dec 11 '17 at 10:33