如何从 terra 包中获取 SpatRasterDataset 的子集?
我正在尝试使用 R 中的 terra 包从 SpatRasterDataset 中选择多个栅格。但是,当我尝试一次选择 x 个栅格时,名称仅显示前 x 个栅格名称,无论我尝试选择哪个栅格。选择单个栅格似乎工作正常,尽管给定的名称不存在。
那么如何正确子集 SpatRasterDataset 并保持与栅格关联的正确名称?
可重现的例子:
# Create SpatRasterDataset
r1 <- rast(matrix(1:100, nrow=10, ncol=10))
r2 <- rast(matrix(200:101, nrow=10, ncol=10))
r3 <- rast(matrix(c(2,200), nrow=10, ncol=10))
s <- sds(r1,r2,r3)
# Name rasters
names(s) <- c("A","B","C")
names(s)
# Add to list
l <- list(s)
l[[1]]
l[[1]][1:2] # shows rasters A,B
l[[1]][2:3] # also shows rasters A,B
l[[1]][c("B","C")] # shows rasters A,B
l[[1]][3]
image(l[[1]][3]) # shows correct raster
# Subset SpatRasterDataset
s2 <- l[[1]][2:3] # select two rasters
names(s2) # names A, B
s2[1] # actually raster B
s2[2] # actually raster C
s3 <- l[[1]][3] # select one rasters
names(s3) # has some other name
s3 # has correct raster
I am trying to select several rasters from a SpatRasterDataset using the terra package in R. However when I try to select x rasters at once, the names show only the first x raster names, regardless of which rasters I try to select. Selecting a single raster seems to work fine, although the name given to it is not present.
So how do I properly subset a SpatRasterDataset keeping the correct names associated with rasters?
Reproducible example:
# Create SpatRasterDataset
r1 <- rast(matrix(1:100, nrow=10, ncol=10))
r2 <- rast(matrix(200:101, nrow=10, ncol=10))
r3 <- rast(matrix(c(2,200), nrow=10, ncol=10))
s <- sds(r1,r2,r3)
# Name rasters
names(s) <- c("A","B","C")
names(s)
# Add to list
l <- list(s)
l[[1]]
l[[1]][1:2] # shows rasters A,B
l[[1]][2:3] # also shows rasters A,B
l[[1]][c("B","C")] # shows rasters A,B
l[[1]][3]
image(l[[1]][3]) # shows correct raster
# Subset SpatRasterDataset
s2 <- l[[1]][2:3] # select two rasters
names(s2) # names A, B
s2[1] # actually raster B
s2[2] # actually raster C
s3 <- l[[1]][3] # select one rasters
names(s3) # has some other name
s3 # has correct raster
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
SpatRasterDataset 的名称与 SpatRaster 的(图层)名称不对应。
通过您的示例,
您可以像这样对 SpatRasterDataset
s
进行子集化以获得新的 SpatRasterDataset:或者像这样获得 SpatRaster
“A”、“B”和“C”是子数据集的名称(变量),而不是 SpatRaster 的(图层)名称。这些不能相同,因为每个子数据集都有一个名称,而 SpatRaster 可以有多个(图层)名称。
例如,您可能有
一个 SpatRaster 可以有一个可以设置的变量名称
,但这可能不是您想要的。
The names of a SpatRasterDataset do not correspond to the (layer) names of a SpatRaster.
With your example
You can subset SpatRasterDataset
s
like this to get a new SpatRasterDataset:Or like this to get a SpatRaster
"A", "B", and "C" are names of the sub-datasets (variables), not the (layer) names of the SpatRasters. These cannot be the same because each sub-dataset has a single name, whereas a SpatRaster can have many (layer-) names.
For example, you might have
A SpatRaster can have a variable name which you can set with
but that is probably not what you are after.
当使用多层子数据集对 SpatRasterDataset 进行子集化时,@robert-hijmans 提供的答案仍然会导致意外行为。这是一个具有以下结构的 SDS 示例:
因此,使用索引进行子集化确实有效,但变量名称是错误的。这是我想出的简单解决方案 - 只需使用用于子集 SDS 的字符向量设置
varnames
即可。The answer provided by @robert-hijmans still results in unexpected behavior when subsetting a SpatRasterDataset with multi-layer sub-datasets. Here is an example with a SDS with the following structure:
So subsetting with an index does work, but the variable names are wrong. This is the simple solution I came up with - just setting the
varnames
with the character vector used to subset the SDS.