What is the best way to deal with temporary directory and workspaces while processing bigger rasters?

834 Views Asked by At

As large workspaces have been occupied by the temporary folder, I indexed to the folder in the remote desktop referencing solutions from R-help and in my case using terraOptions this way:

terraOptions(tempdir = "/processing/tmp_terra/")

I cropped a raster (took 6 hours) and saved it as a file name LA_crop.tif to another folder with a different name. I loaded the saved raster in the new session:

myRaster <- rast(“/home/shared/om/data/rasters/LA_crop.tif”)
myPolygon <- vect(“/home/shared/om/data/polygons/PA.gpkg”)

# Now, while performing other operations like: 

r <- rasterize(myPolygon, myRaster, myPolygon$ID)

# I am getting this error:
Error: [rasterize] file does not exist: /tmp/RtmpiiUwq0/spat_jprtedxq260WQLq.tif

# Everytime I run the code, the name of tmp file in error part gets changed:
Error: [rasterize] file does not exist: /tmp/RtmpnyTR6l/spat_MpDvnvNP26auyqt.tif

sessionInfo()
# R version 4.0.3 (2020-10-10)
# Platform: x86_64-pc-linux-gnu (64-bit)
# Running under: Ubuntu 20.04 LTS

But, when I perform similar operations taking an example from here, it works fine.

library(terra)
# terra version 1.1-4
f <- system.file("ex/lux.shp", package="terra")
v <- vect(f)
f <- system.file("ex/elev.tif", package="terra")
r <- rast(f)

z <- rasterize(v, r, "NAME_1")    
zonal(r, z, mean, na.rm=TRUE)

#  zone      elevation
#1    0       403.1779
#2    1       283.8853
#3    2       316.1935
  1. What am I doing wrong here? Is it because I changed the temporary directory?
  2. Any further suggestions on how to deal with this problem efficiently (future reference).
1

There are 1 best solutions below

1
Robert Hijmans On

What you refer to as "file" is in fact

"/tmp/RtmpiiUwq0/spat_jprtedxq260WQLq.tif"

That file is in a temporary folder created by R. Such folders are removed when a session ends. You state however, that you saved the file elsewhere. If that is the case, you should use that file.