[R-sig-Geo] raster multicore processing: WriteValues inside foreach loop
Hugo Costa
hugo@gco@t@ @end|ng |rom gm@||@com
Sun Sep 20 12:44:38 CEST 2020
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]]
More information about the R-sig-Geo
mailing list