How to compute the least-accumulative-cost distance using PathDistance


This sample code demonstrates how compute the least-accumulative-cost distance using IDistanceOp2::PathDistance.

How to use

  1. Paste the code into VBA.
  2. Make sure the Spatial Analyst extension is checked.
  3. Add reference to ESRI SpatialAnalyst Object Library.
  4. Add reference to ESRI GeoAnalyst Object Library.
  5. Modify output workspace, horizontal and vertical factor in the code.
  6. Call the function ComputePathDistance from another sub or function and pass the source, cost, surface, horizontal and vertical rasters respectively.
  7. Save the raster returned by the function ComputePathDistance.
[VBA]
Function ComputePathDistance(pSource As IRaster, pCost As IRaster, pSurface As IRaster, pHorizontalRas As IRaster, pVerticalRas As IRaster) As IRaster
    'The functions demonstrate how to use the PathDistance method of esriSpatialAnalyst.IDistanceOp2'Calculates for each cell the least-accumulative-path distance over a path surface to a source cell or a set of source cells
    
    On Error GoTo ErrorHandler
    
    'Create a RasterDistanceOp object
    Dim pDistanceOp As IDistanceOp2
    Set pDistanceOp = New RasterDistanceOp
    
    'Open a RasterWorkspace
    Dim pWSF As IWorkspaceFactory
    Dim pWS As IWorkspace
    Dim pRasWS As IRasterWorkspace
    
    Set pWSF = New RasterWorkspaceFactory
    Set pWS = pWSF.OpenFromFile("C:\temp\myworkspace", 0)
    Set pRasWS = pWS
    
    'Create Raster Analysis Environment
    Dim pEnv As IRasterAnalysisEnvironment
    Set pEnv = pDistanceOp
    Set pEnv.OutWorkspace = pRasWS
    
    'Create Horizontal Factor
    Dim pHorizontalFactor As IPathDistanceHorizontalFactor
    Set pHorizontalFactor = New PathDistanceHorizontalFactor
    pHorizontalFactor.SetBinary 2, 60
    
    'Create Vertical Factor
    Dim pVerticalFactor As IPathDistanceVerticalFactor
    Set pVerticalFactor = New PathDistanceVerticalFactor
    pVerticalFactor.SetLinear 1.2, -33, 45, 1 / 50
    
    'Execute the method and return output as a Raster
    Dim pRasOut As IRaster
    Set pRasOut = pDistanceOp.PathDistance(pSource, pCost, pSurface, pHorizontalRas, pHorizontalFactor, pVerticalRas, pVerticalFactor)
    Set ComputePathDistance = pRasOut
    Exit Function
ErrorHandler:
    MsgBox Err.Description
End Function