Search code examples
rgeospatialrasterresolution

How to change resolution of 2 rasters layers in R?


I am working with two rasters stacks: bioclim (climate data) and soil data. They have different resolution. The description of data:

bioclim
#class      : RasterStack 
#dimensions : 163, 319, 51997, 19  (nrow, ncol, ncell, nlayers)
#resolution : 0.1666667, 0.1666667  (x, y)
#extent     : 18.83337, 72.00005, 40.99999, 68.16666  (xmin, xmax, ymin, ymax)
#crs        : NA 

soil
#class      : RasterStack 
#dimensions : 1256, 2213, 2779528, 5  (nrow, ncol, ncell, nlayers)
#resolution : 0.02259376, 0.02259376  (x, y)
#extent     : 20, 69.99999, 42.62224, 71  (xmin, xmax, ymin, ymax)
#crs        : NA 

I tried function:

soil <- aggregate(soil, fact=7.376669487504514)

I just put this number (fact), because it equals to resolution of bioclim divided by resolution of soil.

But, they have little differences:

res(soil)
#[1] 0.1581563 0.1581563

res(bioclim)
#[1] 0.1666667 0.1666667

Also I tried other functions like aggregate, but not successful.

I need to create main stack of two stacks: bioclim and soil. Please, could anybody help me?

env <- stack(bioclim,soil)

Solution

  • Here is a self-contained, minimal, reproducible example:

    library(raster)
    bioclim <- raster(nrow=163, ncol=319, ext=extent(18.83337, 72.00005, 40.99999, 68.16666))    
    soil <- raster(nrow=1256, ncol=2213, ext=extent(20, 69.99999, 42.62224, 71))
    values(soil) = 1:ncell(soil)
    

    Solution: if you cannot use (dis-)aggregate, you can use resample

    sb <- resample(soil, bioclim)
    sb
    #class      : RasterLayer 
    #dimensions : 163, 319, 51997  (nrow, ncol, ncell)
    #resolution : 0.1666667, 0.1666667  (x, y)
    #extent     : 18.83337, 72.00005, 40.99999, 68.16666  (xmin, xmax, ymin, ymax)
    #crs        : +proj=longlat +datum=WGS84 +no_defs 
    #source     : memory
    #names      : layer 
    #values     : 284578.3, 2779484  (min, max)
    

    Or use terra, if you need better performance:

    library(terra)
    bc <- rast(nrow=163, ncol=319, ext=ext(18.83337, 72.00005, 40.99999, 68.16666))
    so <- rast(nrow=1256, ncol=2213, ext=ext(20, 69.99999, 42.62224, 71))
    values(so) = 1:ncell(so)
      
    sb <- resample(so, bc)