I would set up a loop that reads each raster using the raster::raster
function. This creates a pointer so, in this regard, is very fast. You can then use the raster::extent
function to create a polygon representing the footprint of the given (i) raster. Using as(extent(i), "SpatialPolygons")
will create an actual sp class polygon object. Once you have this polygon you can use rgeos::gOverlaps
to test the intersection of the raster polygon with your jurisdictional polygon (eg., France). You can collect these results (image name, TRUE/FALSE) in a data.frame.
Here is an example that uses lapply in lieu of the for loop. The if check makes sure that the test polygon is in the same projection space as the first raster. Just ignore the warnings. Since we are creating an extent polygon on the fly, it does not have an assigned proj4string and so throws a warning regarding non-matching projection strings. I added a warning suppression but, it is not really necessary.
library(raster)
library(rgeos)
library(rgdal)
setwd(..)
r.list <- list.files(getwd(), "tif$")
p <- readOGR(getwd(), "france")
r <- raster(r.list[1])
if(proj4string(p) != proj4string(r)) p <- spTransform(p, proj4string(r))
options(warn=-1)
i <- lapply(r.list, FUN = function(x) { rgeos::gOverlaps( as(extent(raster(x)),
"SpatialPolygons"), p) })
( r.intersect <- data.frame(raster = r.list, intersects = unlist(i)) )
Now you can subset the rasters that do match.
( rmatch <- r.intersect[r.intersect$intersects == TRUE,]$raster )
If all you are after is the string of rasters that match, you can shortcut creating a data.frame and just use the index of TRUE matches and subset your raster list vector object directly.
r.list[which(unlist(lapply(r.list, FUN = function(x) { rgeos::gOverlaps( as(extent(raster(x)),
"SpatialPolygons"), p) })) == TRUE)]