Reputation: 31
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.
library(raster)
library(sp)
library(rgdal)
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
writeRaster(cropraster,"croprasterout")
# Perform Save workspace as...
# Exit R
#open R without reading history file
# Load Workspace File...
library(raster)
library(sp)
library(rgdal)
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:
str(cropraster)
> .. .. ..@ name : chr >"/private/var/folders/8y/6924gdvx3jg6k2rz5yrzwz100000gr/T/Rtmpc0b3TY/raster/r_t mp_2017-09-25_180525_90510_17665.grd"
> .. .. ..@ datanotation: chr "INT2S"
> .. .. ..@ byteorder : Named chr "little"
> .. .. .. ..- attr(*, "names")= chr "value"
> .. .. ..@ nodatavalue : num -32768
str(croprasteragain)
> .. .. ..@ name : chr "/QGISWork/Chelsa1.2/IntegerBioclim/crapdir/junk.rasterout.grd"
> .. .. ..@ datanotation: chr "FLT8S"
> .. .. ..@ byteorder : Named chr "little"
> .. .. .. ..- attr(*, "names")= chr "value"
> .. .. ..@ nodatavalue : num -1.7e+308
Thanks for any help!
Upvotes: 1
Views: 915
Reputation: 31
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.
Upvotes: 2
Reputation: 6671
When you create a raster with library(raster)
, there is a good chance that data contained in the raster are saved in a temporary folder (or in the RAM if small enough). But data are not saved in the R-session. The only information saved in the R-session is the metadata and the location of data in RAM or on disk.
Thus, when you save and restore your session, the only way to get your raster data is to be sure the raster is at the same place than where it was before closing session. If you do not save yourself the raster and its data in a known place, you will lose it for next session.
This is why in your tests for your second session :
Hence, if you want to be able to re-use previous raster object, you need to use writeRaster
and read it, like you did with your reloadedraster
. Try to use a file extension when saving your raster: .grd
is the default one for library(raster)
.
Upvotes: 2