I am having 20 raster layers and 1 shapefile. I would like to:
crop and mask each raster iteratively (i.e., first crop and mask the first raster, then the second and so on)
save each cropped raster with the same name as its original into a subfolder of the working directory.
I can do this for every raster (manually, one-by-one) but I would like to automate the process. My raster layers do not have the same extent, that's why I want to crop and mask each one by one (also they are large in size (mb)). Here is the code for doing this in a one-by-one manner:
library(terra) setwd("path") r1 = rast("path/a.tif") r2 = rast("path/b.tif") poly <- vect("path/poly.shp") r1 <- crop(r1, poly, mask = T) r2 <- crop(r2, poly, mask = T) dir.create(file.path(getwd(), 'clip'), recursive = T) writeRaster(r1, "path/clip/r1.tif") writeRaster(r2, "path/clip/r2.tif")
From here you can download a small subset the data.