将内存中的光栅砖直接转换为光盘中的砖

Convert raster brick in memory directly to brick from disc

我在内存中有一个现有的光栅砖块对象,例如:

library(raster)
#> Loading required package: sp

set.seed(84832)

r1 <- raster(matrix(runif(4), ncol = 2))
r2 <- raster(matrix(runif(4), ncol = 2))

b <- brick(r1, r2)

b
#> class      : RasterBrick 
#> dimensions : 2, 2, 4, 2  (nrow, ncol, ncell, nlayers)
#> resolution : 0.5, 0.5  (x, y)
#> extent     : 0, 1, 0, 1  (xmin, xmax, ymin, ymax)
#> crs        : NA 
#> source     : memory
#> names      :    layer.1,    layer.2 
#> min values : 0.45211936, 0.04150152 
#> max values :  0.9454290,  0.2539412

有没有办法直接存盘转成砖读盘?像这样的事情(失败):

b <- brick(b, "b.grd")
#> Warning in .local(x, ...): NAs introduced by coercion

#> Warning in .local(x, ...): NAs introduced by coercion
#> Error in rep(Inf, nl): invalid 'times' argument

b
#> class      : RasterBrick 
#> dimensions : 2, 2, 4, 2  (nrow, ncol, ncell, nlayers)
#> resolution : 0.5, 0.5  (x, y)
#> extent     : 0, 1, 0, 1  (xmin, xmax, ymin, ymax)
#> crs        : NA 
#> source     : memory
#> names      :    layer.1,    layer.2 
#> min values : 0.45211936, 0.04150152 
#> max values :  0.9454290,  0.2539412

与将砖块写入文件然后读回并覆盖现有砖块相反:

writeRaster(b, "b.grd")

b <- brick("b.grd")

b
#> class      : RasterBrick 
#> dimensions : 2, 2, 4, 2  (nrow, ncol, ncell, nlayers)
#> resolution : 0.5, 0.5  (x, y)
#> extent     : 0, 1, 0, 1  (xmin, xmax, ymin, ymax)
#> crs        : NA 
#> source     : memory
#> names      :    layer.1,    layer.2 
#> min values : 0.45211936, 0.04150152 
#> max values :  0.9454290,  0.2539412

reprex package (v0.3.0)

于 2019-11-27 创建

这是您要找的吗?

library(raster)
set.seed(84832)
r1 <- raster(matrix(runif(4), ncol = 2))
r2 <- raster(matrix(runif(4), ncol = 2))
b <- brick(r1, r2)
x <- writeRaster(b, "test123.tif", overwrite=TRUE)
x

#class      : RasterBrick 
#dimensions : 2, 2, 4, 2  (nrow, ncol, ncell, nlayers)
#resolution : 0.5, 0.5  (x, y)
#extent     : 0, 1, 0, 1  (xmin, xmax, ymin, ymax)
#crs        : NA 
#source     : test123.tif 
#names      :  test123.1,  test123.2 
#min values : 0.45211936, 0.04150152 
#max values :  0.9454290,  0.2539412