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?

1 Answer 1


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.


## 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[58] <- 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[1], y, y[1]), ncol = 2)))), ID = 1)))

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

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
    Commented Jan 4, 2018 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
    Commented Jan 5, 2018 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
    Commented Jan 5, 2018 at 1:06
  • 1
    Change any(!is.na(raster::extract(rast, checkArea)[[1]])) to any(raster::extract(rast, checkArea)[[1]] > 2). Please marked as solved if this answer solves your problem.
    – JMT2080AD
    Commented Jan 5, 2018 at 21:26
  • 1
    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
    Commented Jan 5, 2018 at 21:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.