EVOLUTION-MANAGER
Edit File: focal.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: Focal values</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 focal {raster}"><tr><td>focal {raster}</td><td style="text-align: right;">R Documentation</td></tr></table> <h2>Focal values</h2> <h3>Description</h3> <p>Calculate focal ("moving window") values for the neighborhood of focal cells using a matrix of weights, perhaps in combination with a function. </p> <h3>Usage</h3> <pre> ## S4 method for signature 'RasterLayer' focal(x, w, fun, filename='', na.rm=FALSE, pad=FALSE, padValue=NA, NAonly=FALSE, ...) </pre> <h3>Arguments</h3> <table summary="R argblock"> <tr valign="top"><td><code>x</code></td> <td> <p>RasterLayer</p> </td></tr> <tr valign="top"><td><code>w</code></td> <td> <p>matrix of weights (the moving window), e.g. a 3 by 3 matrix with values 1; see Details. The matrix does not need to be square, but the sides must be odd numbers. If you need even sides, you can add a column or row with weights of zero</p> </td></tr> <tr valign="top"><td><code>fun</code></td> <td> <p>function (optional). The function fun should take multiple numbers, and return a single number. For example mean, modal, min or max. It should also accept a <code>na.rm</code> argument (or ignore it, e.g. as one of the 'dots' arguments. For example, <code>length</code> will fail, but <code>function(x, ...){na.omit(length(x))}</code> works. </p> </td></tr> <tr valign="top"><td><code>filename</code></td> <td> <p>character. Filename for a new raster (optional)</p> </td></tr> <tr valign="top"><td><code>na.rm</code></td> <td> <p>logical. If <code>TRUE</code>, <code>NA</code> will be removed from focal computations. The result will only be <code>NA</code> if all focal cells are <code>NA</code>. Except for some special cases (weights of 1, functions like min, max, mean), using <code>na.rm=TRUE</code> is generally not a good idea in this function because it will unbalance the effect of the weights </p> </td></tr> <tr valign="top"><td><code>pad</code></td> <td> <p>logical. If <code>TRUE</code>, additional 'virtual' rows and columns are padded to <code>x</code> such that there are no edge effects. This can be useful when a function needs to have access to the central cell of the filter</p> </td></tr> <tr valign="top"><td><code>padValue</code></td> <td> <p>numeric. The value of the cells of the padded rows and columns</p> </td></tr> <tr valign="top"><td><code>NAonly</code></td> <td> <p>logical. If <code>TRUE</code>, only cell values that are <code>NA</code> are replaced with the computed focal values</p> </td></tr> <tr valign="top"><td><code>...</code></td> <td> <p>Additional arguments as for <code><a href="writeRaster.html">writeRaster</a></code></p> </td></tr> </table> <h3>Details</h3> <p><code>focal</code> uses a matrix of weights for the neighborhood of the focal cells. The default function is <code>sum</code>. It is computationally much more efficient to adjust the weights-matrix than to use another function through the <code>fun</code> argument. Thus while the following two statements are equivalent (if there are no <code>NA</code> values), the first one is faster than the second one: </p> <p><code>a <- focal(x, w=matrix(1/9, nc=3, nr=3))</code> </p> <p><code>b <- focal(x, w=matrix(1,3,3), fun=mean)</code> </p> <p>There is, however, a difference if <code>NA</code> values are considered. One can use the <code>na.rm=TRUE</code> option which may make sense when using a function like <code>mean</code>. However, the results would be wrong when using a weights matrix. </p> <p>Laplacian filter: <code>filter=matrix(c(0,1,0,1,-4,1,0,1,0), nrow=3)</code> </p> <p>Sobel filters: <code>fx=matrix(c(-1,-2,-1,0,0,0,1,2,1) / 4, nrow=3)</code> and <code>fy=matrix(c(1,0,-1,2,0,-2,1,0,-1)/4, nrow=3)</code> </p> <p>see the <code><a href="focalWeight.html">focalWeight</a></code> function to create distance based circular, rectangular, or Gaussian filters. </p> <h3>Value</h3> <p>RasterLayer </p> <h3>See Also</h3> <p><code><a href="focalWeight.html">focalWeight</a></code> </p> <h3>Examples</h3> <pre> r <- raster(ncols=36, nrows=18, xmn=0) values(r) <- runif(ncell(r)) # 3x3 mean filter r3 <- focal(r, w=matrix(1/9,nrow=3,ncol=3)) # 5x5 mean filter r5 <- focal(r, w=matrix(1/25,nrow=5,ncol=5)) # Gaussian filter gf <- focalWeight(r, 2, "Gauss") rg <- focal(r, w=gf) # The max value for the lower-rigth corner of a 3x3 matrix around a focal cell f = matrix(c(0,0,0,0,1,1,0,1,1), nrow=3) f rm <- focal(r, w=f, fun=max) # global lon/lat data: no 'edge effect' for the columns xmin(r) <- -180 r3g <- focal(r, w=matrix(1/9,nrow=3,ncol=3)) ## Not run: ## focal can be used to create a cellular automaton # Conway's Game of Life w <- matrix(c(1,1,1,1,0,1,1,1,1), nr=3,nc=3) gameOfLife <- function(x) { f <- focal(x, w=w, pad=TRUE, padValue=0) # cells with less than two or more than three live neighbours die x[f<2 | f>3] <- 0 # cells with three live neighbours become alive x[f==3] <- 1 x } # simulation function sim <- function(x, fun, n=100, pause=0.25) { for (i in 1:n) { x <- fun(x) plot(x, legend=FALSE, asp=NA, main=i) dev.flush() Sys.sleep(pause) } invisible(x) } # Gosper glider gun m <- matrix(0, nc=48, nr=34) m[c(40, 41, 74, 75, 380, 381, 382, 413, 417, 446, 452, 480, 486, 517, 549, 553, 584, 585, 586, 619, 718, 719, 720, 752, 753, 754, 785, 789, 852, 853, 857, 858, 1194, 1195, 1228, 1229)] <- 1 init <- raster(m) # run the model sim(init, gameOfLife, n=150, pause=0.05) ## Implementation of Sobel edge-detection filter ## for RasterLayer r sobel <- function(r) { fy <- matrix(c(1,0,-1,2,0,-2,1,0,-1), nrow=3) fx <- matrix(c(-1,-2,-1,0,0,0,1,2,1) , nrow=3) rx <- focal(r, fx) ry <- focal(r, fy) sqrt(rx^2 + ry^2) } ## End(Not run) </pre> <hr /><div style="text-align: center;">[Package <em>raster</em> version 3.3-13 <a href="00Index.html">Index</a>]</div> </body></html>