How to write rasters after stacking them?

随声附和 提交于 2019-12-04 23:58:28

问题


There are several raster files that I want to manipulate and then write them again.

rasterfiles   <- list.files("C:\\data", "*.envi", full.names = TRUE)
d1 <-  overlay(stack(rasterfiles ), 
               fun=function(x) movingFun(x, fun=mean, n=3, na.rm=TRUE))
d2=unstack(d1)

I am grateful to any idea on how we write d2 (rasters)


回答1:


 writeRaster(d1, file="d1.nc") #other file formats such as .envi work as well

works since d1 is one single raster and not a list of rasters: indeed the result of overlay is one single raster (see ?overlay).
Additionally the concept of stack is precisely to take several rasters having one layer and produce one raster with several layer.
Eventually if you really want to save each layer separately, you can unstack your raster prior to writing.
In which case:

d2 <- unstack(d1)
outputnames <- paste(seq_along(d2), ".nc",sep="")
for(i in seq_along(d2)){writeRaster(d2[[i]], file=outputnames[i])}



回答2:


Plannapus solution should work. Alternatively, you can either write to a single file in one step:

 rasterfiles   <- list.files("C:\\data", "*.envi", full.names = TRUE)
 d1 <-  overlay(stack(rasterfiles ), 
           fun=function(x) movingFun(x, fun=mean, n=3, na.rm=TRUE), 
           filename='output.tif' )

Or to multiple files in two steps

 rasterfiles   <- list.files("C:\\data", "*.envi", full.names = TRUE)
 d1 <-  overlay(stack(rasterfiles ), 
           fun=function(x) movingFun(x, fun=mean, n=3, na.rm=TRUE))
 d2 <- writeRaster(d1, 'out.tif', bylayer=TRUE)


来源:https://stackoverflow.com/questions/14890369/how-to-write-rasters-after-stacking-them

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