makeWorld: Build the world from a defined environment

View source: R/makeWorld.R

makeWorldR Documentation

Build the world from a defined environment

Description

Function that defines the grid that can be traversed–the "world"–as well as the cells that can be accessed from each individual cell. This is the most time-intensive function.

Usage

makeWorld(
  tiles = NULL,
  dir = tempdir(),
  cols = c("x_i", "y_i", "dz", "dl", "dr"),
  output = "file"
)

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.

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.

cols

A character vector containing the name of the important spatial variables to be retained. Default is cols = c("x_i","y_i","z_i","z_f","dz","dl","dr"), but c("x_f","y_f") are also available.

output

A character string or vector, consisting of one or both of c('file','object'), representing whether a file should be written and/or whether an object should be returned. Default is output = file.

Details

It first checks to see if the required elevation models have been downloaded for each source file for the requested tiles grid, and then if transition .gz then if they have been converted to a sector's local files have already been created in the dir workspace. If not, it generates each at each step

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 five columns

(1) $from, a character string of all possible origin cells in format "x,y", rounded to the next-lowest integer

(2) $to, a character string of all possible destination cells in format "x,y" rounded to the next-lowest integer

(3) $dz, a numeric representing the change in elevation for each origin-destination pair

(4) $dl, a numeric representing the change in planimetric distance (x,y)

(5) $dr, a numeric representing the change in displacement (x,y,z)

Likewise, in the /World/Loc directory the local z elevation values projected to the locally defined grid as a writeRST file.

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)
            
makeWorld(tiles = tiles, dir = dir)

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