Skip to contents

Create a SpatRaster with values reflecting a cell property: "x", "y", "xy", "col", "row", "cell" or "chess". Alternatively, a function can be used. In that case, cell values are initialized without reference to pre-existing values. For example, use fun=runif to initialize cells with random numbers.

For small objects that fit in memory, a vector containing all cell values can be assigned using, for instance, the values function. The init function also works when the object does not fit in memory (by writing it directly to the disk).

Usage

# S4 method for class 'SpatRaster'
init(x, fun, ..., mask=FALSE, filename="", overwrite=FALSE, wopt=list())

Arguments

x

SpatRaster

fun

function to be applied. This must be either a single number, multiple numbers, a function, or one of a set of known 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 or both, row, col or cell number or a chessboard pattern (alternating 0 and 1 values)

...

additional arguments passed to fun

mask

logical. If TRUE, cells that are NA in x (which must have values) are set to NA in the output

filename

character. Output filename

overwrite

logical. If TRUE, filename is overwritten

wopt

additional arguments for writing files as in writeRaster

Value

SpatRaster

Examples

r <- rast(ncols=10, nrows=5, xmin=0, xmax=10, ymin=0, ymax=5)
x <- init(r, fun="cell")
y <- init(r, fun=runif)

# initialize with a single value 
z <- init(r, fun=8)

# keep NA cells of x
e <- rast(system.file("ex/elev.tif", package="terra"))
lat <- init(e, fun="y", mask=TRUE)