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);
}
}