【问题标题】:R: Write RasterStack and preserve layer namesR:写入 RasterStack 并保留图层名称
【发布时间】:2015-01-01 23:42:54
【问题描述】:

我有一个光栅堆栈stk,由 R 中的三个光栅图像组成。这是一个简单的示例

# 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"

我为栅格图层指定名称:

# set layer names to "one", "two" and "three"
> names(stk) <- c('one','two','three')

> names(stk)
[1] "one" "two" "three"

当我使用以下方法将 RasterStack 写入 GeoTiff(多层)时:

writeRaster(stk,"myStack.tif", format="GTiff")

图层会根据文件名重命名(参见下面的&gt; names(stk))。

当我在光栅堆栈中读取时:

> 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"

您知道在 R 中编写 RasterStacks 时保留图层名称的任何方法吗?我尝试将堆栈写入 GeoTIFF 和 NetCDF 格式。

谢谢,凯文

【问题讨论】:

  • 你在哪里读取堆积的 tif 文件?
  • 保罗,感谢您的浏览。我只是清理了示例以使其更清晰并修复了一些措辞。我使用stk &lt;- stack("myStack.tif")(最后一个代码块的第一行)读取了堆叠的 tif 文件。再次感谢。

标签: r raster netcdf geotiff


【解决方案1】:

您可以使用原生光栅格式:

myRaster <- writeRaster(stk,"myStack.grd", format="raster")

光栅网格格式由二进制 .gri 文件和 .grd 头文件组成。这将保留您的图层名称。但请注意,.gri 二进制文件未压缩。

如果您需要在其他程序中打开光栅 grd 文件,您很可能需要编写额外的头文件。我通常使用 ENVI 标头格式来做到这一点。

hdr(myRaster, format = "ENVI")

例如,要从 qgis 打开文件,您需要选择 .gri 文件(二进制文件),它应该可以工作。

【讨论】:

  • 感谢您的回答!在 R 中读回栅格,图层名称被保留。但是,QGIS 无法显示正确的图层名称,尽管它们已正确存储在 .grd 和 .hdr 文件中。有解决办法吗?
  • 是否可以将图层名称与数据一起存储在一个文件中?还是 GeoTIFF 根本不支持图层命名?
【解决方案2】:

有点晚了,但可能会帮助其他人寻找可能的解决方案:

writeRaster(stk, filename=names(stk), bylayer=TRUE,format="GTiff")

【讨论】:

  • 这会将每一层写入一个单独的文件,而不是根据 OP 的问题将所有层及其名称写入一个堆栈..
【解决方案3】:

我将我的文件编写为 ENVI 文件,并更改了 ENVI 头文件中的波段名称。现在可以在 ENVI 和 ArcGis 中打开文件,并保留图层名称。

#write ENVI file (.envi; .hdr; .envi.aux.xml) with automatic layer names
writeRaster(stk, "myStack" , format="ENVI")

#change layer names in ENVI header (.hdr):
n="myStack.hdr"  
x <- readLines(n)
x <- gsub("Band 1,", "one,", x) 
x <- gsub("Band 2,", "two," , x)
x <- gsub("Band 3", "three", x)  
cat(x, file=n, sep="\n") #overwrites the old ENVI header

/编辑 我刚刚注意到,当 .envi 文件被导入回 R 时,图层名称会再次被删除。 SAGA 中也有同样的问题。

image <- stack("myStack.envi")  
names(image)
#[1] "myStack.1" "myStack.2" "myStack.3"

image = readGDAL("myStack.envi") 
names(image)
#[1] "band1" "band2" "band3"

【讨论】:

    【解决方案4】:

    您可以使用terrastars

    使用土地

    如果您将堆栈转换为 rast 对象(来自 terra 包),则会保留名称

    然后您可以使用 raster::writeRaster 或 terra::writeRaster,无论哪种方式,名称都会保留!

    library(terra)
    x <- rast(stk)
    terra::writeRaster(x, "file.tif")
    y <- raster::brick("file.tif") 
    #y <- terra::rast("file.tif")
    #y <- stars::read_stars("file.tif")
    names(y)
    

    使用星星

    library(stars)
    x <- st_as_stars(stk)
    write_stars(x, "file2.tif")
    y <- raster::brick("file2.tif")
    #y <- terra::rast("file2.tif")
    #y <- stars::read_stars("file2.tif")
    names(y)
    

    【讨论】:

      猜你喜欢
      • 2022-10-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-21
      • 1970-01-01
      • 2015-02-01
      相关资源
      最近更新 更多