I have a script where I crop a list of rasters by the extent of one of many shapefiles. So far all of by shapefiles have been such that I could manually select the one with the largest extent (i.e. all others fit within the largest one). But I'm trying to make my script automated (and robust) so that it generates the largest extent considering a list of shapefiles that may overlap (i.e. one has greatest x_max, another greatest x_min).
Here's a (mostly) reproducible example (even though birds is within cities, it's a start). The output of my code would ideally generate something with the same as the extent of cities (a better example would be one with mixed values, but this is the best I could find).
library(rgdal)
library(raster)
dsn <- system.file('vectors', package = 'rgdal')[1]
cities <- readOGR(dsn = dsn, layer = 'cities')
birds <- readOGR(dsn=dsn, layer = "trin_inca_pl03")
shapefile_vector <- c(cities, birds)
shapefile_plots <- lapply(X = shapefile_vector, FUN = plot)
extent(cities)
extent(birds)
find_extent <- function(shapefile){
as.matrix(extent(shapefile))
}
shapefile_extents <- lapply(X = shapefile_vector, FUN = find_extent)
shapefile_extents
I can't figure out how to go from a list of matrices (shapefile_extents) to a list of absolute max coordinates to use in something like (taken from Crop a raster file in R):
e <- as(extent(-16, -7.25, 4, 12.75), 'SpatialPolygons')
crs(e) <- "+proj=longlat +datum=WGS84 +no_defs"
r <- crop(worldpopcount, e)