0

I am trying to calculate the spatial correlation between two rasters. I have two large rasters with the same extent, resolution, etc

class      : RasterLayer
dimensions : 45598, 53241, 2427683118  (nrow, ncol, ncell)
resolution : 30, 30  (x, y)
extent     : 273366.8, 1870597, 367780.7, 1735721  (xmin, xmax, ymin, ymax)```

These layers have massive NAs cells I tried to use terra::focalCor with the stack of those layers.

corr=focalCor(layerstack, w=9, cor)

But I have this issue

Error in v[[j - 1]] <- t(sapply(1:nrow(Y), function(i, ...) fun(X[i, ],  :
  more elements supplied than there are to replace

Any ideas or suggestions? Cheers

DarthOrion
  • 15
  • 4

1 Answers1

1

It would have been easier to provide a specific answer with actual data provided to be able to reproduce your issue, but in this case it seems like you imported your gridded data using raster::raster() creating a RasterLayer object, but according to ?focalCor, x has clearly to be a SpatRaster with at least two layers.

So, try terra::rast(c("grid_1.tif", "grid_2.tif")) |> terra::focalCor(w = 9, cor) instead.

Edit:

Thanks for your reprex. I dared to reduce dimensions and modify the extent a little bit in order to reduce processing time:

library(terra)

r <- rast(ncols = 100, nrows = 100, 
          xmin = 0, xmax = 25, ymin = 0, ymax = 25, 
          crs = "epsg:4326") 

r1 <- init(r, fun = runif) 
r2 <- init(r, fun = runif) 

r_stack <- c(r1, r2) 

r_stack_cor_5 <- focalCor(r_stack, w = 5, cor) 
r_stack_cor_5
#> class       : SpatRaster 
#> dimensions  : 100, 100, 1  (nrow, ncol, nlyr)
#> resolution  : 0.25, 0.25  (x, y)
#> extent      : 0, 25, 0, 25  (xmin, xmax, ymin, ymax)
#> coord. ref. : lon/lat WGS 84 (EPSG:4326) 
#> source      : memory 
#> name        :       lyr1 
#> min value   : -0.6476946 
#> max value   :  0.6948594

r_stack_cor_25 <- focalCor(r_stack, w = 25, cor)
r_stack_cor_25
#> class       : SpatRaster 
#> dimensions  : 100, 100, 1  (nrow, ncol, nlyr)
#> resolution  : 0.25, 0.25  (x, y)
#> extent      : 0, 25, 0, 25  (xmin, xmax, ymin, ymax)
#> coord. ref. : lon/lat WGS 84 (EPSG:4326) 
#> source      : memory 
#> name        :       lyr1 
#> min value   : -0.1020998 
#> max value   :  0.1045798

I used fun = cor instead of function(x, y) cor(x, y) but the result is the same according to all.equal(). However, your example seems to work - and I'm failing to recognize the issue at the moment.

dimfalk
  • 853
  • 1
  • 5
  • 15
  • Thanks for your reply. This is a reproducible example of my question. I am having the same issue as the previous post 'r <- rast(ncols=3453, nrows=1757, xmin=1168177, xmax=1271767, ymin=964181, ymax=1016891, crs = "epsg:3005") r1 <- init(r, fun=runif) r2 <- init(r, fun=runif) r_stack <- c(r1, r2) r_stack_cor <- focalCor(r_stack, w=5, function(x, y) cor(x, y)) r_stack_cor_25 <- focalCor(r_stack, w=25,function(x, y) cor(x, y))' – DarthOrion Aug 03 '22 at 23:15
  • @DarthOrion: By previous post you mean your initial question? I updated my reply with your example provided - and (somehow) I don't see any issues here. – dimfalk Aug 04 '22 at 01:50
  • Thanks @falk-env. You are right, with small rasters the focalCorr works well. The issue will occur as long you are using a moderate-size raster. Would you mind trying with my example, including the same dimensions? – DarthOrion Aug 04 '22 at 19:40
  • I was about to write there is already an issue [rspatial/terra#607](https://github.com/rspatial/terra/issues/607) adressing exactly this behaviour but then I noticed this is where you got the reprex from, so you'll either be aware of that or this is you... However, with your dim and res the attempt is also failing with `more elements supplied than there are to replace`. If I were you, I'd try to find out the critical number of grid cells when `focalCor` with `w = 5` and `w = 25` is failing considering your RAM - and then split the original raster into `n` overlapping tiles for processing. – dimfalk Aug 04 '22 at 20:56
  • Thanks! Yes, you are right. A colleague posted that issue on terra. I'll try your advice. Thank you so much – DarthOrion Aug 05 '22 at 00:02