Setting analysis environment


This sample shows the basic steps to set RasterAnalysisEnvironment by creating a raster using a RasterMaker Operator.

How to use:
  1. Start ArcMap and paste the code into VBA.
  2. Change the output directory if necessary.
  3. Set the reference: ESRI Spatial Analyst Shared Object Library.
  4. Make sure the Spatial Analyst Extension is checked.
  5. Run the procedure: SetAnalysisEnvironment().
Sub SetAnalysisEnvironment()
     ' Create a RasterMaker operator
     Dim pRMakerOp As IRasterMakerOp
    Set pRMakerOp = New RasterMakerOp

     ' Create analysis environment
     Dim pEnv As IRasterAnalysisEnvironment
    Set pEnv = pRMakerOp

     ' set output workspace
     Dim pWS As IWorkspace
    Dim pWSF As IWorkspaceFactory
    Set pWSF = New RasterWorkspaceFactory
    Set pWS = pWSF.OpenFromFile("c:\temp", 0)
    Set pEnv.OutWorkspace = pWS
     ' Set cell size
     pEnv.SetCellSize esriRasterEnvValue, 30

     ' Set output extent
     Dim pExt As IEnvelope
    Set pExt = New Envelope
    pExt.XMin = 0
    pExt.YMin = 0
    pExt.XMax = 3000
    pExt.YMax = 3000
    pEnv.SetExtent esriRasterEnvValue, pExt

     ' Set output spatial reference
     Dim pPRJ As ISpatialReference
    Dim pSRS As ISpatialReferenceFactory2
    Set pSRS = New SpatialReferenceEnvironment
    Set pPRJ = pSRS.CreateSpatialReference(54008)   'PCS code for Sinusodial
    Set pEnv.OutSpatialReference = pPRJ

     ' Perform spatial operation
     Dim pOutRaster As IRaster
    Set pOutRaster = pRMakerOp.MakeConstant(10, True)

     ' Add the result to Map
     Dim pMxDoc As IMxDocument
    Set pMxDoc = ThisDocument
    Dim pMap As IMap
    Set pMap = pMxDoc.FocusMap
    Dim pRLayer As IRasterLayer
    Set pRLayer = New RasterLayer
    pRLayer.CreateFromRaster pOutRaster
End Sub