Create Slope in VBA

Created:4/3/2001
Description:

This sample code demonstrates how to use Spatial Analyst operators in VBA using an example of ISurfaceOp::Slope

How to use:
  1. Run ArcMap and add an elevation raster dataset.
  2. Paste the code into VBA.
  3. Make sure the Spatial Analyst Extension is checked.
  4. Add reference to ERSI Spatial Analyst Shared Object Library.
  5. From the Macros dialog, run CreateSlope subroutine.
Sub CreateSlope() 
     ' Get the Map
     Dim pMxDoc As IMxDocument
    Set pMxDoc = ThisDocument
    Dim pMap As IMap
    Set pMap = pMxDoc.FocusMap  

   ' Get the input raster from the first layer in ArcMap
     Dim pRasterLy As IRasterLayer
    Dim pLy As ILayer
    Dim pInRaster As IRaster
    Set pLy = pMap.Layer(0)
    If Not TypeOf pLy Is IRasterLayer Then Exit Sub
    Set pRasterLy = pLy
    Set pInRaster = pRasterLy.Raster
  
   ' Create a Spatial operator
     Dim pSurOp As ISurfaceOp
    Set pSurOp = New RasterSurfaceOp 

    ' Set output workspace 
    Dim pEnv As IRasterAnalysisEnvironment
    Set pEnv = pSurOp
    Dim pWS As IWorkspace
    Dim pWSF As IWorkspaceFactory
    Set pWSF = New RasterWorkspaceFactory
    Set pWS = pWSF.OpenFromFile("c:\temp",0)
    Set pEnv.OutWorkspace = pWS 

    ' Perform Spatial operation
     Dim pOutRaster As IRaster
    Set pOutRaster = pSurOp.Slope(pInRaster, esriGeoAnalysisSlopeDegrees) 

    ' Add it into ArcMap 
    Set pRasterLy = New RasterLayer
    pRasterLy.CreateFromRaster pOutRaster
    pMap.AddLayer pRasterLy
End Sub