HRESULT E_FAIL from ITopologicalOperator.ConstructUnion()

3289
3
07-31-2015 11:21 AM
marwamohamed
New Contributor

I am trying to union polygons but i get this error "Error HRESULT E_FAIL has been returned from a call to a COM component."

at this line :unionedPolygon.ConstructUnion(geometryBag as ESRI.ArcGIS.Geometry.IEnumGeometry);

Here is the code :

public static ESRI.ArcGIS.Geometry.IPolygon MergePolygonsWithCondition(ESRI.ArcGIS.Geodatabase.IFeatureClass featureClass,

String whereClause)

        {

            //Check input objects.

            if (featureClass == null)

            {

                return null;

            }

            try

            {

                ESRI.ArcGIS.Geodatabase.IGeoDataset geoDataset = featureClass as ESRI.ArcGIS.Geodatabase.IGeoDataset;

                ESRI.ArcGIS.Geodatabase.IQueryFilter queryFilter = new ESRI.ArcGIS.Geodatabase.QueryFilterClass();

                queryFilter.WhereClause = whereClause;

                ESRI.ArcGIS.Geometry.IGeometry geometryBag = new ESRI.ArcGIS.Geometry.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.

                ESRI.ArcGIS.Geodatabase.IFeatureCursor featureCursor = featureClass.Search(queryFilter, false);

                ESRI.ArcGIS.Geometry.IGeometryCollection geometryCollection = geometryBag as

ESRI.ArcGIS.Geometry.IGeometryCollection;

                ESRI.ArcGIS.Geodatabase.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.

                ESRI.ArcGIS.Geometry.ITopologicalOperator unionedPolygon = new ESRI.ArcGIS.Geometry.PolygonClass();

                unionedPolygon.ConstructUnion(geometryBag as ESRI.ArcGIS.Geometry.IEnumGeometry);

                return unionedPolygon as ESRI.ArcGIS.Geometry.IPolygon;

            }

            catch (Exception ex)

            {

                Logger.LoggerInstance.LogError(ex.Message, DateTime.Now, "MergePolygonsWithCondition",

"SpatialOperations", "");

                throw new Exception(ex.Message);

            }

        }

0 Kudos
3 Replies
DuncanHornby
MVP Notable Contributor

Marwa,

Could you please edit your question and apply the language syntax to make reading your code easier for others? Go to the Advance Editor option then >> to go to the syntax highlighter.

Your code looks identical to the sample code so this would suggest to me that the data is at fault? You do not describe your data, is it some monster multi-part dataset, that could be a cause? I suggest you try union the data with the standard Union tool to see if that works?

0 Kudos
AxelThomas
New Contributor III

Hi there,

I obviously have the same problem with code that worked in ArcGIS 9.3 but fails in 10.1.

The error code looks as follows, the last line roughly translates as : 'interface not supported'

ITopoOp5_error.jpg

There are a number of related threads

(e.g. ITopological Operator, Intermittent COMException on Union ) but following their recommandations (namely to simplify the geometries) did not help as the error already occurs hwn assigning the feature to the TopologyOperator.

The code where the error occurs looks like this:

If pF.Shape.GeometryType = esriGeometryType.esriGeometryPolygon Then
pNewPolygon = pF.ShapeCopy
pTopoOp = CType(pNewPolygon, ESRI.ArcGIS.Geometry.ITopologicalOperator5)
pTopoOp.IsKnownSimple_2 = False
pTopoOp.Simplify()
pGeoCollection.AddGeometry(pTopoOp)
End If

Right now there are only two polygons with 4 points each involved so size should not be an issue.

Any ideas on how to solve this problem are greatly appreciated!

Regards,

Axel

0 Kudos
AxelThomas
New Contributor III

I solved the problem by using ITopologicalOperator2 instead of  ITopologicalOperator5 (ArcGIS 10.1, .Net 4.0). No idea why ESRI has to make it so difficult converting code from VBA to .Net...

0 Kudos