我的輸入柵格由多個圖層組成,每個圖層的圖像區域都沒有數據值。這些圖層不完全重疊,我試圖輸出一個只包含所有波段(任何圖層上沒有NoData值的區域)的交集的文件。R柵格包中的交叉帶
以下作品幾層,但不能用於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)
就如何有效地做到這一點更加任何想法/得到它與大量的圖層一起工作?
這是聯盟,而不是交集...和它等於我的原始文件。 – Benjamin 2011-04-09 04:00:12
沒錯。現在希望更接近。 – RobertH 2011-04-09 15:47:58
一個簡單而簡潔的解決方案,但我用我的大rasterStacks內存不足...... :( – Benjamin 2011-04-15 03:35:35