With a raster and a reference raster, I'd like to do something like this:

# resample raster to ref_raster to match resolution/origin using max as aggregate
raster = resample(raster, ref_raster, 'max')

How can I optimally achieve this?

The raster::resample function offers two different options to resample: ngb (Nearest Neighbor) and bilinear (basically a mean, as I understand). From this question I gathered that resample basically calls aggregate, as can be seen here:

if (method == 'bilinear') {
    x <- aggregate(x, ag, 'mean')
} else {  
    x <- aggregate(x, ag, modal)

I haven't found an option to use other functions for resampling (functions like aggregate would allow, e.g. max, min or any function transforming multiple values to one and accepting a na.rm argument).

I also haven't found an option to make aggregate match the origin, as resample would do. Also if using aggregate, I'll first have to calculate the factor myself, which isn't a huge issue, but doesn't make for short code.

Is the "right" approach then to first aggregate (to match the resolution) and then resample (to match the origin)? Or is there an easier (and less computationally expensive) option?

  • Have you tried creating a custom function using resample code and changing the function line?
    – aldo_tapia
    Commented Dec 15, 2021 at 23:17
  • no - to be honest, not understanding parts of the code of the resample function made me reluctant to adopt it. It uses several functions/constructs that I'm not familiar with - starting with the signature setMethod('resample', signature(x='Raster', y='Raster'), function(x, y, method="bilinear", filename="", ...), what does setMethod do here? You think simply changing x <- aggregate(x, ag, 'mean') to x <- aggregate(x, ag, 'max') in line 43 is sufficient? (call with 'bilinear' for max)
    – Honeybear
    Commented Dec 16, 2021 at 6:51
  • 1
    You can use the terra package to resample a raster using the max as a function. This might help you (rdrr.io/github/rspatial/terra/man/resample.html)
    – Nikos
    Commented Aug 17, 2023 at 15:48
  • perfect, that's what I'm looking for. If you make it into an answer, I'll accept it!
    – Honeybear
    Commented Sep 7, 2023 at 9:40


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.