Issue using saveRDS() with raster objects

点点圈 提交于 2019-12-11 12:12:34

问题


I'm trying to use saveRDS() to save a large number of lists each containing a raster layer and a list with metadata. It worked fine when the raster layer was extracted from a ncdf file, but when the original file is an ascii file, saveRDS() only writes a pointer to the original file instead of writing the values to the end file.

Here's a condensed version of what's going on:

require(raster)

mf <- raster('myfile.asc')
meta <- list(mylonglistofmetadata)
res <- list(mf, meta)
saveRDS(res, 'myresult.Rdata')

myresult.Rdata is now simply a 33KB pointer to myfile.asc, when I really would like it to store the values so it will still work after I erase myfile.asc (so it should be about 15MB)

In contrast, for other files in ncdf format:

require(ncdf4)
require(raster)

ff <- 'myfile2.nc'
nc <- nc_open(ff)
meta <- list(mylonglistofmetadata)
res <- list(nc, meta)
saveRDS(res, 'myresult2.Rdata')

Here, myresult2.Rdata is storing everything just like I want it to, so my guess is that the issue arises with the raster package?

Anyone has any idea on how to fix this? I would prefer not to use writeRaster(), since I'm trying to keep the metadata together with the data, and use the same format as in my batch extracted from ncdf files to ease later processing.


回答1:


The short answer is that you can do:

mf <- raster('myfile.asc')
mf <- readAll(mf)
mf

Now, the values are in memory and will be saved to the .RData file

Also note that:

  • You can save metadata with the data via writeRaster (see ?raster::metadata

  • you can access ncdf files (with geographic data) via raster('myfile2.nc')

  • your example for the ncdf file is not informative, as you do not actually use nc for anything. If you replaced mf with nc it would not work either after you removed 'myfile2.nc'



来源:https://stackoverflow.com/questions/30904740/issue-using-saverds-with-raster-objects

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