简体   繁体   English

根据其位置设置栅格NA的像素

[英]Set pixels of raster NA based on their location

I have a raster which has 50+ bands. 我的栅格有50多个波段。 What I would like to is to search for all pixel == 0 within the first band of the raster. 我要搜索的是在栅格的第一个波段内的所有像素== 0。 Following I would like to set all of these pixels NA in the other raster bands as well. 接下来,我还要在所有其他光栅带中设置所有这些像素NA。 Thus, I don't have to do the search for 0 again 50+ times. 因此,我不必再次搜索0超过50次。 Here is my example: 这是我的示例:

cl_input <- brick("sometif")
for(i in 1:nlayers(cl_input)){
    print(names(cl_input[[i]]))
    cl_input[[i]][cl_input[[i]] == 0] <- NA   
}

I hope I stated out my need clearly =) 我希望我清楚地表明了我的需要=)

Thanks in advance, cheers 预先感谢,欢呼

You can use the mask method; 您可以使用mask方法; using the first layer as the mask, and by setting the maskvalue to zero. 使用第一层作为遮罩,并将maskvalue设置为零。

library(raster)
b <- brick(nrow=2, ncol=2, nl=3)
values(b) <- matrix(c(0,1,1),4,3)
values(b)
##     layer.1 layer.2 layer.3
##[1,]       0       1       1
##[2,]       1       1       0
##[3,]       1       0       1
##[4,]       0       1       1

d <- mask(b, b[[1]],  maskvalue=0)
values(d)
##     layer.1 layer.2 layer.3
##[1,]      NA      NA      NA
##[2,]       1       1       0
##[3,]       1       0       1
##[4,]      NA      NA      NA

No need for a loop here. 此处无需循环。 You can search for 0 in all layers at once: 您可以一次在所有图层中搜索0:

cl_input <- brick("sometif")
cl_input[cl_input == 0] <- NA

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM