terra
terra copied to clipboard
Can't send 'SpatExtent' object in parallel
This is a minor issue and with an easy work around, however I still wanted to document the behaviour here so the issue is known.
Sending SpatExtent object to parallel computation fails with a unhelpful error message. I know already that SpatRaster and SpatVector needed to be wrap to allow usage on a cluster, however it seems it's also the case for SpatExtent and there is no method in wrap to do so (it may not be needed either).
RE:
library(terra)
library(parallel)
# prepare raster
f <- system.file("ex/elev.tif", package="terra")
r <- rast(f)
# create a list of extent
ls_ext <- lapply(list(r,r), ext)
# the function to be parallized
my_fc <- function(the_extent){
library(terra)
the_extent[c(1,2)]
}
# calling it normally
lapply(ls_ext, my_fc)
[[1]]
xmin xmax
5.741667 6.533333
[[2]]
xmin xmax
5.741667 6.533333
# calling it in parallel
cl <- makeCluster(2)
parLapply(cl, ls_ext, my_fc)
Error in checkForRemoteErrors(val) :
2 nodes produced errors; first error: NULL value passed as symbol address
stopCluster(cl)
It's not a major problem and it can be easily fixed by sending vector instead of SpatExtent and converting them back with ext:
# fixing the function so I do not export a S4 object for the extent
my_fc_fix <- function(the_extent){
library(terra)
ext(the_extent)[c(1,2)]
}
cl <- makeCluster(2)
parLapply(cl, lapply(ls_ext, "["), my_fc_fix)
[[1]]
xmin xmax
5.741667 6.533333
[[2]]
xmin xmax
5.741667 6.533333
stopCluster(cl)
If you think of an easy way to fix this problem it would be nice, but considering the easy work around, feel free to close this issue right away.
I could add a wrap/unwrap for SpatExtent. It seems unnecessary since as.vector(ext) and similar as you show is so easy. But perhaps it is good for completeness sake; and to make it clear that SpatExtent cannot be passed as-is.