2023-01-13

raster::gridDistance() fails on medium to large raster files

I have some moderate sized raster files (max size ~190 MB) that I would like to calculate grid distances for using raster::gridDistance()

I'm finding that at best, the operation is very slow and at worst, R just crashes for the largest of my files. Please note: I'm not too interested in memory management advice (e.g. maxing out memory.limit(), breaking into smaller rasters or pursuing parallel processing methods) as these are sidestepping my real issue. If grid distances really should not be attempted for 190+ MB size files, then I will just break the job into smaller chunks.

The raster::gridDistance() documentation mentions that the function is "currently only implemented as a 'queen' case in which cells have 8 neighbors". Also from the documentation, I understand that I can try to solve "errors in the case of complex objects spread over different chunks... by varying the chunk size, see function setOptions()." and that "Additional distance measures and options (directions, cost-distance) are available in the 'gdistance' package", but I have been hesitant to pursue these without really understanding their limitations/considerations.

Thanks to this question R - terra::distance() equivalent of raster::gridDistance(..., origin = x, omit = y) I understand that there is an alternative method using terra::gridDistance(), but I am not able to discern if the operation is any more efficient or suitable for my needs than raster::gridDistance()

I haven't posted a reprex or session info as my question is really as follows:

  1. Is terra::gridDistance() (or some other alternative like those offered by {gdistance}) really a more efficient (faster) or customizable way for calculating a grid distance using moderate-large raster files?
  2. If not, what are considerations for changing how the grid distance is calculated (varying chunk size or other means) using raster::gridDistance() and setOptions()?

Apologies if this question doesn't quite meet guidelines - I would have just posted it as a comment on the linked question above, but my reputation isn't high enough. If there is enough interest, I can reformat my question so that it better fits guidelines with a reprex etc. Also, I am posting the question here rather than Geographic Information Systems because the original linked question was posted here.



No comments:

Post a Comment