raster (version 1.1.7)

focalFilter: Focal filter

Description

Calculate values for the neighborhood of focal cells using a matrix of weights

Usage

focalFilter(raster, filter, fun=sum, filename="", ...)

Arguments

raster
A RasterLayer object
filter
a matrix of weights. See Details
fun
a function to apply to the product of the matrix of weights and the values
filename
Output filename for a new raster
...
additional arguments. See Details.

Value

  • A new RasterLayer object (in the R environment), and in some cases the side effect of a new file on disk.

Details

focalFilter uses a matrix of weights for the neigbhorhood of the focal cells, together with a function (normally sum). For example, filter=matrix(1/9, nrow=3, ncol=3) would be equivalent to mean with ngb=3 in the focal function. Gaussian filter: filter=matrix(c(1,2,3,2,1,2,3,4,3,2,3,4,5,4,3,2,3,4,3,2,1,2,3,2,1), nrow=5)/65 Laplacian filter: filter=matrix(c(0,1,0,1,-4,1,0,1,0), nrow=3) Sobel filter: filter=matrix(c(1,2,1,0,0,0,-1,-2,-1) / 4, nrow=3) Another example: filter=matrix(c(0,0,0,0,1,1,0,1,1), nrow=3) and fun=max returns the max value for the lower-rigth corner of a 3x3 matrix around a focal cell The following additional arguments can be passed, to replace default values for this function rll{ overwrite Logical. If TRUE, "filename" will be overwritten if it exists format Character. Output file type. See writeRaster datatype Character. Output data type. See dataType progress Character. Valid values are "text", "tcltk", "windows" (on that platform only) and "" }

See Also

focal

Examples

Run this code
r <- raster(ncols=36, nrows=18)
r[] <- runif(ncell(r)) 
gf=matrix(c(1,2,3,2,1,2,3,4,3,2,3,4,5,4,3,2,3,4,3,2,1,2,3,2,1), nrow=5)/65
rff <- focalFilter(r, filter = gf)

Run the code above in your browser using DataLab