How to check rasters with at least on cell falling within a polygon boundaries

I have some independent rasters (named r1, r2, r3, r4, and so on), congruent each other concerning reference system, extent and resolution. Each raster has cells that score the following values: 1, 2, 3, or NA.

Moreover, I have a layer represented by a polygon (named "boundary").

Is there a method to count and list (i.e. identify) all rasters which have at least one cell with no NA value falling within the polygon boundaries?

I'm working with R: could you provide me the appropriate code, please?

Essentially, you want to look at raster::extract, then look if there are any values in the vector that is returned. I have an example here that makes 10 rasters in a list, adds a value to the even number rasters, makes a polygon to extract with, then sapply over the raster list finding any vectors from extract that have a value.

library(raster)
library(sp)

## make rasters, evens have a 1 at index 58
rasts <- lapply(1:10, function(x) raster(matrix(rep(NA, 100), ncol = 10)))
hasValue <- seq(2, 10, 2)
rasts[hasValue] <- lapply(rasts[hasValue], function(x){x <- 1; return(x)})

## check that evens have a value
which(sapply(rasts, function(x) any(!is.na(getValues(x)))))

## make spatial polygon
x <- c(0.5, 0.6, 0.9)
y <- c(0.6, 0.2, 0.6)
checkArea <- SpatialPolygons(list(Polygons(list(Polygon(list(matrix(c(x, x, y, y), ncol = 2)))), ID = 1)))

## check for values in polygon
checkForValueInPolygon <- function(rast){
any(!is.na(raster::extract(rast, checkArea)[]))
}

rastersWithValue <- which(sapply(rasts, checkForNaInPolygon))

## does rasters with value match hasValue?
all(hasValue %in% rastersWithValue) & all(rastersWithValue %in% hasValue)
• Thank you, but the code does not work ("Error in stopifnot(is.list(srl)) : argument "srl" is missing, with no default). More important, if I understand correctly your script does not match my aims: I want to find every raster which has at least one cell with a value of 1 or greater falling whitin the polygon boundary. – Marco Jan 4 '18 at 23:39
• I removed the pipes, which might have been causing your error (works fine on my machine). I have also updated the example to look for values instead on NA. – JMT2080AD Jan 5 '18 at 0:16
• The code works now! Unfortunately, when I call "rastersWithValue" (using my layers) the output is: "integer(0)". Even if I am sure about the spatial consintency of my layers (rasters and polygons fit well when plotted together), I'm wondering if there are a trouble in my data which affect the functioning of the code – Marco Jan 5 '18 at 1:06
• Change any(!is.na(raster::extract(rast, checkArea)[])) to any(raster::extract(rast, checkArea)[] > 2). Please marked as solved if this answer solves your problem. – JMT2080AD Jan 5 '18 at 21:26
• Also, read up on raster::extract. You can access the help page by running ?raster::extract in your R terminal. You can do a lot with it in regards to finding values in a raster. – JMT2080AD Jan 5 '18 at 21:34