# 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 DataLab