I'm finding errors using the function fasterRastDistance()
in the fasterRaster
R library when running the same script in several simultaneous RStudio instances.
The error looks like this:
ERROR: You must select the PERMANENT mapset before updating the current
location's projection (current mapset is <<UNKNOWN>>)
access: Invalid argument
ERROR: LOCATION <C:/Users/JavierF/AppData/Local/Temp/RtmpqoyKjm\<UNKNOWN>>
not available
Error in doGRASS(cmd, flags = flags, ..., parameters = parameters, echoCmd = echoCmd, :
Parameter <input> has multiple values
The script runs smoothly if I use just one RStudio execution, but appears at some point of a loop iteration if I use several simultaneous RStudio sessions. I want to use several RStudio instances because that allows me to process more files simultaneously; I am aware that code parallelization might work better than several RStudio sessions for that matter, but I cannot use that for unrelated reasons (the script also uses the terra
package that does not work well with parallelization).
I don't have much experience with GRASS or rgrass7
, which underlies fasterRaster
, but I believe that the error indicates that some temporary files created within GRASS environments share name across RStudio instances. My interpretation is that, at some point, these tmp files are deleted when no longer needed by one of the R instances, and then the file is missed by the other instance.
If that is correct, my approach to overcoming this problem would be to explicitly choose a unique tmp file name each time, but I don't have enough rgrass7
/GRASS knowledge about how to go about that.
A simplified version of the script is:
grassDir <- 'C:/Program Files/GRASS GIS 7.8'
rasters_directories <- list.files(..., full.names = T) # these are rasterized lines, with value 1 on lines and NA on pixels not overlapping lines
for (r in rasters_directories){
line_raster <- rast(r) # (I use rast() from terra because the rest of the script runs faster like that)
dist_raster <- fasterRastDistance(rast = raster(line_raster),
metric = "geodesic",
grassDir = grassDir)
writeRaster(dist_raster, paste0(".../", r, ".tif)) # (well, a modification of this to pick the name, not the full directory...)
}
How do I solve the issue?
fasterRastDistance
function is simply callingrgrass7::execGRASS("r.grow.distance")
.fasterRaster
has been updated to GRASS 8.3+, but do consider raising these questions on the package's GitHub issues page. Issues pages are usually the most direct way to get into contact with the package authors (me, in this case).