How to save raster data in R object format?

99封情书 提交于 2019-12-24 00:41:55

问题


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.


回答1:


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




回答2:


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



来源:https://stackoverflow.com/questions/40473199/how-to-save-raster-data-in-r-object-format

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!