Given the documentation for the writeRaster()
function, your code should work, however it looks like it hasn't been implemented for a RasterBrick
object (I don't know why this is).
That said, I think you can accomplish what you're trying to do using iteration and the raster()
function, which creates a single-layer raster object. From a RasterBrick
, the syntax would look like raster(brick_object, layer = 1)
. From there, you can write the (now single-band) raster to disk. Using the longlake_osm
object in the ggspatial package (a raster brick with 3 layers), your code might look like this:
library(raster)
#> Loading required package: sp
ggspatial::load_longlake_data()
longlake_osm
#> class : RasterBrick
#> dimensions : 471, 553, 260463, 3 (nrow, ncol, ncell, nlayers)
#> resolution : 3.33, 3.32 (x, y)
#> extent : 409891.4, 411732.9, 5083289, 5084853 (xmin, xmax, ymin, ymax)
#> coord. ref. : +proj=utm +zone=20 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs
#> data source : /Library/Frameworks/R.framework/Versions/3.5/Resources/library/ggspatial/longlake/longlake.tif
#> names : longlake.1, longlake.2, longlake.3
#> min values : 25, 74, 46
#> max values : 255, 255, 255
# with a single band, this is how to extract it as a separate raster
single_band <- raster(longlake_osm, layer = 1)
writeRaster(single_band, "layer_1.tif")
# for multiple bands, I think this works well in a for() loop
layer_filenames <- c("red.tif", "green.tif", "blue.tif")
for(i in 1:3) {
single_band <- raster(longlake_osm, layer = i)
writeRaster(single_band, layer_filenames[i])
}
If you are familiar with the purrr package, you could also write the loop as an "apply-the-function-along-the-list" approach:
library(raster)
#> Loading required package: sp
library(purrr)
ggspatial::load_longlake_data()
layer_filenames <- c("red.tif", "green.tif", "blue.tif")
layers_list <- map(1:3, ~raster(longlake_osm, layer = .))
# use walk2 rather than map2 because we don't want the output
# of writeRaster
walk2(layers_list, layer_filenames, writeRaster)
Created on 2019-04-27 by the reprex package (v0.2.1)