How to write rasters after stacking?

There are some bitmap 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 for any idea about how we write d2

(rasters)

+3


source to share


2 answers


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

      

works because it d1

is one raster, not a list of rasters: indeed, the result overlay

is one raster (see ?overlay

).
Also, the concept stack

is to take multiple rasters with one layer and create one raster layer with multiple layers. In the end, if you really want to save each layer separately, you can unstack

bitmap before recording.
In this case:



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

      

+6


source


Plannapus's solution should work. Alternatively, you can either write to one 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 for 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)

      

+6


source







All Articles