Create a SpatRaster with values reflecting a cell property: 'x', 'y', 'col', 'row', 'cell' or 'chess'. Alternatively, a function can be used. In that case, cell values are initialized without reference to pre-existing values. E.g., initialize with a random number (fun=runif
). While there are more direct ways of achieving this for small objects (see examples) for which a vector with all values can be created in memory, the init
function will also work for Raster* objects with many cells.
# S4 method for SpatRaster
init(x, fun, filename="", overwrite=FALSE, wopt=list(), ...)
SpatRaster
function to be applied. This must be a either a single number, a function, or one of a set of character values. A function must take the number of cells as a single argument to return a vector of values with a length equal to the number of cells, such as fun=runif
. Allowed character values are 'x', 'y', 'row', 'col', 'cell', and 'chess' to get the x or y coordinate, row, col or cell number or a chessboard pattern (alternating 0 and 1 values)
character. Output filename. Optional
logical. If TRUE
, filename
is overwritten
list. Options for writing files as in writeRaster
additional arguments. None implemented
SpatRaster
# NOT RUN {
r <- rast(ncol=10, nrow=5, xmn=0, xmx=10, ymn=0, ymx=5)
x <- init(r, fun="cell")
y <- init(r, fun=runif)
# initialize with a single value
z <- init(r, fun=8)
# }
Run the code above in your browser using DataLab