I have two rasters with the same resolution (10 m), but the difference is that I created the first raster from the vector file (using the rasterize
function) and the other raster is my classification result.
I want to apply the raster mask obtained from the vector to the classification result. However, when I check the extent of the two rasters, I find that they have a slightly different extent (about 3 m), even though they have the same projection (when I check crs()
of both rasters, they match for sure). Also the number of rows and columns is the same for both grids.
When I create a mask, I get the error message Error in compareRaster(x, mask) : different extent.
Do you know how I can align both rasters to the same starting point?
CodePudding user response:
From my experience this problem can be solved by using the resample()-function of the raster package to resample the mask to the spatial properties of the image you want to mask.
mask_new <- resample(mask, img, method="ngb")
Be careful to use an interpolation-method that is not changing the information in the mask-file. E.g., nearest-neighbor should work better than the standard "bilinear" in most cases.
If this does not solve the problem, you can additionally try to play around with the "tolerance" parameter using the rasterOptions()-function of the raster package.