Tom Tom - 1 year ago 124
R Question

unable to fully restore some R raster objects using load workspace file

I am processing georeferenced imagery in the R raster package in Mac0S10.10.5 using R (not RStudio) 3.3.3. After saving workspace to an xxx.RData file using the GUI, I close the session restart R (without reading history file on startup) and then load the workspace again using the GUI. All objects (including the original full global raster I loaded as well as the subset I created using crop()) appear when I enter ls(). However, when I plot the rasters, only the original non-cropped image plots. The cropped subset does not plot and instead I get the following error:

>Error in file(fn, "rb") : cannot open the connection
>In addition: Warning message:
>In file(fn, "rb") :
cannot open file '/private/var/folders/8y/6924gdvx3jg6k2rz5yrzwz100000gr/T/Rtmpc0b3TY/raster/r_tm p_2017-09-25_180525_90510_17665.gri': No such file or directory

Attempting to isolate this problem, I made a 2x2 raster dataset and then made a 1x1 subset of it. Both of these raster objects get restored and are viewable using plot(). So, I am unable to reproduce the problem using these simple datasets. So the problem appears to have something to do with the original large file and my having cropped it.
Furthermore, if I recreate the crop ("newfile <- crop(oldfile, extentObject)"), I can create a raster dataset that is viewable. However, again, if I try to save this workspace and restore it later, I am back to being unable to plot it. If I perform a writeRaster(), I can successfully write the file, exit R, and then re-import this file. Therefore the problem seems to be confined to saving the entire workspace as an "xxx.RData" file.

fullraster <- raster("raster.tif")
cropraster <- crop(fullraster,extentobject)
toyraster <- raster(as.matrix(cbind(c(1,2),c(3,4))))
toyrastersub <- crop(toyraster, extent(c(0,.5,0,.5)))
plot (fullraster) # works
plot (cropraster) # works
plot (toyraster) # works
plot (toyrastersub) #works
# Perform Save workspace as...
# Exit R
#open R without reading history file
# Load Workspace File...
reloadedraster <- raster("croprasterout")
croprasteragain <- crop(fullraster,extentobject)
plot (fullraster) #works
plot (reloadedraster) #works
plot (toyraster) # works
plot (toyrastersub) #works
plot (croprasteragain) # works
plot (cropraster) # produces error in block quotes above.

I have tried reinstalling R, reloading objects, using default workspace file saves, but none of these measures fixes the problem. I have also not been able to find a solution on Stackoverflow or elsewhere on Internet.

One promising clue is that when I print out "str(cropraster)" restored but not plotable from Load Workspace it differs slightly from the one that is recreated that does work "str(croprasteragain)" I'm pasting the differing output below:

> .. .. [email protected] name : chr >"/private/var/folders/8y/6924gdvx3jg6k2rz5yrzwz100000gr/T/Rtmpc0b3TY/raster/r_t mp_2017-09-25_180525_90510_17665.grd"

> .. .. [email protected] datanotation: chr "INT2S"

> .. .. [email protected] byteorder : Named chr "little"

> .. .. .. ..- attr(*, "names")= chr "value"

> .. .. [email protected] nodatavalue : num -32768

> .. .. [email protected] name : chr "/QGISWork/Chelsa1.2/IntegerBioclim/crapdir/junk.rasterout.grd"

> .. .. [email protected] datanotation: chr "FLT8S"

> .. .. [email protected] byteorder : Named chr "little"

> .. .. .. ..- attr(*, "names")= chr "value"

> .. .. [email protected] nodatavalue : num -1.7e+308

Thanks for any help!

Tom Tom
Answer Source

I have since found a response to another question (Issue using saveRDS() with raster objects ), which helped me find a solution to my question that seeems to be the most practical I’m aware of. The answer by @RobertH to that question suggests that adding a readAll(raster) will ensure that values are saved to .RData memory. This seems to be working quite satisfactorily:

    fullraster <- raster("raster.tif")
    cropraster.readall <- readAll(crop(fullraster,cropbox2))
    #save and close R session.
    #delete raster.tif file in OS.
    Reopen and restore R session
    plot(fullraster) #doesn’t work, presumably b/c raster.tif deleted.
    plot(cropraster.readall) #WORKS!  

Using writeRaster to save files individually can also work (as kindly suggested and explained by @StatnMap). For my purposes, I found that solution somewhat less preferred because it would seem to require careful attention in R sessions to saving every necessary raster file individually.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download