EVOLUTION-MANAGER
Edit File: overlay.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>R: Overlay Raster objects</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <link rel="stylesheet" type="text/css" href="R.css" /> </head><body> <table width="100%" summary="page for overlay {raster}"><tr><td>overlay {raster}</td><td style="text-align: right;">R Documentation</td></tr></table> <h2>Overlay Raster objects</h2> <h3>Description</h3> <p>Create a new Raster* object, based on two or more Raster* objects. (You can also use a single object, but perhaps <code><a href="calc.html">calc</a></code> is what you are looking for in that case). </p> <p>You should supply a function <code>fun</code> to set the way that the RasterLayers are combined. The number of arguments in the function must match the number of Raster objects (or take any number). For example, if you combine two RasterLayers you could use multiply: <code>fun=function(x,y){return(x*y)}</code> percentage: <code>fun=function(x,y){return(100 * x / y)}</code>. If you combine three layers you could use <code>fun=function(x,y,z){return((x + y) * z)}</code> </p> <p>Note that the function must work for vectors (not only for single numbers). That is, it must return the same number of elements as its input vectors. Alternatively, you can also supply a function such as <code>sum</code>, that takes <code>n</code> arguments (as <code>'...'</code>), and perhaps also has a <code>na.rm</code> argument, like in <code>sum(..., na.rm)</code>. </p> <p>If a single mutli-layer object is provided, its layers are treated as individual RasterLayer objects if the argument <code>unstack=TRUE</code> is used. If multiple objects are provided, they should have the same number of layers, or it should be possible to recycle them (e.g., 1, 3, and 9 layers, which would return a RasterBrick with 9 layers). </p> <h3>Usage</h3> <pre> ## S4 method for signature 'Raster,Raster' overlay(x, y, ..., fun, filename="", recycle=TRUE, forcefun=FALSE) ## S4 method for signature 'Raster,missing' overlay(x, y, ..., fun, filename="", unstack=TRUE, forcefun=FALSE) </pre> <h3>Arguments</h3> <table summary="R argblock"> <tr valign="top"><td><code>x</code></td> <td> <p>Raster* object</p> </td></tr> <tr valign="top"><td><code>y</code></td> <td> <p>Raster* object, or missing (only useful if <code>x</code> has multiple layers)</p> </td></tr> <tr valign="top"><td><code>...</code></td> <td> <p>Additional Raster objects (and/or arguments for writing files as in <code><a href="writeRaster.html">writeRaster</a>)</code></p> </td></tr> <tr valign="top"><td><code>fun</code></td> <td> <p>Function to be applied. When using RasterLayer objects, the number of arguments of the function should match the number of Raster objects, or it should take any number of arguments. When using multi-layer objects the function should match the number of layers of the RasterStack/Brick object (unless unstack=FALSE) </p> </td></tr> <tr valign="top"><td><code>filename</code></td> <td> <p>Character. Output filename (optional) </p> </td></tr> <tr valign="top"><td><code>recycle</code></td> <td> <p>Logical. Should layers from Raster objects with fewer layers be recycled?</p> </td></tr> <tr valign="top"><td><code>unstack</code></td> <td> <p>Logical. Should layers be unstacked before computation (i.e. does the <code>fun</code> refer to individual layers in a multilayer object)?</p> </td></tr> <tr valign="top"><td><code>forcefun</code></td> <td> <p>Boolean. If <code>TRUE</code>, overlay will not attempt to internally use apply (it is rarely necessary to use this argument)</p> </td></tr> </table> <h3>Details</h3> <p>Instead of the overlay function you can also use arithmetic functions such as <code>*, /, +, -</code> with Raster objects (see examples). In that case you cannot specify an output filename. Moreover, the overlay function should be more efficient when using large data files that cannot be loaded into memory, as the use of the complex arithmetic functions might lead to the creation of many temporary files. </p> <p>While you can supply functions such as <code>sum</code> or <code>mean</code>, it would be more direct to use the Raster* objects as arguments to those functions (e.g. <code>sum(r1,r2,r3)</code>) </p> <p>See <code><a href="rasterize.html">rasterize</a></code> and <code><a href="extract.html">extract</a></code> for "overlays" involving Raster* objects and polygons, lines, or points. </p> <h3>Value</h3> <p>Raster* object </p> <h3>See Also</h3> <p><code> <a href="calc.html">calc</a>, <a href="Arith-methods.html">Arith-methods</a></code> </p> <h3>Examples</h3> <pre> 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){(x*y)} ) #use the individual layers of a RasterStack to get a RasterLayer s <- stack(r1, r2) r5 <- overlay(s, fun=function(x,y) x*y ) # equivalent to r5c <- calc(s, fun=function(x) x[1]*x[2] ) #Combine RasterStack and RasterLayer objects (s2 has four layers. # r1 (one layer) and s (two layers) are recycled) s2 <- stack(r1, r2, r3, r4) b <- overlay(r1, s, s2, fun=function(x,y,z){return(x*y*z)} ) # use a single RasterLayer (same as calc function) r6 <- overlay(r1, fun=sqrt) # 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 # Also works with multi-layer objects. s1 <- stack(r1, r2, r3) x <- overlay(s1, s1, fun=function(x,y)x+y+5) # in this case the first layer of the shorter object is recycled. # i.e., s2 is treated as stack(r1, r3, r1) s2 <- stack(r1, r3) y <- overlay(s1, s2, fun=sum) </pre> <hr /><div style="text-align: center;">[Package <em>raster</em> version 3.3-13 <a href="00Index.html">Index</a>]</div> </body></html>