4

I have three raster files with the same extent and CRS, but with different origin, dimensions and resolution.

r1
class       : RasterLayer 
dimensions  : 3020, 3043, 9189860  (nrow, ncol, ncell)
resolution  : 0.003446425, 0.003657477  (x, y)
extent      : -8.69165, 1.79582, 49.83774, 60.88332  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0 
names       : r1
values      : -0.272593, 5616  (min, max)

r2
class       : RasterLayer 
dimensions  : 1221, 655, 799755  (nrow, ncol, ncell)
resolution  : 0.0160114, 0.009046339  (x, y)
extent      : -8.69165, 1.79582, 49.83774, 60.88332  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0 
names       : r2
values      : 0, 10  (min, max)

r3
class       : RasterLayer 
dimensions  : 1326, 1259, 1669434  (nrow, ncol, ncell)
resolution  : 0.00833, 0.00833  (x, y)
extent      : -8.69165, 1.79582, 49.83774, 60.88332  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0 
names       : r3
values      : -0.4956901, 8.6887  (min, max)

When I try to create a raster stack, I get the following error:

predictors<-stack(r1, r2, r3)
Error in compareRaster(x) : different number or columns

I tried to re-sample r1,r2 according to one raster, lets say r3 - but it did not work.

Any advice how to solve this issue?

The rasters can be downloaded here: https://www.dropbox.com/sh/zfbm05qwmsgp0lh/AAC7xGxSUuEyYeIQbqSqh1FPa?dl=0

3

You can use projectRaster() to resample to a new resolution (also extent and CRS):

r2resampled <- projectRaster(r2,r1,method = 'ngb')
r3resampled <- projectRaster(r3,r1,method = 'bilinear')

The first one is categorical, so it's necessary to use nearest neighbor as method (ngb). The second one is numeric, so you can use bilinear (bilinear) or nearest neighbor (some researchers prefer to use ngb as method because with bilinear you are changing the original information to the new resolution, especially in RS products).

Check the result:

plot(stack(r1,r2resampled,r3resampled))

enter image description here

2

For unifying dimensions and resolutions of rasters, resample() function from Raster package works well for me:

library(raster)

r1 <- raster("Animal.tif")
r2 <- raster("LandTrans.tif")
r3 <- raster("TempJan.tif")

r1 <- resample(r1,r3)
r2 <- resample(r2,r3,method='ngb')

rs <- stack(r1,r2,r3)

plot(rs)

enter image description here

0

Note that it is not necessary to create new dataset. R is able to work with vrt files thanks to a wrapper, so you can use virtual raster files as input. Just make shure that you set the separate option to true. You can also select the resampling method using the r parameter.

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.