2

So, I have two different rasters, and I am trying to match their extent, even though the difference in their existing extents is quite small, but still raster::stack won't stack which is understandable. But, when I use the crop function to crop one raster on the basis of the other raster's extent, their extents still don't match up i.e., xmax of rast1 doesn't change from 757322.8 to 757322.7. . How can I solve this problem, when the purpose of stacking them together to compute standard deviation? I also tried using the raster::extend function but that is giving me a memory error.

library(raster)  
rast1 = raster::brick("path/rast1.tif")

rast2 = raster::brick("path/rast2.tif")

# Get the extent 
extent(rast1)

class      : Extent 
xmin       : 756472.2 
xmax       : 757322.8 
ymin       : 4074670 
ymax       : 4074953 

extent(rast2)

class      : Extent 
xmin       : 756472.2 
xmax       : 757322.7 
ymin       : 4074670 
ymax       : 4074953 

extent_1 = c(756472.2 ,4074670 ,757322.7 ,4074953)
rast1_Crop = crop(rast1, extent_1)
stack = raster::stack(rast1_Crop , rast_2)

Error

Error in compareRaster(x) : different extent
3
  • 2
    You must warp one image to match the extent and pixel size of the other. I do not know how to do that with r. Myself I would use gdalwarp gdal.org/programs/gdalwarp.html with well selected -te and -tr parameters. Warping leads to resampling and it may alter the pixel values but with your data that should not happen with nearest neighbor resampling because pixels will be only slightly shifted.
    – user30184
    Jul 9 at 6:04
  • Both of the rasters have the same pixel size and CRS. Jul 9 at 6:12
  • 1
    I meant that pixel size and extent must both match. In your case warping would effectively slide one image to share the same xmin and xmax. Cropping selects pixels without altering them in any way.
    – user30184
    Jul 9 at 6:18
3

crop can only cut entire cells (rows, columns) so that won't help you here.

If you want to ignore the small difference, and the rasters have the same number of rows and columns, you can do

library(raster)
r1 = raster::brick("path/rast1.tif")
r2 = raster::brick("path/rast2.tif")
extent(r2) <- extent(r1)
s <- stack(r1, r2)

That is not generally a good idea; but given the very small difference it would seem OK here. And it is very efficient.

More generally, you can resample one raster to the structure of another like this

library(raster)
r1 = raster::brick("path/rast1.tif")
r2 = raster::brick("path/rast2.tif")
r1 <- resample(r1, r2)
s <- stack(r1, r2)

For better speed with resample, use terra instead

library(terra)
r1 = rast("path/rast1.tif")
r2 = rast("path/rast2.tif")
r1 <- resample(r1, r2)
s <- c(r1, r2)

And likewise you could do

r1 = rast("path/rast1.tif")
r2 = rast("path/rast2.tif")
ext(r1) <- ext(r2)
s <- c(r1, r2)
2
  • On a side note, can you please suggest spatial statistics packages in R that are compatible with terra raster objects? Jul 9 at 22:12
  • 1
    That is a very broad topic. If you can be more specific about a particular type of analysis, then perhaps ask as question (not a comment). terra is new and there are not many other packages yet that extend it, but that does not mean they are not compatible; e.g. gstat is compatible (see ?terra::interpolate). Jul 9 at 22:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.