Last chance! 50% off unlimited learning
Sale ends in
fun
to set the way that the RasterLayers are combined. The number of arguments in the function must match the number of Raster objects. For example, if you combine two RasterLayers 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)}
If a single mutli-layer object is provided, its layers are treated as individual RasterLayers. If multiple multi-layer objects are provided, they should have the same number of layers, and a brick with the same number of layers is returned (objects could also have a single layer, which is recycled).
You can also supply a function such as sum
, that takes n
arguments. Note that the function must work for vectors (not only single numbers)overlay(x, y, ...)
1) x
and y
are Raster* objects
overlay(x, y, ..., fun, datatype, format, overwrite, progress)
x
Raster* object
y
Raster* object
...
Additional Raster* objects
fun
the function to be applied. The number of arguments of the function should match the number of Raster objects, or it should take any number of arguments
}
2) x
is a RasterStack or RasterBrick object, y
is missing
In this case, the function returns a single RasterLayer based on computations that combine the individual layers of the Raster* object.
overlay(x, fun, overwrite, format, datatype, progress)
x
RasterStack or RasterBrick object
fun
Function to be applied. The number of arguments of the function should match the number of layers of the RasterStack/Brick object
}
3) x
is a RasterLayer object, y
is missing
This is equivalent to using the calc
function
overlay(x, fun, overwrite, format, datatype, progress)
x
RasterLayer object
fun
Function to be applied. This should be a function that takes a single argument
}
In all cases you can use these arguments:
filename
filename for the output raster. A valid filename must be provided when the data of the input rasters are on disk
overwrite
logical. If TRUE
, existing files will be overwritten
format
Character. Output file type. See writeRaster
progress
Character. "text", "window", or "" (the default, no progress bar)
}*, /, +, -
with RasterLayer objects (see examples), but then you cannot specifiy an output filename. However, the overlay function should be more efficient when using large data files that cannot be loaded into memory, as the use of the standard arithmetic functions might cause the creation of many temporary files.
While you can supply functions such as sum
or mean
, it would be more direct to use the Raster* objects are arguments to those functions (e.g. sum(r1,r2,r3)
)
For "overlays" involving Raster objects and polyongs, lines, or points, see, e.g.:
polygonsToRaster, linesToRaster, pointsToRaster, xyValues, polygonValues
calc, Arith-methods
r <- raster(ncol=10, nrow=10)
r1 <- init(r, fun=runif)
r2 <- init(r, fun=runif)
r3 <- overlay(r1, r2, fun=function(x,y){return(x+y)})
# long version for multiplication
r4 <- overlay(r1, r2, fun=function(x,y){return(x*y)} )
#use the individual layers of a RasterStack to get a RasterLayer
s <- stack(r1, r2)
r5 <- overlay(s, fun=function(x,y){return(x*y)} )
#use two RasterStack objects to get a RasterBrick
s2 <- stack(r2, r1)
b <- overlay(s, s2, fun=function(x,y){return(x*y)} )
# use a single RasterLayer (same as calc function)
r6 <- overlay(r1, fun=function(x){return(sqrt(x))} )
# multiplication with more than two layers (make sure the number of RasterLayers matches the arguments of 'fun'
r7 <- overlay(r1, r2, r3, r4, fun=function(a,b,c,d){return(a*b+c*d)} )
# equivalent function, efficient if values can be loaded in memory
r8 <- r1 * r2 + r3 * r4
Run the code above in your browser using DataLab