R 栅格包中波段的交集
我的输入栅格由多个图层组成,每个图层都有一个没有数据值包围的图像区域。这些图层不完全重叠,我试图输出一个仅包含所有波段的交集(任何图层上没有 NoData 值的区域)的文件。
以下内容适用于几个图层,但不适用于我的真实文件中的 50+ 个图层(至少 3000x3000 像素):
library(raster)
fin = "D:\\temp\\all_modes.pix"
fout = "D:\\temp\\test.pix"
inbands = stack(fin, bands = c(3:20))
NAvalue(inbands) = 0
# Not great:
#out = all(is.na(inbands) == FALSE) * inbands
#writeRaster(out, filename=fout, format="PCIDSK", dtype="INT2U", overwrite=TRUE, NAflag=0)
# A little better:
#mymask = all(as.logical(inbands))
#mask(inbands, mymask, filename=fout, format="PCIDSK", dtype="INT2U", overwrite=TRUE, NAflag=0)
# Even better, don't need to keep everything (but still not efficient):
#trim(all(as.logical(inbands)) * inbands, filename=fout, format="PCIDSK", dtype="INT2U", overwrite=TRUE, NAflag=0)
# Even better, calculations get smaller as we progress (is it possible to do even better?)
for(i in 1:nlayers(inbands)){
band_i = subset(inbands, i)
inbands = trim(as.logical(band_i) * inbands)
}
writeRaster(inbands, filename=fout, format="PCIDSK", dtype="INT2U", overwrite=TRUE, NAflag=0)
关于如何更有效地执行此操作/让它与大量图层一起工作有什么想法吗?
My input raster consists of multiple layers, each with an image area surrounded by no data values. These layers don't overlap completely, and I am trying to output a file which consists of only the intersection of all bands (the zone which has no NoData values on any layer).
The following works for a few layers, but not for the 50+ that I have in my real files (at least 3000x3000 pixels):
library(raster)
fin = "D:\\temp\\all_modes.pix"
fout = "D:\\temp\\test.pix"
inbands = stack(fin, bands = c(3:20))
NAvalue(inbands) = 0
# Not great:
#out = all(is.na(inbands) == FALSE) * inbands
#writeRaster(out, filename=fout, format="PCIDSK", dtype="INT2U", overwrite=TRUE, NAflag=0)
# A little better:
#mymask = all(as.logical(inbands))
#mask(inbands, mymask, filename=fout, format="PCIDSK", dtype="INT2U", overwrite=TRUE, NAflag=0)
# Even better, don't need to keep everything (but still not efficient):
#trim(all(as.logical(inbands)) * inbands, filename=fout, format="PCIDSK", dtype="INT2U", overwrite=TRUE, NAflag=0)
# Even better, calculations get smaller as we progress (is it possible to do even better?)
for(i in 1:nlayers(inbands)){
band_i = subset(inbands, i)
inbands = trim(as.logical(band_i) * inbands)
}
writeRaster(inbands, filename=fout, format="PCIDSK", dtype="INT2U", overwrite=TRUE, NAflag=0)
Any ideas on how to do this more efficiently / get it working with a large number of layers?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我首先提出了这个:
x <- trim(inband, filename='fout')
但我现在意识到这不会让你得到你想要的,因为它会返回区域(行/列)其中至少一层的值不是 NA;而不是所有值都不是 NA 的区域。
下面的内容可能会很有效。所有至少具有一个 NA 值的单元格将变为 NA with sum(默认情况下,na.rm=FALSE)。
如果 x 中的所有单元格为 NA,则可能随后
将 r 中的所有单元格设置为 NA
I first proposed this:
x <- trim(inband, filename='fout')
But I now realize that would not get you what you want, as it would return the area (rows/columns) where at least one layer has a value that is not NA; rather than the area where all have a value that is not NA.
The below could be efficient. All cells with at least one NA value will become NA with sum (by default, na.rm=FALSE).
perhaps followed by
to set all cells in r to NA if they are NA in x
感谢您的回答,他们给了我很好的想法。我想出了这个,它要快得多:
Thanks for the answers, they gave me good ideas. I came up with this, which is much faster:
我发现
all
/any
是在长列表上执行逻辑 AND/OR 的方法。这里演示了两个相同的图,可以在小栅格上实现您的目标。在我的系统上,存在将数字强制为逻辑的警告。以下两种方法可以避免警告但可能会慢一些?它们在逻辑上是等效的,但您可以将它们相互比较并与上面的第二种方法进行比较。
I discovered that
all
/any
are the ways to do logical AND/OR on a long list. Here are demonstrated two identical plots that accomplish your goal on small rasters.On my system there are warnings for coercing numeric to logical. The following two methods avoid the warnings but might be slower? They are logically equivalent but you might time them against each other and against the second method above.