Using ArcGIS 10.4.1 ArcObjects with geoprocessing. I have an input feature class with 100s of polygons and also one raster. I iterate through each polygon and clip the raster and then do additional work. It has performed well up to today. It seems some our polygons are outside of the raster area.
So my code in VB.NET looks like this:
'create an IVariantArray to hold the parameter values. Dim parameters As IVariantArray = New VarArray 'populate the variant array with parameter values. parameters.Add(inSurfaceFullName) 'input surface parameters.Add("") 'rectangle to be used as the clipper parameters.Add(outClippedRasterFullName) 'the output raster (ex Raster_3) parameters.Add(inClippingPolyFullName) 'the input template dataset to be used as a clipper. in this case our sub feature class (NOT the input feature class) parameters.Add("") 'all the pixels with the specified value will be set to NoData in the output raster dataset. parameters.Add("ClippingGeometry") 'the raster dataset is clipped based on the perimeter of the polygon shape. 'execute the model tool by name. Dim res As IGeoProcessorResult = GP.Execute("Clip_management", parameters, Nothing)
When it gets to the polygon that does not intersect the raster I get this error:
System.Runtime.InteropServices.COMException (0x80004005): Error HRESULT E_FAIL has been returned from a call to a COM component. at ESRI.ArcGIS.Geoprocessing.GeoProcessorClass.Execute(String Name, IVariantArray ipValues, ITrackCancel pTrackCancel)
I have been looking in the ESRI SDK for a pre-process where I can check if the polygon intersects the raster with a True/False as the result. Is that something that exists? Or another way to check the intersection of a geometry with a raster?
The only thing I have tried is to check if the extents intersect but I can see where this may fail. For example a circle geometry just at the corner of the raster. the envelopes will intersect but the geometry and raster will not.
With help from @MichaelStimson, I ended up testing the raster envelope against the polygon geometry. Seems to work rather well. Here is the function I use to test all features in a feature class against the raster:
''' <summary> ''' this function will determine if the polygon feature class is outside of the raster boundary. it will test all features in the ''' feature class and return one result for the overall disjoint result. ''' <param name="pInSurfaceEnv">the input surface envelope</param> ''' <param name="pPolyDsName">the input polygon Dataset name object</param> ''' </summary> ''' <returns></returns> ''' <remarks></remarks> Private Function IsFeatureClassDisjointWithRaster(pInSurfaceEnv As IEnvelope, pPolyDsName As IDatasetName) As Boolean Try Dim pRelOp As IRelationalOperator2 = CType(pInSurfaceEnv, IRelationalOperator2) Dim pFeatClass As IFeatureClass = OpenFileFeatClassDataset(pPolyDsName.WorkspaceName.PathName, pPolyDsName.Name) Dim pFeatCursor As IFeatureCursor = pFeatClass.Search(Nothing, True) Dim IsFeatClassDisjoint As Boolean = True 'we assume the entire feature class is disjoint Dim pFeature As IFeature = pFeatCursor.NextFeature Do While Not pFeature Is Nothing Dim IsFeatureDisjoint As Boolean = pRelOp.Disjoint(pFeature.ShapeCopy) 'we only need one feature to be non disjoint to make the entire feature class non disjoint If Not IsFeatureDisjoint Then IsFeatClassDisjoint = False 'next feature pFeature = pFeatCursor.NextFeature Loop Return IsFeatClassDisjoint Catch ex As Exception Throw End Try End Function