Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write rasters after stacking them?

Tags:

r

raster

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)

like image 714
Barry Avatar asked Feb 15 '13 07:02

Barry


People also ask

What is a raster stack?

A RasterStack is a collection of RasterLayer objects with the same spatial extent and resolution. A RasterStack can be created from RasterLayer objects, or from raster files, or both. It can also be created from a SpatialPixelsDataFrame or a SpatialGridDataFrame object.

What is a RasterLayer?

RasterLayer. A RasterLayer object represents single-layer (variable) raster data. A RasterLayer object always stores a number of fundamental parameters that describe it. These include the number of columns and rows, the spatial extent, and the Coordinate Reference System.


1 Answers

 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])}
like image 198
plannapus Avatar answered Sep 22 '22 05:09

plannapus