【问题标题】:Convert raster brick in memory directly to brick from disc将内存中的光栅砖直接转换为光盘中的砖
【发布时间】:2020-03-22 11:04:50
【问题描述】:

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

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 日创建

【问题讨论】:

    标签: r raster r-raster


    【解决方案1】:

    这是你要找的吗?

    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 
    

    【讨论】:

    • 是的,太好了 - 我没有意识到可以直接从 writeRaster 分配
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-02-06
    • 2016-12-25
    • 2023-03-17
    • 2017-07-20
    • 1970-01-01
    • 2016-11-17
    • 1970-01-01
    相关资源
    最近更新 更多