Set extent to multiple rasters based on a shapefile and save them while retaining their original names.

I have 20 rasters with the same coordinate system but with different extent. I want to set the same extent to them, based on an extent of a shapefile which is at the same coordinate system with the rasters. Also, the new rasters that will be created, I want them to retain their original names.
If some rasters have smaller extent compare to the shapefile, I want the new extent (of the rasters) to be filled with null values.
Could you please guide me on how I can do that, by giving me a step by step instructions?
Here is what I have done so far:



#Reading the shapefile (keep the extent for later)
myshp = readOGR("shapefile.shp")

Getting the spatial extent of the shapefile

e = extent(myshp)

Reading the rasters

r = list.files(path="my_dir",full.names = TRUE, pattern = "\.tif$")


take a look at crop & mask functions. Try on one raster and then use purrr to map it or a loop!

good luck

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.