Make raster stack with different extent

10,023

I suppose that the extent of two raster are differet though the raster masked by crop function.You should check the both of awc and bio.au extent base on same reolution, rows and columns. Because I couldn't download data from hyperlink, I give an example of my own data.

r <- raster('/big_raster')
r1 <- raster('/small_raster')
r2 <- crop(r,extent(r1))

r1
class       : RasterLayer 
dimensions  : 74, 157, 11618  (nrow, ncol, ncell)
resolution  : 0.0833333, 0.0833333  (x, y)
extent      : 89.2185, 102.3018, 30.96238, 37.12905  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0 
data source : D:\D\temp\Rtest\modis8km.tif 
names       : modis8km 
values      : -32768, 32767  (min, max)

r2
class       : RasterLayer 
dimensions  : 74, 157, 11618  (nrow, ncol, ncell)
resolution  : 0.08333333, 0.08333333  (x, y)
extent      : 89.25, 102.3333, 31, 37.16667  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0 
data source : in memory
names       : g201401a 
values      : -32768, 7789  (min, max)

Though r1 and r1 with same resolution and dimension, the extent have tiny offset. It cause stack error.

 stack(r1,r2)
 Error in compareRaster(x) : different extent

So, you should rebuid the r2 to match r1:

r22 <- raster(vals=values(r2),ext=extent(r1),crs=crs(r1),
                  nrows=dim(r1)[1],ncols=dim(r1)[2])

Now stack(r22,r1) will be successful.

Share:
10,023
Tiny_hopper
Author by

Tiny_hopper

I am an PhD student at the University of Melbourne. In my research I use R, ArcGIS, and Python.

Updated on June 17, 2022

Comments

  • Tiny_hopper
    Tiny_hopper almost 2 years

    I am in trouble making raster stack which have slightly different extent. The answer (1st one) given here is useful but did not help in my case. For example, I want to make a raster stack using bio2 raster for Australia and this Australian raster. The second raster comes for Australia only and the first one is global. So I cropped the global bio2 raster to the same extent of Australian raster using crop() function, but the resultant raster extent (i.e., bio2.au) is slightly different (therefore, I cannot make raster using the cropped raster and the Australian raster, awc). Sample code is below:

    library(raster)
    awc <- raster("path to Australian raster")
    bio2.g <- raster("path to Bio2 global raster")
    # crop bio2.g to the same extent of awc
    bio2.au <- crop(bio2.g, extent(awc))
    
    # make a raster stack
    st <- stack(awc, bio2.au)
    Error in compareRaster(x) : different extent
    

    I have also tried using quick=TRUE within the stack() function. But in this case the cell values in awc is lost. Note: the size of awc raster is 4gb.

    # first make a list of rasters saved in the computer
    li <- list.files("path to file", pattern = ".tif$", full.names = TRUE)
    st <- stack(li, quick=TRUE)
    st[[1]] # no cell values for awc
    

    Your suggestions will be highly appreciated. My ultimate goal is to crop several bioclim rasters to the same extent of Australian raster awc and stack them together so that raster cell values are not lost.

    Edit (after comment of @Cobin):

    Below is the attribute of each raster

    # global raster (bigger raster)
    > r
    class       : RasterLayer 
    dimensions  : 21600, 43200, 933120000  (nrow, ncol, ncell)
    resolution  : 0.008333333, 0.008333333  (x, y)
    extent      : -180, 180, -90, 90  (xmin, xmax, ymin, ymax)
    coord. ref. : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0 
    data source : D:\Worldclim2_Bioclim\wc2.0_bio_30s_02.tif 
    names       : wc2.0_bio_30s_02 
    values      : 0, 37.06667  (min, max)
    
    
    # Australian raster (smaller raster)
    > r1
    class       : RasterLayer 
    dimensions  : 43201, 49359, 2132358159  (nrow, ncol, ncell)
    resolution  : 0.0008333333, 0.0008333333  (x, y)
    extent      : 112.8921, 154.0246, -44.00042, -7.999583  (xmin, xmax, ymin, ymax)
    coord. ref. : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0 
    data source : D:\SoilAWC5cm.EV1.tif 
    names       : SoilAWC5cm.EV1 
    values      : 2.997789, 27.86114  (min, max)
    
    # new raster, after crop() function is applied
    > r2 <- crop(r,extent(r1))
    > r2
    class       : RasterLayer 
    dimensions  : 4320, 4936, 21323520  (nrow, ncol, ncell)
    resolution  : 0.008333333, 0.008333333  (x, y)
    extent      : 112.8917, 154.025, -44, -8  (xmin, xmax, ymin, ymax)
    coord. ref. : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0 
    data source : C:\Users\Anwar\AppData\Local\Temp\Rtmpmg9fyF\raster\r_tmp_2018-11-23_164300_11308_65747.grd 
    names       : wc2.0_bio_30s_02 
    values      : 1.933333, 18.15833  (min, max)
    
    # rebuild r2 to match r1
    > r22 <- raster(vals=values(r2),ext=extent(r1), nrows=dim(r1)[1],ncols=dim(r1)[2])
    
    Error in setValues(r, vals) : 
      length(values) is not equal to ncell(x), or to 1
    
  • Tiny_hopper
    Tiny_hopper over 5 years
    Thank you. My r1 extent is: 112.8921, 154.0246, -44.00042, -7.999583, and my r2 extent after cropping is: 112.8917, 154.025, -44, -8. But I have got the following error: r22 <- raster(vals=values(r2),ext=extent(r1), nrows=dim(r1)[1], ncols=dim(r1)[2]). Error in setValues(r, vals) : length(values) is not equal to ncell(x), or to 1 @Cobin
  • Cobin
    Cobin over 5 years
    The main problem is that the small rasster is not snapping big raster.The rasters have different rows and collumns (width and height) after crop.You should show bio2.au and awc atrribute in question. Maybe cellFrom function to crop raster is preciser. @Crayfish
  • Tiny_hopper
    Tiny_hopper over 5 years
    Thank you. I have now added raster attributes in my main question. @Cobin