r-spatialecology/landscapemetrics

window_lsm for large raster files

Closed this issue · 4 comments

Hi all,
I have a large binary raster that I am trying to calculate some landscape metrics (lsm_l_area_mn, more specifically) using moving windows (window_lsm). The issue I am facing is that the raster might be too large and window_lsm is not only taking forever to run but also spitting the following error later:

Error in .focal_fun(as.vector(t(v)), w, paddim, runfun, NAonly) : 
  Evaluation error: object 'f' not found.
invalid option "error"

Here is some details of my raster:

class      : RasterLayer 
dimensions : 24674, 13797, 340427178  (nrow, ncol, ncell)
resolution : 50, 50  (x, y)
extent     : 171030.3, 860880.3, 5425628, 6659328  (xmin, xmax, ymin, ymax)
crs        : +proj=tmerc +lat_0=0 +lon_0=-115 +k=0.9992 +x_0=500000 +y_0=0 +datum=NAD83 +units=m +no_defs 
source     : mosaic.tif 
names      : mosaic 
values     : 1, 1  (min, max)

Does anyone recognize the above error? Also, is there a different or better approach to deal with large raster files that could speed the processing time?

The raster was initially a 0/1 raster, but then I realized that window_lsm will calculate the chosen metric for all classes (including the 0 class), so I reclassified the raster to have only one class (1), while leaving the other cells as NA.

Here is an example of the approach I am taking.

library(landscapemetrics)
library(landscapetools)

#converting to binary (1/NA)
example_landscape<-landscape %>% reclassify(rcl = c(-1,1,NA,
                                                    1,2,1,
                                                    2,3,1))

window_w2w<-window_lsm(example_landscape,matrix(1,5,5), what = "lsm_l_area_mn" ) 

The example does not produce any errors and is slightly different from what I am doing, the only difference being the size of the window (21x21 in my case)

Hm, your raster doesn't seem to be that big... Can you maybe share the tif file so we can try to reproduce the issue?

For sure, thanks for your willingness to help. The raster can be accessed here

I also tried to GitHub version and it gives the same error

OK, I tried to run this, and you were right, the raster is rather big for window_lsm and it takes ages.

The plan that should kinda solve this is now to re-implement window_lsm with internals from terra (#245) so that it can hopefully be run parallelized in future (rspatial/terra#36).