This document is archived and information here might be outdated.  Recommended version.


Executing a map algebra expression (ArcObjects .NET 10.6 SDK)
ArcObjects Help for .NET developers > Developing with ArcGIS > Learning ArcObjects > ArcGIS extensions > Spatial Analyst > Executing a map algebra expression (ArcObjects .NET 10.6 SDK)

Executing a map algebra expression


Summary
This topic provides details on creating map algebra expressions for spatial analysis operations.

About executing a map algebra expression

In addition to the standard operator objects in ArcGIS Spatial Analyst, another way to apply raster operations is through RasterMapAlgebraOp and RasterModel. These two objects allow you to execute map algebra expressions on your data. Since, the entire expression is executed by the internal raster engine, it might be difficult to debug a complex expression performing several operations if an error occurs. These objects only perform operations on an ESRI grid and, therefore, data is converted internally when required. Use other analysis objects or geoprocessing tools wherever possible to take full advantage of better debugging options, optimized code, and improved performance. 
The following code example shows how to execute a complex multiline map algebra expression using RasterModel:
[C#]
public void UsingRasterModel()
{
    //Get rasters. 
    IRasterDataset inRasDS01;
    inRasDS01=OpenRasterDataset("c:\\temp", "elev1");
    IRasterDataset inRasDS02;
    inRasDS02=OpenRasterDataset("c:\\temp", "elev2");
    IRaster ras01=inRasDS01.CreateDefaultRaster();
    IRaster ras02=inRasDS02.CreateDefaultRaster();
    //Raster model 01 - Declare. 
    IRasterModel rasterModel;
    rasterModel=new RasterModelClass();
    //Raster model 02 - Create raster analysis environment.
    IRasterAnalysisEnvironment env;
    env=(IRasterAnalysisEnvironment)rasterModel;
    IWorkspaceFactory workspaceFactory=new RasterWorkspaceFactoryClass();
    IWorkspace workspace=workspaceFactory.OpenFromFile("c:\\temp", 0);
    env.OutWorkspace=workspace;
    //Raster model 03 - Script. 
    rasterModel.Script="[out]=[in01] + [in02]";
    //Raster model 04 - Bind. 
    rasterModel.BindRaster(ras01, "in01");
    rasterModel.BindRaster(ras02, "in02");
    //Raster model 05 - Execute. 
    rasterModel.Execute();
    //Raster model 06 - Get bound raster. 
    IRaster rasOut;
    rasOut=rasterModel.get_BoundRaster("out");
    //Raster model 07 - Unbind symbols. 
    rasterModel.UnbindSymbol("in01");
    rasterModel.UnbindSymbol("in02");
    rasterModel.UnbindSymbol("out");
    //Save output raster. 
    ISaveAs2 saveAs;
    saveAs=(ISaveAs2)rasOut;
    saveAs.SaveAs("rasmodel1", workspace, "GRID");
}
[VB.NET]
Public Sub UsingRasterModel()
    'Get rasters.
    Dim inRasDS01 As IRasterDataset
    inRasDS01=OpenRasterDataset("c:\temp", "elev1")
    Dim inRasDS02 As IRasterDataset
    inRasDS02=OpenRasterDataset("c:\temp", "elev2")
    Dim ras01 As IRaster
    ras01=inRasDS01.CreateDefaultRaster()
    Dim ras02 As IRaster
    ras02=inRasDS02.CreateDefaultRaster()
    'Raster model 01 - Declare.
    Dim rasterModel As IRasterModel
    rasterModel=New RasterModelClass()
    'Raster model 02 - Create raster analysis environment.
    Dim env As IRasterAnalysisEnvironment
    env=rasterModel
    Dim workspaceFactory As IWorkspaceFactory=New RasterWorkspaceFactoryClass()
    Dim workspace As IWorkspace=workspaceFactory.OpenFromFile("c:\temp", 0)
    env.OutWorkspace=workspace
    'Raster model 03 - Script.
    rasterModel.Script="[out]=[in01] + [in02]"
    'Raster model 04 - Bind.
    rasterModel.BindRaster(ras01, "in01")
    rasterModel.BindRaster(ras02, "in02")
    'Raster model 05 - Execute.
    rasterModel.Execute()
    'Raster model 06 - Get bound raster.
    Dim rasOut As IRaster
    rasOut=rasterModel.BoundRaster("out")
    'Raster model 07 - Unbind symbols.
    rasterModel.UnbindSymbol("in01")
    rasterModel.UnbindSymbol("in02")
    rasterModel.UnbindSymbol("out")
    'Save output raster.
    Dim saveAs As ISaveAs2
    saveAs=rasOut
    saveAs.SaveAs("rasmodel1", workspace, "GRID")
End Sub
The following code example shows how to create a map algebra expression using RasterMapAlgebraOp:
[C#]
public void UsingRasterMapAlgebra()
{
    //Get rasters. 
    IRasterDataset inRas01;
    inRas01=OpenRasterDataset("c:\\temp", "elev1");
    IRasterDataset inRas02;
    inRas02=OpenRasterDataset("c:\\temp", "elev2");
    //Create RasterMapAlgebraOp. 
    IMapAlgebraOp mapAlgebraOp;
    mapAlgebraOp=new RasterMapAlgebraOpClass();
    //Set environment. 
    IRasterAnalysisEnvironment env=default(IRasterAnalysisEnvironment);
    env=(IRasterAnalysisEnvironment)mapAlgebraOp;
    IWorkspaceFactory workspaceFactory=new RasterWorkspaceFactoryClass();
    IWorkspace workspace=workspaceFactory.OpenFromFile("c:\\temp", 0);
    env.OutWorkspace=workspace;
    //Bind rasters.
    mapAlgebraOp.BindRaster((IGeoDataset)inRas01, "Ras01");
    mapAlgebraOp.BindRaster((IGeoDataset)inRas02, "Ras02");
    //Execute script. 
    IRaster rasOut;
    rasOut=(IRaster)mapAlgebraOp.Execute("[Ras01] + [Ras02]");
    //Save output raster. 
    ISaveAs2 saveAs;
    saveAs=(ISaveAs2)rasOut;
    saveAs.SaveAs("outMA", workspace, "GRID");
}
[VB.NET]
Public Sub UsingRasterMapAlgebra()
    'Get rasters.
    Dim inRas01 As IRasterDataset
    inRas01=OpenRasterDataset("c:\temp", "degs")
    Dim inRas02 As IRasterDataset
    inRas02=OpenRasterDataset("c:\temp", "negs")
    'Create RasterMapAlgebraOp.
    Dim mapAlgebraOp As IMapAlgebraOp
    mapAlgebraOp=New RasterMapAlgebraOpClass()
    'Set environment.
    Dim env As IRasterAnalysisEnvironment
    env=mapAlgebraOp
    Dim workspaceFactory As IWorkspaceFactory=New RasterWorkspaceFactoryClass()
    Dim workspace As IWorkspace=workspaceFactory.OpenFromFile("c:\temp", 0)
    env.OutWorkspace=workspace
    'Bind rasters.
    mapAlgebraOp.BindRaster(inRas01, "Ras01")
    mapAlgebraOp.BindRaster(inRas02, "Ras02")
    'Execute script.
    Dim rasOut As IRaster
    rasOut=mapAlgebraOp.Execute("[Ras01] + [Ras02]")
    'Save output raster.
    Dim saveAs As ISaveAs2
    saveAs=rasOut
    saveAs.SaveAs("outMA", workspace, "GRID")
End Sub






To use the code in this topic, reference the following assemblies in your Visual Studio project. In the code files, you will need using (C#) or Imports (VB .NET) directives for the corresponding namespaces (given in parenthesis below if different from the assembly name):
Development licensing Deployment licensing
ArcGIS Desktop Basic: Spatial Analyst ArcGIS Desktop Basic: Spatial Analyst
ArcGIS Desktop Standard: Spatial Analyst ArcGIS Desktop Standard: Spatial Analyst
ArcGIS Desktop Advanced: Spatial Analyst ArcGIS Desktop Advanced: Spatial Analyst