raster multicore processing: WriteValues inside foreach loop

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

raster multicore processing: WriteValues inside foreach loop

Hugo Costa
Dear list

with package raster it's possible to process large images in blocks, like
this:

library(raster)

r<- raster(system.file("img", "Rlogo.tiff", package="tiff"))
s <- raster(r)
b <- blockSize(r)
s <- writeStart(s, filename=tempfile(fileext = ".tif"),  overwrite=TRUE)

for (i in 1:b$n){
  v <- getValuesBlock(r, row=b$row[i], nrows=b$nrows[i])
  s <- writeValues(s, v, b$row[i])
}

s <- writeStop(s)
plot(s)

However, I wonder if it's possible to replace the for loop by foreach, like
this:

library(foreach)
cl <- parallel::makeCluster(2)
doSNOW::registerDoSNOW(cl)

r<- raster(system.file("img", "Rlogo.tiff", package="tiff"))
s <- raster(r)
b <- blockSize(r)
s <- writeStart(s, filename=tempfile(fileext = ".tif"),  overwrite=TRUE)

foreach (i=1:b$n, .packages = "raster") %dopar% {
  v <- getValuesBlock(r, row=b$row[i], nrows=b$nrows[i])
  s <- writeValues(s, v, b$row[i])
}
parallel::stopCluster(cl)

s <- writeStop(s)

However, the code above fails with
Error in { : task 1 failed - "Null external pointer

Apparently, the WriteValues inside foreach is not able to write in the tif
file. Is it possible to fix this?
Could foreach be an easier alternative to the multi-core functions
exemplified in the raster vignette here
<https://rspatial.org/raster/pkg/appendix1.html>?

Thanks
Hugo

        [[alternative HTML version deleted]]

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo