【问题标题】:Write RasterStack and preserve metadata in R在 R 中编写 RasterStack 并保留元数据
【发布时间】:2022-10-18 03:34:59
【问题描述】:

我想写一个RasterStack 对象并保留各个层的名称和元数据。 here 解释了如何保留名称。在编写RasterStack 对象时,有没有办法保留各个层的元数据? 这是可复制的代码:

# load library
library(raster)

# create example rasters
ras_1 <- raster(nrows=180, ncols=360, xmn=-180, xmx=180, ymn=-90, ymx=90, resolution=, vals=1)
ras_2 <- raster(nrows=180, ncols=360, xmn=-180, xmx=180, ymn=-90, ymx=90, resolution=, vals=2)
ras_3 <- raster(nrows=180, ncols=360, xmn=-180, xmx=180, ymn=-90, ymx=90, resolution=, vals=3)

# assign names
names(ras_1) <- "raster_A"
names(ras_2) <- "raster_B"
names(ras_3) <- "raster_C"

# assign metadata
metadata(ras_1) <- list("metadata_raster_A")
metadata(ras_2) <- list("metadata_raster_B")
metadata(ras_3) <- list("metadata_raster_C")

# check
ras_1
ras_2
ras_3
metadata(ras_1)
metadata(ras_2)
metadata(ras_3)

# create and check stack
raster_stack <- stack(ras_1,
                      ras_2,
                      ras_3)
raster_stack
raster_stack[[1]]
metadata(raster_stack[[1]])

# write raster stack to disk
setwd("~")

# load library
library(terra)
# create rast object
raster_stack_terr <- rast(raster_stack)
# write raster stack
terra::writeRaster(raster_stack_terr, "raster_stack_terr_test.tif")

# load and check raster stack
raster_stack_check <- stack("raster_stack_terr_test.tif")
raster_stack_check
raster_stack_check[[1]]
names(raster_stack_check[[1]])
metadata(raster_stack_check[[1]])

根据here 的第三个答案,使用terra 保留名称。

从磁盘打开RasterStack 时,不会保留元数据。查看控制台输出:

> metadata(raster_stack_check[[1]])
list()

编写和重新加载RasterStack 对象时如何保留各个层的元数据?谢谢!

【问题讨论】:

    标签: r metadata raster geotiff terra


    【解决方案1】:

    {terra} 似乎没有提供与 raster::metadata() 等效的功能。但是,从我的角度来看,用例在这里会受到限制,因为在写入磁盘时,您只能将结构化信息存储在相应的格式特定标签中(至少,这是我的理解)。

    TIFF 文件 (c.f. here) 似乎提供以下标签:

    TIFFTAG_DOCUMENTNAME
    TIFFTAG_IMAGEDESCRIPTION
    TIFFTAG_SOFTWARE
    TIFFTAG_DATETIME
    TIFFTAG_ARTIST
    TIFFTAG_HOSTCOMPUTER
    TIFFTAG_COPYRIGHT
    TIFFTAG_XRESOLUTION
    TIFFTAG_YRESOLUTION
    TIFFTAG_RESOLUTIONUNIT
    

    另一方面,据我所知,ESRI-Grids 不提供存储元数据的任何可能性,除了已知的标头和文件名。

    如果您只想将某些元数据与您的栅格对象一起存储,您不妨使用attr(r, "meta") &lt;- "foobar"。但是,我看不到这些(随机)信息如何以特定格式存储并在之后恢复。

    您在使用{terra} 时已经注意到names(),但还有time() 需要提及。也许这已经满足您的需求,因为您没有具体说明您打算存储什么。

    # set up a raster stack with three layers
    library(terra)
    #> terra 1.6.17
    
    # create raster
    r <- rast(nrows = 10, ncols = 10)
    values(r) <- rnorm(100)
    
    # set metadata
    names(r) <- "foo"
    time(r) <- as.Date("2000-01-01")
    attr(r, "meta") <- "bar"
    
    # inspect
    r
    #> class       : SpatRaster 
    #> dimensions  : 10, 10, 1  (nrow, ncol, nlyr)
    #> resolution  : 36, 18  (x, y)
    #> extent      : -180, 180, -90, 90  (xmin, xmax, ymin, ymax)
    #> coord. ref. : lon/lat WGS 84 
    #> source      : memory 
    #> name        :       foo 
    #> min value   : -2.503790 
    #> max value   :  1.998731 
    #> time (days) : 2000-01-01
    
    # write to disk
    writeRaster(r, "sample.tif", overwrite = TRUE)
    
    # read from disk
    r2 <- rast("sample.tif")
    r2
    #> class       : SpatRaster 
    #> dimensions  : 10, 10, 1  (nrow, ncol, nlyr)
    #> resolution  : 36, 18  (x, y)
    #> extent      : -180, 180, -90, 90  (xmin, xmax, ymin, ymax)
    #> coord. ref. : lon/lat WGS 84 (EPSG:4326) 
    #> source      : sample.tif 
    #> name        :       foo 
    #> min value   : -2.503790 
    #> max value   :  1.998731 
    #> time (days) : 2000-01-01
    
    # try to access attributes
    attr(r2, "meta")
    #> NULL
    

    正如预期的那样,作为属性存储的数据已经丢失,而通过names()time() 提供的信息仍然存在。

    【讨论】:

      猜你喜欢
      • 2015-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-14
      • 2022-01-25
      • 1970-01-01
      • 1970-01-01
      • 2015-10-26
      相关资源
      最近更新 更多