raster_tools.distance.cost_distance_analysis#
- raster_tools.distance.cost_distance_analysis(costs, sources, elevation=None)[source]#
Calculate accumulated cost distance, traceback, and allocation.
This function uses Dijkstra’s algorithm to compute the many-sources shortest-paths solution for a given cost surface. Valid moves are from a given pixel to its 8 nearest neighbors. This produces 3 rasters. The first is the accumulated cost distance, which contains the distance-weighted accumulated minimum cost to each pixel. The cost to move from one pixel to the next is
length * mean(costs[i], costs[i+1])
, wherelength
is 1 for horizontal and vertical moves andsqrt(2)
for diagonal moves. The costs raster’s resolution informs the actual scaling to use. Source locations have a cost of 0. If elevation provided, the length calculation incorporates the elevation data to make the algorithm 3D aware.The second raster contains the traceback values for the solution. At each pixel, the stored value indicates the neighbor to move to in order to get closer to the cost-relative nearest source. The numbering is as follows:
6 7 8 5 X 1 4 3 2
Here, X indicates the current pixel and the numbers are the neighbor pixel positions. 1 indicates the neighbor immediately to the right and 7 indicates the neighbor immediately above. In terms of rows and columns, these are the neighbor +1 column over and +1 row above, respectively. a value of 0 indicates that the current pixel is a source pixel and -1 indicates that the pixel was not traversed due to a null value.
The third raster contains the source allocation for each pixel. Each pixel is labeled based on the source location that is closest in terms of cost distance. The label is the value stored in the sources raster at the corresponding source location.
- Parameters
costs (Raster or raster path) – A raster representing a cost surface. Values less than 0 are treated as null values.
sources (Raster or raster path, or sequence) – A raster or sequence of indices. If a raster, pixels that are not null are used as source locations. The raster must have a null value set. The values at valid locations are used as the corresponding values in the allocation output. If a sequence, must have shape (M, 2) where M is the number of source pixels. Each item represents an index into costs to be used as a source. The element number, starting at 0, is used as the corresponding allocation value.
elevation (Raster or raster path, optional) – A raster containing elevation values on the same grid as costs. If provided, the elevation values are used when calculating the travel distance between pixels. This makes the algorithm 3D aware.
- Returns
cost_distance (Raster) – The accumulated cost distance solution. This is the same shape as the costs input Raster and has the same null value.
traceback (Raster) – The traceback result. This is the same shape as the costs input Raster.
allocation (Raster) – The allocation result. This is the same shape as the costs input Raster.
References