bfastSpatial (version 0.6.2)

areaSieve: Apply a sieve to a raster layer

Description

Applies an areal threshold to a raster layer. A threshold is supplied in square metres (to be generalized in future) and all pixel 'clumps' smaller than this threshold are deleted.

Usage

areaSieve(x, thresh = 5000, directions = 8, verbose = FALSE,
  keepzeros = FALSE, cores = 1, ...)

Arguments

x

Input raster layer.

thresh

Numeric. Areal threshold (in square metres). All pixel clumps smaller than this threshold will be deleted.

directions

Numeric. Define pixel neighbours using diagonals (directions=8; "Queen's case") or without diagonals (directions=4; "Rook's case"). See clump for more information.

keepzeros

Logical. Treat zeros as NA's (default option for clump) or as actual pixel values? If FALSE (default), the default method used in clump is used. If TRUE, an intermediate unit raster is first created, areaSieve is run on that raster, and the sieved unit raster is used to mask the input raster.

cores

Numeric. Number of cores to use for parallel processing (only valid if x is a RasterBrick or RasterStack)

...

Additional arguments to be passed to writeRaster if nlayers(x) > 1, or to mask if nlayers(x) == 1 and keepzeroes == TRUE, or to overlay if nlayers(x) == 1 and keepzeroes==FALSE.

Value

raster with pixels in clumps smaller than that specified as thresh removed. All spatial parameters and data are otherwise identical to x.

Details

In all cases, a filename can be passed to areaSieve, but the arguments allowed by the ... differ depending on whether a multi-layered raster object is supplied - in which case writeRaster is used - or if a single-layered raster is supplied - in which case either mask or overlay is used.

areaSieve is based on the raster::clump, which by default ignores zeroes (ie. considers them as NA's). To consider zeroes as valid pixel values when applying the sieve, set keepzeroes to TRUE.

See Also

clump

Examples

Run this code
# NOT RUN {
# load test raster
data(tura)
# extract the first layer
r <- raster(tura, 1)

# create pixel 'islands' by reassigning some values to NA
r[r < 7500] <- NA
# zoom into extent where pixel islands were generated
e <- extent(c(821958, 822697, 830504, 831070))
plot(r, ext=e)

# apply areaSieve with default threshold of 5000 square metres
rs <- areaSieve(r)

# compare two rasters
op <- par(mfrow=c(1, 2))
plot(r, ext=e, legend=FALSE)
plot(rs, ext=e, legend=FALSE)
par(op)

# same as above, but assign 0 instead of NA
# (ie. simulate a situation where 0's are valid pixel values)
r <- raster(tura, 1)
r[r < 7500] <- 0

# apply areaSieve with default threshold of 5000 square metres and keep zeros
rs <- areaSieve(r, keepzeros = TRUE)

# compare two rasters
op <- par(mfrow=c(1, 2))
plot(r, ext=e, legend=FALSE)
plot(rs, ext=e, legend=FALSE)
par(op)
# }

Run the code above in your browser using DataCamp Workspace