0

I'm trying to execute the raster calculator function in VB.NET but I'm having trouble with the gp.execute line. Sorry if the code is confusing, I'm fairly new to VB.net. I keep getting this error:

An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in ESRI.ArcGIS.Geoprocessor.dll
Additional information: Error HRESULT E_FAIL has been returned from a call to a COM component.

Here's my code. I thought it might have been the expression, but the error occurs at the execute command.

Private Sub ExecuteRasterCalc()
    Dim raster1 As String = "T:\TeamProject\Group2\Ca Slope and Aspect\CA_ASP_RISK1.tif"
    Dim raster2 As String = "T:\TeamProject\Group2\Ca Slope and Aspect\CA_SLP_RISK1.tif"
    Dim raster3 As String = "T:\TeamProject\Group2\Ca Landcover Fire Intensity\CaFIRE_INTEN11.tif"
    Dim raster4 As String = "T:\TeamProject\Group2\Ca Biomass Load\FUEL_LOAD1.tif"
    Dim expression As String = "'" & raster1 & "'+'" & raster2 & "'+'" & raster3 & "'+'" & raster4 & "'"
    Dim outputpath As String = "T:\TeamProject\Group2\Test.gdb\rastercalc"
    Dim gp = New ESRI.ArcGIS.Geoprocessor.Geoprocessor()

    gp.OverwriteOutput = True
    gp.AddOutputsToMap = True

    Dim parameters As IVariantArray = New VarArray
    parameters.Add(expression)
    parameters.Add(outputpath)

    Dim result As IGeoProcessorResult = gp.Execute("RasterCalculator", parameters, Nothing)
End Sub
0

2 Answers 2

2

One of the benefits that VB.Net has over C# is its ability to use GeoProcessing Tools as objects with properties, not needing a VarArray to set the parameters. This is how I used them in my projects, with a function for each tool that I used. For example, this is the Add Join tool function:

Friend Function AddJoin(ByVal pInObject As Object, ByVal InFieldName As String, ByVal pJoinTable As ESRI.ArcGIS.Geodatabase.ITable, ByVal JoinFieldName As String) As Boolean

    Dim Join As New ESRI.ArcGIS.DataManagementTools.AddJoin
    Dim Result As ESRI.ArcGIS.Geoprocessing.IGeoProcessorResult2

    Try
        Using releaser As New ESRI.ArcGIS.ADF.ComReleaser
            releaser.ManageLifetime(Join)

            Join.in_layer_or_view = pInObject
            Join.in_field = InFieldName
            Join.join_table = pJoinTable
            Join.join_field = JoinFieldName

            Result = RunTool(Join, Nothing)
            If Result Is Nothing Then
                Windows.Forms.MessageBox.Show("Could not join the two items", "Join failed", Windows.Forms.MessageBoxButtons.OK, Windows.Forms.MessageBoxIcon.Exclamation)
                Return False
            End If

            Return True
        End Using

    Catch ex As Exception
        System.Windows.Forms.MessageBox.Show(ex.ToString & vbNewLine & ex.StackTrace.ToString, "Join datasets")
        Return False
    End Try

End Function

This is the function does the processing

Friend Function RunTool(ByVal Process As ESRI.ArcGIS.Geoprocessor.IGPProcess, ByVal TC As ESRI.ArcGIS.esriSystem.ITrackCancel, Optional ByVal AddOutput As Boolean = False) As ESRI.ArcGIS.Geoprocessing.IGeoProcessorResult

    Dim Result As ESRI.ArcGIS.Geoprocessing.IGeoProcessorResult

    Try
        System.Windows.Forms.Cursor.Current = Windows.Forms.Cursors.WaitCursor
        Dim GP As New ESRI.ArcGIS.Geoprocessor.Geoprocessor
        GP.OverwriteOutput = True
        GP.AddOutputsToMap = AddOutput

        Result = CType(GP.Execute(Process, Nothing), ESRI.ArcGIS.Geoprocessing.IGeoProcessorResult)
        If Result.Status <> ESRI.ArcGIS.esriSystem.esriJobStatus.esriJobSucceeded Then
            Return Nothing
        End If
        GP.ClearMessages()
        Return Result
    Catch ex As Exception
        Return Nothing
    End Try

End Function
0

I've not checked if your parameters are correct but the tool name is definitely incorrect, you need to provide it with the toolbox alias

So this:

gp.Execute("RasterCalculator", parameters, Nothing)

Should be this:

gp.Execute("RasterCalculator_sa", parameters, Nothing)

As a side note I get the GeoProcessor object through its interface, so here is how I would call the Select tool in the Analysis toolbox for comparison:

    Dim pGP As IGeoProcessor
    pGP = New GeoProcessor
    With pGP
        .AddOutputsToMap = False
        .LogHistory = False
        .OverwriteOutput = True
    End With
    
    Dim pVariantArray As IVariantArray
    pVariantArray = New VarArray

    ' Load parameters
    With pVariantArray
        .Add(m_pFeatureLayer.FeatureClass)
        .Add(outFC)
        .Add(sSQL)
    End With

    ' Execute tool
    Dim pGeoProcessorResult As IGeoProcessorResult
    pGeoProcessorResult = pGP.Execute("Select_analysis", pVariantArray, Nothing)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.