A function to automatically perform the raster arithmetic necessary to calculate the cost-of-travel for paths with multiple waypoints, and the predicted cost of taking a detour to any arbitrary point in the landscape (a 'corridor'). getCosts must have been run before this tool can be used.

makeCorridor(rasters = tempdir(), order, costs = "all", name = NULL)

Arguments

rasters

One of either a character string or multilayer SpatRaster. If character string, it represents the filepath to the workspace used as dir for the previous functions. Default is tempdir() but unless you are not following best practices you will have to change it to your output directory. If multilayer SpatRaster, it should be the output (or identical in form) to the getCosts function with "object" %in% output. Note that if files have been generated for two different from objects in the getCosts sharing an attribute with the same ID name the function may throw an error.

order

A character vector containing the desired path in order of visited nodes. For example, to visit "A" then "B" then "C" then "A" the vector would be c("A","B","C","A"). Note that these MUST correspond to the ID names for the from features used in the getCosts function and must have previously been calculated

costs

A character vector containing any combination of pre-calculated cost names (e.g. dt for time, dW_l for work using energyCosts) if the input world data.table is the output of of the calculateCosts function. This selects which types of costs will be calculated. costs = 'all' is shorthand for costs = c("dt","dW_l","dE_l") while costs = 'energetics' is shorthand for c("dW_l","dE_l"). Default is 'all'. Note that these must have previously been calculated.

name

A character vector representing the outname in getCosts. If none is provided, it will be the name of the variable passed to the function in the from slot. If length(costs) == 1, a Raster* If length(costs) > 1 a list of Raster* with one slot for each cost.

Value

Rasters representing cost corridors.

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)]
#>            x    y         z
#>      1: 0.01 0.01 1.0146139
#>      2: 0.02 0.01 1.0404641
#>      3: 0.03 0.01 1.0675194
#>      4: 0.04 0.01 1.0958300
#>      5: 0.05 0.01 1.1254477
#>     ---                    
#> 249996: 4.96 5.00 1.0711366
#> 249997: 4.97 5.00 1.0428260
#> 249998: 4.98 5.00 1.0157707
#> 249999: 4.99 5.00 0.9899205
#> 250000: 5.00 5.00 0.9652271
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)

# Import the data lying between x = (12000,16000) and y = (32000,36000)
region <- ext(c(12000,16000,32000,36000))
region <- as.polygons(region)
crs(region) <- crs(grid)

# 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)
            
# Calculate cost rasters
costRasters  <- getCosts(region, from = points, dir = dir,
                         destination = 'all',
                         polygons = 'center',
                         costs = 'all', costFUN = energyCosts,
                         output = c('object','file'),
                         m = 70, v_max = 1.5, BMR = 76, k = 3.5, s = 0.05, l_s = 1,
                         L = 0.8)
#> [1] "Cropping Tile SECTOR_01 (1 of 4)"
#> [1] "Cropping Tile SECTOR_02 (2 of 4)"
#> [1] "Cropping Tile SECTOR_06 (3 of 4)"
#> [1] "Cropping Tile SECTOR_07 (4 of 4)"
#> [1] "Cropping Tile SECTOR_03 (1 of 2)"
#> [1] "Cropping Tile SECTOR_08 (2 of 2)"
#> [1] "Cropping Tile SECTOR_11 (1 of 2)"
#> [1] "Cropping Tile SECTOR_12 (2 of 2)"
#> [1] "Cropping Tile SECTOR_13 (1 of 1)"
#> [1] "Cropping Tile SECTOR_16 (1 of 2)"
#> [1] "Cropping Tile SECTOR_17 (2 of 2)"
#> [1] "Cropping Tile SECTOR_18 (1 of 1)"
#> [1] "Cropping Tile SECTOR_09 (1 of 3)"
#> [1] "Cropping Tile SECTOR_14 (2 of 3)"
#> [1] "Cropping Tile SECTOR_19 (3 of 3)"
#> [1] "Cropping Tile SECTOR_21 (1 of 2)"
#> [1] "Cropping Tile SECTOR_22 (2 of 2)"
#> [1] "Cropping Tile SECTOR_23 (1 of 1)"
#> [1] "Cropping Tile SECTOR_04 (1 of 1)"
#> [1] "Cropping Tile SECTOR_05 (1 of 2)"
#> [1] "Cropping Tile SECTOR_10 (2 of 2)"
#> [1] "Cropping Tile SECTOR_15 (1 of 1)"
#> [1] "Cropping Tile SECTOR_24 (1 of 1)"
#> [1] "Cropping Tile SECTOR_20 (1 of 1)"
#> [1] "Cropping Tile SECTOR_25 (1 of 1)"
#> Imported  9  Sectors
#> 
  |                                                                            
  |                                                                      |   0%
#> Warning: C:\Users\andre\AppData\Local\Temp\RtmpU5cHvW\World\costFUN.gz already defined. Ignoring input parameters.
#> Imported  9  Sectors
#> 
  |                                                                            
  |============                                                          |  17%
  |                                                                            
  |=======================                                               |  33%
#> Warning: C:\Users\andre\AppData\Local\Temp\RtmpU5cHvW\World\costFUN.gz already defined. Ignoring input parameters.
#> Imported  9  Sectors
#> 
  |                                                                            
  |===================================                                   |  50%
  |                                                                            
  |===============================================                       |  67%
#> Warning: C:\Users\andre\AppData\Local\Temp\RtmpU5cHvW\World\costFUN.gz already defined. Ignoring input parameters.
#> Imported  9  Sectors
#> 
  |                                                                            
  |==========================================================            |  83%
  |                                                                            
  |======================================================================| 100%
                        
# Calculating the corridors from a list of RasterStacks,
# with path 1 -> 2 -> 4 -> 1 -> 5
corridors <- makeCorridor(rasters = costRasters, order = c(1,2,5,1,4),)

#### Example 2:
# Calculating the corridors from a workspace directory
# with path 1 -> 2 -> 4 -> 1 -> 5
corridors <- makeCorridor(rasters = dir, name = 'points', 
                          order = c(1,2,5,1,4))