execute raster calculator geoprocessing tool from vb.net

4693
6
Jump to solution
09-19-2013 10:51 PM
sameerpuppal
Occasional Contributor
Hi,

Need help with executing geoprocessing tool. I want to execute Raster Calculator from Map Algebra in VB.Net.

Regards,
1 Solution

Accepted Solutions
sameerpuppal
Occasional Contributor
Do you need something specific?

There is pretty good explanation of how to run a Geoprocessor tool here:
http://help.arcgis.com/en/sdk/10.0/arcobjects_net/conceptualhelp/index.html#/Using_geoprocessing/000...

And the Raster Calculator:
http://help.arcgis.com/en/sdk/10.0/arcobjects_net/componenthelp/index.html#/Members/0047000027820000...


Finally did it! posting so that this might be helpful for someone like me.

Public Sub ExecuteRasterCalc()
        Dim expression As String = "'D:\Clip2.img'*'D:\Clip3.img'"
        Dim outputpath As String = "C:\Default.gdb\rastercalc7"
        Dim geoprocessor = New ESRI.ArcGIS.Geoprocessor.Geoprocessor()
        Dim ToolRasterCalc As New ESRI.ArcGIS.SpatialAnalystTools.RasterCalculator(expression, outputpath)
        geoprocessor.OverwriteOutput = True
        geoprocessor.AddOutputsToMap = True
        Dim result As IGeoProcessorResult = geoprocessor.Execute(ToolRasterCalc, Nothing)
    End Sub

Regards,
Sameer Puppal

View solution in original post

Reply
0 Kudos
6 Replies
JohnStephens
Occasional Contributor
Reply
0 Kudos
sameerpuppal
Occasional Contributor
Do you need something specific?

There is pretty good explanation of how to run a Geoprocessor tool here:
http://help.arcgis.com/en/sdk/10.0/arcobjects_net/conceptualhelp/index.html#/Using_geoprocessing/000...

And the Raster Calculator:
http://help.arcgis.com/en/sdk/10.0/arcobjects_net/componenthelp/index.html#/Members/0047000027820000...


Hi,


yes specifically i want to (raster1 + raster2) in the expression or such similar operations. I dont want to use RasterMapAlgebraOp and RasterModel instead want to use GP. I am not able to figure out how to execute gp - rastercaclulator  from Arcobjects . I have seen the first link you shared before but still confused how to use this snippet to execute.

I would really appreciate if you help me out with this.

Regards,
Sameer
sameerpuppal
Occasional Contributor
[HTML]http://help.arcgis.com/en/sdk/10.0/arcobjects_net/componenthelp/index.html#/Members/0047000027820000...

Can anyone help me to use this raster calculator in vb.net. specially how to set the expression.
Reply
0 Kudos
sameerpuppal
Occasional Contributor
Do you need something specific?

There is pretty good explanation of how to run a Geoprocessor tool here:
http://help.arcgis.com/en/sdk/10.0/arcobjects_net/conceptualhelp/index.html#/Using_geoprocessing/000...

And the Raster Calculator:
http://help.arcgis.com/en/sdk/10.0/arcobjects_net/componenthelp/index.html#/Members/0047000027820000...


Finally did it! posting so that this might be helpful for someone like me.

Public Sub ExecuteRasterCalc()
        Dim expression As String = "'D:\Clip2.img'*'D:\Clip3.img'"
        Dim outputpath As String = "C:\Default.gdb\rastercalc7"
        Dim geoprocessor = New ESRI.ArcGIS.Geoprocessor.Geoprocessor()
        Dim ToolRasterCalc As New ESRI.ArcGIS.SpatialAnalystTools.RasterCalculator(expression, outputpath)
        geoprocessor.OverwriteOutput = True
        geoprocessor.AddOutputsToMap = True
        Dim result As IGeoProcessorResult = geoprocessor.Execute(ToolRasterCalc, Nothing)
    End Sub

Regards,
Sameer Puppal

View solution in original post

Reply
0 Kudos
BramVan_Helleputte1
New Contributor II

Hi,

I've been struggeling with this as well the last few days, this is the only specific proposition around, but is doesn't work for me.

First of all my geoprocessor.Execute() takes 3 arguments instead of 2, I dont know why because I've used it with 2 args in the past...

For the ToolRasterCalc arg, the Execute doesn't accept this type RasterCalculator, it wants name as string

Maybe I am using the wrong Geoprocessor, I thought there were 2, a 'GeoProcessor' and a 'Geoprocessor', but that's in c# and vb is case insensitive..

I used your code like this:

    Public Sub RasterCalculator(ByVal outputRasterPath As String, ByVal expression As String)

        Dim gp As New ESRI.ArcGIS.Geoprocessor.Geoprocessor

        Dim toolRasterCalc As New RasterCalculator(expression, outputRasterPath)

        gp.OverwriteOutput = True

        Dim gpResult As IGeoProcessorResult = New GeoProcessorResult

        gpResult = gp.Execute(toolRasterCalc.ToolboxName, Nothing, Nothing)

    End Sub

I keep getting a nullreference exception at the gp.Execute line but I don't know why...

any help here?

BramVan_Helleputte1
New Contributor II

While having some more experience with the geoprocessors, I can suggest it's better to use an IGPProcess as the first argument of Execute(), the second arg is some default TrackCancel object that you first have to set up. So it's better to use the overloaded function with 2 arguments.

So it would look something like this (example with another tool)

STEPS:

1) Initalize tool object, set properties

Dim copyraster As New CopyRaster

        With copyraster

            .in_raster = inputpath

            .out_rasterdataset = outputPath

            .pixel_type = FrameworkConstants.RASTER_PIXEL_TYPE

        End With

2) Intialize the Geoprocessor

        Dim gp2 As ESRI.ArcGIS.Geoprocessor.Geoprocessor = New ESRI.ArcGIS.Geoprocessor.Geoprocessor()

        gp2.OverwriteOutput = True

        gp2.AddOutputsToMap = False

3) Create IGPProcess and TrackCancel object

        Dim result2 As ESRI.ArcGIS.Geoprocessing.IGeoProcessorResult = Nothing

        Dim pGPProcess2 As IGPProcess = DirectCast(copyraster, IGPProcess)

        Dim pTrackCancel As ITrackCancel = GetDefaultCancelTracker()

4) Execute and cast to IGeProcessorResult

        result2 = DirectCast(gp2.Execute(pGPProcess2, pTrackCancel), ESRI.ArcGIS.Geoprocessing.IGeoProcessorResult)

5) Check if object is filled and status is correct

        If (result2 Is Nothing) OrElse (result2.Status <> esriJobStatus.esriJobSucceeded) Then

            Throw New GeoProcessingException(GetFullToolName(copyraster), gp2)

        End If

If this doesn't work, you could sail a slightly different course and use Map Algebra instead, I prefer using this if I want to do a raster calculation

Help: ArcObjects Help for .NET developers