Search code examples
rrasterterra

Parallelizing terra::crop


I'm trying to parallelize terra::crop but I get the error:

Error in { : task 1 failed - "NULL value passed as symbol address"

I know SpatRast can't be serialized but in the Github issue section I've found some solutions involving wraping the raster, or passing the filename instead of the rast object. However none of this solution works for me:

library(terra)
j <- system.file("ex/lux.shp", package="terra")
v <- vect(j)
f <- system.file("ex/elev.tif", package="terra")
r <- rast(f)
w <- wrap(r)

cl <- makeCluster(6)
registerDoParallel(cl)

#passing a wrapped SpatRast
cropped <- foreach(i=1:6,.packages = "terra")%dopar%{
  reg <- v[i]
  crp <- terra::crop(w,reg,mask=T)
  return(crp)
}

#reading the raster in the nodes
cropped <- foreach(i=1:6,.packages = "terra")%dopar%{
      r <- rast(f)
      reg <- v[i]
      crp <- terra::crop(w,reg,mask=T)
      return(crp)
    }

#and also doing all inside the loop does not works
cropped <- foreach(i=1:6,.packages = "terra")%dopar%{
  r <- rast(f)
  w <- wrap(r)
  reg <- v[i]
  crp <- terra::crop(w,reg,mask=T)
  return(crp)
}

stopCluster(cl)

in the last example I get another error:

Error in { : task 1 failed - "external pointer is not valid"

Can someone guide me through this problem?


Solution

  • library(terra)
    library(doParallel)
    j <- system.file("ex/lux.shp", package="terra")
    f <- system.file("ex/elev.tif", package="terra")
    
    cl <- makeCluster(6)
    registerDoParallel(cl)
    
    cropped <- foreach(i=1:6,
                       .packages = "terra") %dopar% {
      reg <-  vect(j)[i]
      crp <- wrap(terra::crop(rast(f),
                              reg,
                              mask=T))
      return(crp)
    }
    
    lapply(cropped,unwrap)