focalMultiBand: Apply a spatial filter to a Raster* object

Description Usage Arguments Details Value See Also Examples

View source: R/focalMultiBand.R

Description

Apply a spatial filter to a RasterLayer or all layers of a RasterStack or RasterBrick object. The mathematical operation applied within the neighborhood can be done by using a function (fun) or by setting the weights of the matrix w.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
focalMultiBand(
  x,
  w,
  fun,
  filename = "",
  na.rm = FALSE,
  pad = FALSE,
  padValue = NA,
  NAonly = FALSE,
  keepNA = TRUE,
  ...
)

Arguments

x

Raster* object or list of Raster* objects.

w

Matrix of weights (moving window). A 3x3 windows with weights of 1 would be w=matrix(1,nr=3,nc=3) for example.

fun

Function (optional). The function should accept a vector of values and return a single number (e.g. mean). It should also accept a na.rm argument.

filename

Character. Output file name including path to directory and eventually extension. If x is a list, filename must be a vector of characters with one file name for each element of x. Default is "" (output not written to disk).

na.rm

Logical. If TRUE (default), NAs are removed from computation

pad

Logical. IF TRUE, rows and columns are added around x to avoid removing border cells.

padValue

Numeric. Value of pad cells. Usually set to NA and used in combination with na.rm=TRUE

NAonly

Logical. If TRUE only cell values that are NA are replaced with the computed focal values.

keepNA

Logical. If TRUE (default), NA cells of x are unchanged

...

Additional arguments passed to writeRaster

Details

If x contains NA values and na.rm = TRUE is used , using fun or w with weights adjusted to apply equivalent mathematical operation might not produce the same outputs (in that case using weights would give wrong results). See the documentation of focal for more information.

Also, cells of x with NA values might get a non-NA value assigned when located in the neighborhood of non-NA cells and na.rm = TRUE is used. In that case, setting keepNA = TRUE (default) ensures that NA cells of x still have NA values in the output raster.

Value

Raster* object or list of Raster* objects.

See Also

focal

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
# Load raster package
library(raster)

# Open and stack ALS metrics
elev_p95 <- raster(system.file("extdata/examples/ALS_metrics_p95.tif",package="foster"))
cover <- raster(system.file("extdata/examples/ALS_metrics_cov_mean.tif",package="foster"))
Y_vars <- stack(elev_p95,cover)

#Define 3x3 filter with weights of 1
filt <- matrix(1, nrow = 3, ncol = 3)

# Smoothing
Y_vars_smooth <- focalMultiBand(x = Y_vars,
                                w=filt,
                                fun=mean,
                                pad=TRUE,
                                padValue=NA,
                                na.rm=TRUE,
                                keepNA = TRUE)

mqueinnec/foster documentation built on March 28, 2021, 4:27 p.m.