ArcObjects Library Reference  

CreateMathFunctionRasterDataset

About the Create a Math Function Raster dataset Sample

[C#]

CreateMathFunctionRasterDataset.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ESRI.ArcGIS.DataSourcesRaster;
using ESRI.ArcGIS.SpatialAnalyst;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.esriSystem;

namespace CreateMathFunctionRasterDataset
{
    class Program
    {
        static void Main(string[] args)
        {
            ESRI.ArcGIS.RuntimeManager.Bind(ESRI.ArcGIS.ProductCode.Desktop);
            IAoInitialize aoInit = new AoInitialize();
            esriLicenseStatus licStat = aoInit.Initialize(esriLicenseProductCode.esriLicenseProductCodeEngine);
            licStat = aoInit.CheckOutExtension(esriLicenseExtensionCode.esriLicenseExtensionCodeSpatialAnalyst);

            CreateMathFunctionRasterDataset();
            aoInit.Shutdown();
        }

        public static void CreateMathFunctionRasterDataset()
        {
            //Create the Raster Function object and Function Arguments object for first operation 
            IRasterFunction rasterFunction1 = new MathFunction();
            IMathFunctionArguments mathFunctionArguments1 = new MathFunctionArguments() as IMathFunctionArguments;

            //Specify operation to be "Plus" for the first operation
            mathFunctionArguments1.Operation = esriGeoAnalysisFunctionEnum.esriGeoAnalysisFunctionPlus;

            //Specify input rasters to the operation
            IRasterDataset ras01 = OpenRasterDataset("c:\\data\\test", "degs");
            IRasterDataset ras02 = OpenRasterDataset("c:\\data\\test", "negs");
            mathFunctionArguments1.Raster = ras01;
            mathFunctionArguments1.Raster2 = ras02;            

            //Create and initialize 1st function raster dataset with the Raster Function object and its arguments object
            IFunctionRasterDataset functionRasterDataset1;
            functionRasterDataset1 = new FunctionRasterDataset();
            functionRasterDataset1.Init(rasterFunction1, mathFunctionArguments1);

            //Create the Raster Function and the Function Arguments object for the 2nd operation
            IRasterFunction rasterFunction2 = new MathFunction();
            IMathFunctionArguments mathFunctionArguments2 = new MathFunctionArguments() as IMathFunctionArguments;

            //Specify operation to be "Divide" for the 2nd operation
            mathFunctionArguments2.Operation = esriGeoAnalysisFunctionEnum.esriGeoAnalysisFunctionDivide;

            //Specify input rasters to the 2nd operation
            //Use the output function raster dataset from the 1st operation as one of the input             
            mathFunctionArguments2.Raster = functionRasterDataset1;
            IRasterDataset ras03 = OpenRasterDataset("c:\\data\\test", "cost");
            mathFunctionArguments2.Raster2 = ras03;

            //Create and initialize the 2nd function raster dataset
            IFunctionRasterDataset functionRasterDataset2;
            functionRasterDataset2 = new FunctionRasterDataset();
            IFunctionRasterDatasetName functionRasterDatasetName = (IFunctionRasterDatasetName)new FunctionRasterDatasetName();
            functionRasterDatasetName.FullName = "c:\\output\\math_out.afr";
            functionRasterDataset2.FullName = (IName)functionRasterDatasetName;
            functionRasterDataset2.Init(rasterFunction2, mathFunctionArguments2);
                        
            //Save the 2nd function raster dataset            
            ITemporaryDataset temporaryDataset = (ITemporaryDataset)functionRasterDataset2;            
            temporaryDataset.MakePermanent();
        }        
        
        public static IRasterDataset OpenRasterDataset(string sPath, string sFileName)
        {
            // Returns RasterDataset object given a file name and its directory.
            // sPath: path of the input raster dataset.
            // sFileName: name of the input raster dataset.

            IRasterDataset rasterDataset = null;

            try
            {
                IWorkspaceFactory workspaceFactory = new RasterWorkspaceFactory();
                IRasterWorkspace rasterWorkspace;

                rasterWorkspace = (IRasterWorkspace)workspaceFactory.OpenFromFile(sPath, 0);
                rasterDataset = rasterWorkspace.OpenRasterDataset(sFileName);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Failed in Opening RasterDataset. " + ex.InnerException.ToString());
            }

            return rasterDataset;
        }

        public static IRasterWorkspace OpenRasterWorkspace(string sPath)
        {
            IWorkspaceFactory workspaceFactory = new RasterWorkspaceFactory();
            IRasterWorkspace rasterWorkspace = null;

            rasterWorkspace = (IRasterWorkspace)workspaceFactory.OpenFromFile(sPath, 0);

            return rasterWorkspace;
        }
    }
}

[Visual Basic .NET]

CreateMathFunctionRasterDataset.vb

Imports ESRI.ArcGIS.DataSourcesRaster
Imports ESRI.ArcGIS.SpatialAnalyst
Imports ESRI.ArcGIS.Geodatabase
Imports ESRI.ArcGIS.Geometry
Imports ESRI.ArcGIS.esriSystem

Module Module1

    Sub Main()
        ESRI.ArcGIS.RuntimeManager.Bind(ESRI.ArcGIS.ProductCode.Desktop)
        Dim aoInit As IAoInitialize
        aoInit = New AoInitialize()
        Dim licStat As esriLicenseStatus
        licStat = aoInit.Initialize(esriLicenseProductCode.esriLicenseProductCodeEngine)
        licStat = aoInit.CheckOutExtension(esriLicenseExtensionCode.esriLicenseExtensionCodeSpatialAnalyst)

        CreateMathFunctionRasterDataset()
        aoInit.Shutdown()
    End Sub

    Public Sub CreateMathFunctionRasterDataset()

        'Create the Raster Function object and Function Arguments object for first operation 
        Dim rasterFunction1 As IRasterFunction
        rasterFunction1 = New MathFunction()
        Dim mathFunctionArguments1 As IMathFunctionArguments
        mathFunctionArguments1 = New MathFunctionArguments()

        'Specify operation to be "Plus" for the first operation
        mathFunctionArguments1.Operation = esriGeoAnalysisFunctionEnum.esriGeoAnalysisFunctionPlus

        'Specify input rasters to the operation
        Dim ras01 As IRasterDataset
        Dim ras02 As IRasterDataset
        ras01 = OpenRasterDataset("c:\data\test", "degs")
        ras02 = OpenRasterDataset("c:\data\test", "negs")
        mathFunctionArguments1.Raster = ras01
        mathFunctionArguments1.Raster2 = ras02

        'Create and initialize 1st function raster dataset with the Raster Function object and its arguments object
        Dim functionRasterDataset1 As IFunctionRasterDataset
        functionRasterDataset1 = New FunctionRasterDataset()
        functionRasterDataset1.Init(rasterFunction1, mathFunctionArguments1)

        'Create the Raster Function and the Function Arguments object for the 2nd operation
        Dim rasterFunction2 As IRasterFunction
        rasterFunction2 = New MathFunction()
        Dim mathFunctionArguments2 As IMathFunctionArguments
        mathFunctionArguments2 = New MathFunctionArguments()

        'Specify operation to be "Divide" for the 2nd operation
        mathFunctionArguments2.Operation = esriGeoAnalysisFunctionEnum.esriGeoAnalysisFunctionDivide

        'Specify input rasters to the 2nd operation
        'Use the output function raster dataset from the 1st operation as one of the input   
        mathFunctionArguments2.Raster = functionRasterDataset1
        Dim ras03 As IRasterDataset
        ras03 = OpenRasterDataset("c:\data\test", "cost")
        mathFunctionArguments2.Raster2 = ras03

        'Create and initialize the 2nd function raster dataset
        Dim functionRasterDataset2 As IFunctionRasterDataset
        functionRasterDataset2 = New FunctionRasterDataset()
        Dim functionRasterDatasetName As IFunctionRasterDatasetName
        functionRasterDatasetName = New FunctionRasterDatasetName()
        functionRasterDatasetName.FullName = "c:\output\math_out.afr"
        functionRasterDataset2.FullName = functionRasterDatasetName
        functionRasterDataset2.Init(rasterFunction2, mathFunctionArguments2)

        'Save the 2nd function raster dataset
        Dim temporaryDataset As ITemporaryDataset
        temporaryDataset = functionRasterDataset2
        temporaryDataset.MakePermanent()
        
    End Sub

    Public Function OpenRasterDataset(ByVal sPath As String, ByVal sFileName As String) As IRasterDataset

        ' Returns RasterDataset object given a file name and its directory.
        ' sPath: path of the input raster dataset.
        ' sFileName: name of the input raster dataset.

        Dim rasterDataset As IRasterDataset = Nothing

        Try
            Dim workspaceFactory As IWorkspaceFactory = New RasterWorkspaceFactory()
            Dim rasterWorkspace As IRasterWorkspace

            rasterWorkspace = CType(workspaceFactory.OpenFromFile(sPath, 0), IRasterWorkspace)
            rasterDataset = rasterWorkspace.OpenRasterDataset(sFileName)
        Catch ex As Exception
            Console.WriteLine("Failed in Opening RasterDataset. " & ex.InnerException.ToString)
        End Try

        Return rasterDataset

    End Function

    Public Function OpenRasterWorkspace(ByVal sPath As String) As IRasterWorkspace
        Dim workspaceFactory As IWorkspaceFactory
        workspaceFactory = New RasterWorkspaceFactory()

        Dim rasWS As IRasterWorkspace
        rasWS = Nothing

        rasWS = workspaceFactory.OpenFromFile(sPath, 0)
        OpenRasterWorkspace = rasWS
    End Function

End Module