This sample code demonstrates how to calculate slope using the ISurfaceOp::Slope method
How to use
- Start ArcMap and add an elevation raster dataset.
- Paste the code into VBA.
- Make sure the Spatial Analyst extension is checked.
- Add reference to ESRI SpatialAnalyst Object Library.
- Run the sub from the Macros dialog.
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