8

I have two rasters of different resolution and extent:

> res(Elevation)
[1] 0.002083333 0.002083333

> res(Ann_precip)
[1] 0.008333333 0.008333333 

> extent(Elevation)
class       : Extent 
xmin        : -15.07722 
xmax        : -7.641806 
ymin        : 7.193611 
ymax        : 12.67694 

> extent(Ann_precip)
class       : Extent 
xmin        : -15.075 
xmax        : -7.641667 
ymin        : 7.191667 
ymax        : 12.675

My question is, in order for these two rasters to have matching resolutions and extents, is it better to:

A) use the raster::aggregate function

> 0.008333333/0.002083333
[1] 4

Elevation_res<-aggregate(Elevation, fact=4, fun=mean)

and the raster::extend function

Elevation_res<-extend(Elevation_res, Ann_precip, values=NA)

(although here I still get different but very similar extents and resolutions):

> res(Elevation_res)
[1] 0.008333333 0.008333333

> res(Ann_precip)
[1] 0.008333333 0.008333333

> res(Elevation_res)==res(Ann_precip)
[1] FALSE FALSE

> extent(Elevation_res)
class       : Extent 
xmin        : -15.07722 
xmax        : -7.635556 
ymin        : 7.193611 
ymax        : 12.67694 

> extent(Ann_precip)
class       : Extent 
xmin        : -15.075 
xmax        : -7.641667 
ymin        : 7.191667 
ymax        : 12.675 

or

b) use the raster::resample function

Elevation_res<-resample(Elevation, Ann_precip, method="bilinear")

> res(Elevation_res)==res(Ann_precip)
[1] TRUE TRUE

> extent(Elevation_res)==extent(Ann_precip)
[1] TRUE

I'm asking this because I've read in Wegmann et al (2016) (p110) (if I understand correctly) that resampling greatly affects pixel values, and that aggregate(),extend() and crop() should be used instead. Since differences in resolution and extent are quite small in my case, can I assume that bias created by resampling would be minimal here?

9

Check resample function of raster package. When resample is used with 'bilinear method, the output is the same one than aggregate:

if (!skipaggregate) {
    rres <- res(y) / res(x)
    resdif <- max(rres)
    if (resdif > 2) {
        ag <- pmax(1, floor(rres-1))
        if (max(ag) > 1) {
            if (method == 'bilinear') {
                x <- aggregate(x, ag, 'mean')
            } else {  
                x <- aggregate(x, ag, modal)
            }
        }
    }

With an example:

library(raster)

r <- raster(nrow=4,ncol=8)

r2 <- raster(nrow=2,ncol=4)

r <- setValues(r,values = 1:32)

r_agg <- aggregate(r,fact=2,fun=mean)

r_resam <- resample(r,r2,method='bilinear')

values(r_resam) == values(r_agg)
## [1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE

values(r_resam)
## [1]  5.5  7.5  9.5 11.5 21.5 23.5 25.5 27.5

But if you use 'ngb' as method, the result is different (method depends of your data, if is categorical you must use 'ngb'):

r_resam2 <- resample(r,r2,method='ngb')

values(r_resam2)
## [1] 10 12 14 16 26 28 30 32

And extend doesn't change resolution, only extent:

r
## class       : RasterLayer 
## dimensions  : 4, 8, 32  (nrow, ncol, ncell)
## resolution  : 45, 45  (x, y)
## extent      : -180, 180, -90, 90  (xmin, xmax, ymin, ymax)
## coord. ref. : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0 

r_ext <- extend(r,r2,values=NA)

r_ext
## class       : RasterLayer 
## dimensions  : 4, 8, 32  (nrow, ncol, ncell)
## resolution  : 45, 45  (x, y)
## extent      : -180, 180, -90, 90  (xmin, xmax, ymin, ymax)
## coord. ref. : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0

And crop() as extend(), will no change resolution.

  • @MarieL sorry, is extent (I misspelled the word) – aldo_tapia Sep 13 '17 at 0:04
  • Is the bilinear option equivalent to mean as the function for aggregate and ngb option equivalent to modal? I am referring to cases where the target is coarser resolution (larger pixel size) than the input that needs to be transformed. – user3386170 Mar 29 '18 at 20:05
  • 1
    @user3386170 yes, check these lines: github.com/cran/raster/blob/… – aldo_tapia Mar 29 '18 at 20:08

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.