Search code examples
r-rasterextent

Stacking 20 rasters with different extents in R


I have 20 rasters stored in a list called allrasters. They have different extents (see below). I want to stack them so first I use lapply to crop all list items to the raster with the smallest extent - the 10th list item.

library(raster)
rastlist <- list.files(path = "C:/Users/bhauptman/Box/Ch2/R/data/tif_shp", pattern='.tif$', all.files=TRUE, full.names=T)
allrasters <- lapply(rastlist, raster)
crop_allrasters <-lapply(allrasters, FUN = crop, allrasters[[10]])

It runs but when I check to see if they all now have the same extent, no:

compareRaster(crop_allrasters)
>Error in compareRaster(crop_allrasters) : different extent

Here are the original extents of the 20 rasters:

    > extent(allrasters[[1]])
class      : Extent 
xmin       : -223287.4 
xmax       : 122512.6 
ymin       : -340387.9 
ymax       : 298012.1 
> extent(allrasters[[2]])
class      : Extent 
xmin       : -459600.8 
xmax       : 335249.2 
ymin       : -503894.5 
ymax       : 449855.5 
> extent(allrasters[[3]])
class      : Extent 
xmin       : -459600.8 
xmax       : 335249.2 
ymin       : -503894.5 
ymax       : 449855.5 
> extent(allrasters[[4]])
class      : Extent 
xmin       : -260723.4 
xmax       : 150713.8 
ymin       : -350863.2 
ymax       : 305266.8 
> extent(allrasters[[5]])
class      : Extent 
xmin       : -260740.3 
xmax       : 150709.6 
ymin       : -350843.9 
ymax       : 305286.1 
> extent(allrasters[[6]])
class      : Extent 
xmin       : -223300 
xmax       : 129000 
ymin       : -344600 
ymax       : 298550 
> extent(allrasters[[7]])
class      : Extent 
xmin       : -223300 
xmax       : 129000 
ymin       : -344600 
ymax       : 298550 
> extent(allrasters[[8]])
class      : Extent 
xmin       : -223300 
xmax       : 129000 
ymin       : -344600 
ymax       : 298550 
> extent(allrasters[[9]])
class      : Extent 
xmin       : -223300 
xmax       : 129000 
ymin       : -344600 
ymax       : 298550 
> extent(allrasters[[10]])
class      : Extent 
xmin       : -223300 
xmax       : 129000 
ymin       : -344600 
ymax       : 298550 
> extent(allrasters[[11]])
class      : Extent 
xmin       : -223300 
xmax       : 129000 
ymin       : -344600 
ymax       : 298550 
> extent(allrasters[[12]])
class      : Extent 
xmin       : -223300 
xmax       : 129000 
ymin       : -344600 
ymax       : 298550 
> extent(allrasters[[13]])
class      : Extent 
xmin       : -223300 
xmax       : 129000 
ymin       : -344600 
ymax       : 298550 
> extent(allrasters[[14]])
class      : Extent 
xmin       : -223300 
xmax       : 129000 
ymin       : -344600 
ymax       : 298550 
> extent(allrasters[[15]])
class      : Extent 
xmin       : -421253.8 
xmax       : 293346.2 
ymin       : -463338.6 
ymax       : 411561.4 
> extent(allrasters[[16]])
class      : Extent 
xmin       : -421253.8 
xmax       : 293346.2 
ymin       : -463338.6 
ymax       : 411561.4 
> extent(allrasters[[17]])
class      : Extent 
xmin       : -260776 
xmax       : 150724 
ymin       : -350868.1 
ymax       : 305331.9 
> extent(allrasters[[18]])
class      : Extent 
xmin       : -260776 
xmax       : 150724 
ymin       : -350868.1 
ymax       : 305331.9 
> extent(allrasters[[19]])
class      : Extent 
xmin       : -260776 
xmax       : 150724 
ymin       : -350868.1 
ymax       : 305331.9 
> extent(allrasters[[20]])
class      : Extent 
xmin       : -260776 
xmax       : 150724 
ymin       : -350868.1 
ymax       : 305331.9 

Solution

  • In principle that should work, illustrated here with some example data and with terra instead of raster

    library(terra)
    f <- system.file("ex/elev.tif", package="terra")
    x = list(r, r, crop(r, c(5,6,49,50)))
    y = lapply(x, crop, y=x[[3]])
    
    sapply(x, \(i) as.vector(ext(i))) |> t()
    #         xmin     xmax     ymin     ymax
    #[1,] 5.741667 6.533333 49.44167 50.19167
    #[2,] 5.741667 6.533333 49.44167 50.19167
    #[3,] 5.741667 6.000000 49.44167 50.00000
    
    sapply(y, \(i) as.vector(ext(i))) |> t()
    #         xmin xmax     ymin ymax
    #[1,] 5.741667    6 49.44167   50
    #[2,] 5.741667    6 49.44167   50
    #[3,] 5.741667    6 49.44167   50
     
    

    It seems that this does not work for some of the input rasters in your case. You could try to use crop directly on those rasters to find out why.