Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

patches

Detect patches (clumps) of cells


Description

Detect patches (clumps). Patches are groups of cells that are surrounded by cells that are NA. Set zeroAsNA to TRUE to also identify patches separated by cells with values of zero.

Usage

## S4 method for signature 'SpatRaster'
patches(x, directions=4, zeroAsNA=FALSE, filename="", ...)

Arguments

x

SpatRaster

directions

integer indicating which cells are considered adjacent. Should be 8 (Queen's case) or 4 (Rook's case)

zeroAsNA

logical. If TRUE treat cells that are zero as if they were NA

filename

character. Output filename

...

options for writing files as in writeRaster

Value

SpatRaster. Cell values are either a patch number

See Also

Examples

r <- rast(nrow=18, ncol=36, xmin=0)
r[1:2, 5:8] <- 1
r[5:8, 2:6] <- 1
r[7:12, 22:36] <- 1
r[15:16, 18:29] <- 1
p <- patches(r)

r <- rast(nrow=10, ncol=10, xmin=0)
r[] <- 0
r[3, 3] <- 10
r[4, 4] <- 10
r[5, 5:8] <- 12
r[6, 6:9] <- 12

# remove zeros if need be with zeroAsNA

p4 <- patches(r, zeroAsNA=TRUE)
p8 <- patches(r, 8, zeroAsNA=TRUE)

# patches for different values
# remove zeros
rr <- classify(r, cbind(0, NA))
# first make layers for each value
s <- segregate(rr, keep=TRUE, other=NA)
p <- patches(s)

terra

Spatial Data Analysis

v1.2-10
GPL (>= 3)
Authors
Robert J. Hijmans [cre, aut] (<https://orcid.org/0000-0001-5872-2872>), Roger Bivand [ctb] (<https://orcid.org/0000-0003-2392-6140>), Karl Forner [ctb], Jeroen Ooms [ctb] (<https://orcid.org/0000-0002-4035-0289>), Edzer Pebesma [ctb] (<https://orcid.org/0000-0001-8049-7069>)
Initial release
2021-05-12

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.