Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to save raster data in R object format?

Tags:

r

save

raster

I don't know how to deal with save.image()and saveRDS()with raster data in R. I have understood that raster package open a connexion with the image file using raster() function, so it doesn't really open the file into R workspace.

I want to save my workspace (data.frame, list, raster, etc) with save.image() function (or similar) and open it in a different computer. If I try to plot or process a raster object saved in a different computer, always have the same issue:

Error in .local(.Object, ...) : 
  `C:\path\to\file.tif' does not exist in the file system,
and is not recognised as a supported dataset name.

Is there a way to save a raster object (opened as external file) in R format? I don't mean raster format as tiff nor grid and others.

like image 634
aldo_tapia Avatar asked Mar 10 '23 17:03

aldo_tapia


2 Answers

At your own risk, you can use the readAll function to load the raster into memory before saving. e.g.

r <- raster(system.file("external/test.grd", package="raster"))
r <- readAll(r) # force data into memory
save(r, file = 'r.RData')

It can be loaded on a different machine as mentioned

load('r.Rdata`)

Beware, this will be problematic for very large rasters on memory limited systems

like image 145
user3357177 Avatar answered Mar 13 '23 13:03

user3357177


You can save rasters, like other R objects, using the save command.

save(r,file="r.Rdata")

On a different computer, you can load that file using

load("r.Rdata")

which will bring back the raster r in your workspace.

I have tried this across Windows and Linux and it never gives problems

like image 36
Peter Herman Avatar answered Mar 13 '23 14:03

Peter Herman