Learn R Programming

gdistance (version 1.1-2)

shortestPath: Shortest path

Description

Calculates the shortest path from an origin to a goal.

Usage

shortestPath(transition, origin, goal, ...)

Arguments

transition
TransitionLayer object
origin
SpatialPoints, vector or matrix with coordinates, at the moment only the first cell is taken into account
goal
SpatialPoints, vector or matrix with coordinates
...
Additional argument: output

Value

  • Transition object.

Details

As an additional argument output, the desired output object can be specified: either TransitionLayer (default), TransitionStack or SpatialLines. If there is more than one path either (1) transition values in the TransitionLayer get values of 1 / number of paths or (2) the SpatialLines object will contain more than one line.

See Also

costDistance, accCost

Examples

Run this code
#Create a new raster and set all its values to a random value.
r <- raster(nrows=18, ncols=36) 
r <- setValues(r,runif(ncell(r), 0.4, 0.6))

#Create a Transition object from the raster
tr <- transition(r,mean,8)
trC <- geoCorrection(tr, type="c")

#Create two sets of coordinates
sP1 <- SpatialPoints(cbind(-105,55))
sP2 <- SpatialPoints(cbind(105,-55))

#Calculate the shortest path
sPath1 <- shortestPath(tr, sP1, sP2)
sPath2 <- shortestPath(tr, sP1, sP2, output="SpatialLines")
plot(raster(sPath1))
lines(sPath2)

Run the code above in your browser using DataLab