getCosts | R Documentation |
A function that calculates the cost to travel between two sets of points. This can be between two circumscribed sets of points, or one circumscribed one ('nodes') and all other points on the landscape.
getCosts(
region,
from,
to = NULL,
id = "ID",
dir = tempdir(),
x = "x",
y = "y",
destination = "pairs",
polygons = "centroid",
costs = c("dt", "dW_l", "dE_l"),
direction = "both",
output = "object",
outname = deparse(substitute(from)),
overwrite = FALSE,
...
)
region |
A SpatVector, Spatial* or Raster* representing the area of maximum movement |
from |
One of data.frame, data.table, SpatVector, SpatialPointsDataFrame, or
SpatialPolygonsDataFrame representing the origin locations. If |
to |
One of data.frame, data.table, SpatVector SpatialPointsDataFrame, or
SpatialPolygonsDataFrame representing the origin locations. Optional if
|
id |
Character string representing the column containing the unique
IDs for for each location in the |
dir |
A filepath to the directory being used as the workspace.
Default is |
x |
A character vector representing the column containing the 'x' coordinates.
Required if |
y |
A character vector representing the column containing the 'y' coordinates.
Required if |
destination |
One of |
polygons |
One of |
costs |
A character vector containing any combination of the strings
of differential values present in the environment (see
|
direction |
A character vector containing one or both of |
output |
A character vector containing one or both of |
outname |
A character vector describing the name of the set of input
nodes for raster analysis. Ignored unless |
overwrite |
Should any output files with the same outname be overwritten?
Default is |
... |
Additional arguments to pass to |
There are four possible workflows:
(1) If you simply desire the distance between two sets of points, provide
entries for from
and to
(or just from
if the interest is
in all distances between locations in that object). Output is a distance matrix.
The computational time for this operation is comparable to generating a raster
for the distance to all cells in the world (unless all of the locations
in the object are close to each other). So unless the operation is to be done
multiple times, it is highly recommended to generate the rasters as below and extract
values
(2) If you wish to generate a RasterStack of costs from and/or to all nodes
in the from
object, set the output = 'object'
and
destination = 'all'
.
(3) You may also save the rasters as a series of .tif
files in the same workspace
directory as the transition .gz
tensor files and the cropped/downloaded
DEMs. This allows us to use getCosts
within a loop for large numbers of origin
nodes without running into random access memory limitations. Do this by
setting output = 'file'
and destination = 'all'
.
(4) You may perform (2) and (3) simultaneously by setting
output == c('file','object')
and destination = 'all'
.
A list, with entries for each combination of selected costs
and directions
. The object class of the list entries depends on the
destination
and output
parameters:
(1) If destination = 'pairs'
, entries are distance matrices.
(2) If destination = 'all'
and 'object' %in% output
,
entries are RasterStacks with each Raster* representing the cost to/from
each node.
(3) If destination = 'all'
and output == 'file'
, no list is output.
Moreover, if file %in% output
, then the cost rasters are saved in the
workspace directory.
#### Example 1:
n <- 5
dem <- expand.grid(list(x = 1:(n * 100),
y = 1:(n * 100))) / 100
dem <- as.data.table(dem)
dem[, z := 250 * exp(-(x - n/2)^2) +
250 * exp(-(y - n/2)^2)]
dem <- rast(dem)
ext(dem) <- c(10000, 20000, 30000, 40000)
crs(dem) <- "+proj=lcc +lat_1=48 +lat_2=33 +lon_0=-100 +datum=WGS84"
# Export it so it doesn't just exist on the memory
dir <- tempdir()
writeRaster(dem, paste0(dir,"/DEM.tif"),overwrite=TRUE)
# Import raster, get the grid
dem <- rast(paste0(dir,"/DEM.tif"))
grid <- makeGrid(dem = dem, nx = n, ny = n, sources = TRUE)
# Select all tiles that exist between x = (12000,16000) and y = (32000,36000)
tiles <- ext(c(12000,16000,32000,36000))
tiles <- as.polygons(tiles)
crs(tiles) <- crs(grid)
tiles <- whichTiles(region = tiles, polys = grid)
region <- grid[8,]
# Generate five random points that fall within the region
points <- data.table(ID = 1:5,
x = runif(5, ext(region)[1], ext(region)[2]),
y = runif(5, ext(region)[3], ext(region)[4]))
# Make a world but limit it to the DEM grid size
defineWorld(source = grid, cut_slope = 0.5,
res = res(dem), dir = dir, overwrite=TRUE)
# Get time and work costs between points
costMatrix <- getCosts(grid[8,], from = points, dir = dir,
costs = c('dt','dW_l'), costFUN = energyCosts,
m = 70, v_max = 1.5, BMR = 76, k = 3.5, s = 0.05, l_s = 1,
L = 0.8)
#### Example 2:
# Calculate the cost rasters to travel to ad from the center of a polygon
costRasters <- getCosts(grid[8,], from = grid[8,], dir = dir, destination = 'all',
polygons = 'center',
costs = c('dt','dW_l'), costFUN = energyCosts,
m = 70, v_max = 1.5, BMR = 76, k = 3.5, s = 0.05, l_s = 1,
L = 0.8)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.