How to calculate slope using RasterSurfaceOp


This sample code demonstrates how to calculate slope using the ISurfaceOp::Slope method

How to use

  1. Start 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 ESRI SpatialAnalyst Object Library.
  5. Run the sub from the Macros dialog.
[VBA]
Sub Calculate_Slope()
    'Get the focused Map from MapDocument
    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 pLayer As ILayer
    Dim pRasLayer As IRasterLayer
    Dim pInRaster As IRaster
    
    Set pLayer = pMap.Layer(0)
    If Not TypeOf pLayer Is IRasterLayer Then Exit Sub
    
    Set pRasLayer = pLayer
    Set pInRaster = pRasLayer.Raster
    
    'Create a RasterSurfaceOp operator
    Dim pSurfaceOp As ISurfaceOp
    Set pSurfaceOp = New RasterSurfaceOp
    
    'Set output workspace
    Dim pEnv As IRasterAnalysisEnvironment
    Set pEnv = pSurfaceOp
    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 = pSurfaceOp.Slope(pInRaster, esriGeoAnalysisSlopeDegrees)
    
    'Create a raster layer from output and add it into ArcMap
    Dim pOutRasLayer As IRasterLayer
    Set pOutRasLayer = New RasterLayer
    pOutRasLayer.CreateFromRaster pOutRaster
    pMap.AddLayer pOutRasLayer
End Sub