Learn R Programming

terra (version 0.5-2)

overlay: Overlay SpatRaster objects

Description

Create a new SpatRaster object, by combining two or more SpatRaster objects.

You should supply a function fun to determine how the SpatRasters are combined. The number of arguments in the function must match the number of SpatRaster objects (or take any number). For example, if you combine two SpatRaster objects you could use multiply: fun=function(x,y){return(x*y)} percentage: fun=function(x,y){return(100 * x / y)}. If you combine three layers you could use fun=function(x,y,z){return((x + y) * z)}

Note that the function work for vectors (not only for single numbers). That is, it must return the same number of elements as its input vectors.

The function must take each layer as an argument.

Use app for functions such as sum, that take n arguments.

Usage

# S4 method for SpatRaster,SpatRaster
overlay(x, y, fun, ..., filename="", overwrite=FALSE, wopt=list())

Arguments

x

SpatRaster

y

SpatRaster

fun

function to be applied. The first two arguments the function should expect must be numerical vectors

...

additional arguments to be passed to fun

filename

character. Output filename. Optional

overwrite

logical. If TRUE, filename is overwritten

wopt

list. Options for writing files as in writeRaster

Value

SpatRaster

See Also

app, math

Examples

Run this code
# NOT RUN {
r1  <- rast(ncol=10, nrow=10)
r2  <- rast(ncol=10, nrow=10)
values(r1) <- 1:ncell(r1)
values(r2) <- 10
x <- overlay(r1, r2, fun=function(x,y){return(x/y)})
# }

Run the code above in your browser using DataLab