I have a raster stack, stk
, consisting of three raster images in R. Here is a simple example
# set up a raster stack with three layers
> library(raster)
> r <- raster(nrows=10,ncols=10)
> r[] <- rnorm(100)
> stk <- stack(r,r,r)
# layer names are set by default
> names(stk)
[1] "layer.1" "layer.2" "layer.3"
I assign names to the raster layers:
# set layer names to "one", "two" and "three"
> names(stk) <- c('one','two','three')
> names(stk)
[1] "one" "two" "three"
When I write the RasterStack to a GeoTiff (multilayered) using:
writeRaster(stk,"myStack.tif", format="GTiff")
The layers are renamed based on the filename (see > names(stk)
below).
When I read in the raster stack:
> stk <- stack("myStack.tif")
# the layer names have been set automatically based on the filename
# they should be "one", "two" and "three"
> names(stk)
[1] "myStack.1" "myStack.2" "myStack.3"
Do you know of any way to preserve the layer names when writing RasterStacks in R? I have tried writing the stack to GeoTIFF and NetCDF formats.
Thanks, Kevin
stk <- stack("myStack.tif")
(the first line of the last code block). Thanks again. – kguay