calculateCosts: Calculate movement costs according to a cost function

View source: R/calculateCosts.R

calculateCostsR Documentation

Calculate movement costs according to a cost function

Description

A function that for a given world of possible movement calculates the transition cost for each in terms of a user-defined cost function

Usage

calculateCosts(
  tiles = NULL,
  costFUN = energyCosts,
  dir = tempdir(),
  costname = deparse(substitute(costFUN)),
  ...
)

Arguments

tiles

A character vector–such as the output to whichTiles—containing the unique tile IDs for sectors that should be in the workspace. Default is NULL.

costFUN

A cost function such as (timeCosts or energyCosts). The input to such a function should be a data.table object with column names present in the makeWorld file (initially c('x_i','x_f','y_i','y_f','z_i','z_f','dz','dl','dr')), and the output should be an data.table object with the same number of rows and the desired output variables as the only column names. Constants can be passed in the ... slot. Default is costFUN = energyCosts

dir

A filepath to the directory being used as the workspace, the same one instantiated with defineWorld. Default is tempdir() but unless the analyses will only be performed a few times it is highly recommended to define a permanent workspace. whichTiles—containing the unique tile IDs for sectors that should be in the workspace. Default is NULL.

costname

A name to save the cost call parametrs. Default is the name of the costFUN variable.

...

Additional parameters to pass to costFUN

Value

An .fst file for each sector named after its sector id stored in the /World/Diff directory, and/or a data.table object (depending on the output parameter) containing a data.table with at least eight columns

(1) $from The "x,y"-format coordinates of each possible origin cell

(2) $to The "x,y"-format coordinates of each possible destination cell

(3) $dz The change in elevation between the from and to cells

(4) $x_i The numeric x coordinate of the origin cell

(5) $y_i The numeric y coordinate of the origin cell

(6) $dl The planimetric distance between the from and to cells

(7) $dr The 3D distance between the from and to cells

(8+) The output cost variables

Examples

# Generate a DEM
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)

# 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)

# Calculate the costs within the world
calculateCosts(tiles = tiles, dir = dir,
m = 70, v_max = 1.5, BMR = 76, k = 3.5, s = 0.05, l_s = 1,
L = 0.8)

andresgmejiar/lbmech documentation built on Feb. 2, 2025, 12:37 a.m.