R:编写RasterStack并保留图层名称 [英] R: Write RasterStack and preserve layer names

查看:621
本文介绍了R:编写RasterStack并保留图层名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a raster stack, stk, consisting of three raster images in R. Here is a simple example

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

我为栅格图层分配名称:

I assign names to the raster layers:

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

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

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

When I write the RasterStack to a GeoTiff (multilayered) using:

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

根据文件名重命名图层(请参见下面的> names(stk)).

The layers are renamed based on the filename (see > names(stk) below).

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

When I read in the raster stack:

> 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格式.

Do you know of any way to preserve the layer names when writing RasterStacks in R? I have tried writing the stack to GeoTIFF and NetCDF formats.

谢谢,凯文

推荐答案

您可以使用本机栅格格式:

You can make use of the native raster format:

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

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

The raster grid format consists of the binary .gri file and the .grd header file. This will preserve your layernames. Note, however, that .gri binary files are not compressed.

如果需要在其他程序中打开raster grd文件,则很可能需要编写一个附加的头文件.我通常使用ENVI标头格式来做到这一点.

If you need to open raster grd files in other programs you will most likely need to write an additional header file. I usually use the ENVI header format to do that.

hdr(myRaster, format = "ENVI")

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

To open the file from qgis for example you'd select the .gri file (the binary) and it should work.

这篇关于R:编写RasterStack并保留图层名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆