Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

costDistance-methods

Cost distance


Description

Calculate the least-cost distance between points.

Usage

costDistance(x, fromCoords, toCoords)

Arguments

x

object of class TransitionLayer

fromCoords

first set of point locations (of SpatialPoints, matrix or numeric class)

toCoords

optional, second set of point locations (of SpatialPoints, matrix or numeric class)

Details

Cost units between cells are defined as the reciprocal of the values in the transition matrix.

The function uses Dijkstra's algorithm, as implemented in the igraph package.

A projection correction is needed for accuracy in the case of grid data for a longlat raster (see function geoCorrection).

Value

distance matrix (S3 class dist or matrix)

Author(s)

Jacob van Etten

References

E.W. Dijkstra. 1959. A note on two problems in connexion with graphs. Numerische Mathematik 1, 269 - 271.

See Also

Examples

library("raster")
# create a new raster and set all its values to unity.
r <- raster(nrows=18, ncols=36) 
r <- setValues(r,runif(ncell(r),0,1))

# create a Transition object from the raster
tr <- transition(r,function(x) 1/mean(x),8)

# asymmetric
ncf <- function(x) max(x) - x[1] + x[2]
tr2 <- transition(r,ncf,8, symm=FALSE)

# create two sets of coordinates
sP1 <- cbind(c(65,5,-65),c(55,35,-35))
sP2 <- cbind(c(50,15,-40),c(80,20,-5))

# from and to identical
costDistance(tr,sP1)
costDistance(tr2,sP1)

# from and to different
costDistance(tr,sP1,sP2)
costDistance(tr2,sP1,sP2)

gdistance

Distances and Routes on Geographical Grids

v1.3-6
GPL (>= 2)
Authors
Jacob van Etten [aut] (<https://orcid.org/0000-0001-7554-2558>), Kauê de Sousa [cre, ctb] (<https://orcid.org/0000-0002-7571-7845>)
Initial release

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.