[R-sig-Geo] Raster: merge cells with sorrounding zone by area

Eduardo Diez eduardodiez at gmx.com
Sat Aug 26 22:41:37 CEST 2017


Dear group,
I usually work defining zones within agriculture fields taking into account
environmental layers. I have gridded data within the boundaries of the
field and cluster it to create management zones (between 3 and 6). Many
times i end up having in some groups or single cells of zone "x" within
zone "y". This results in spatially discontinuous zones. For visual (and
also practical) purposes i would like to get rid of these isolated patches
that are below a defined area size. That is, change the value of the
"isolated" cells for the one of the zone that surrounds them.
Example (sorry if i'm nor very good at making reproducible examples):

library(raster)
ext <- extent(732081.2, 732570.8, 6475458, 6475975)
r1 <- raster(ext, resolution = 50, crs = CRS("+init=epsg:32720"))
r1[1:33] <- 1
r1[34:66] <- 2
r1[67:100] <- 3
# Create "isolated" patches
r1[c(12, 17:18)] <- 2
r1[c(45:46,55)] <- 3
r1[c(77:78,86:87)] <- 1

The idea would be to define a minimum area threshold -in my example 11000
m2- that would detect groups of cells below that size and assign them the
value of the grater zone around it (around meaning 'rook' contiguity). This
would result in the original raster before the comment.

Thanks in advance,
Best

	[[alternative HTML version deleted]]



More information about the R-sig-Geo mailing list