我试着计算72波段光栅。如果前36波段值(近红外波段)大于后36波段值(短波红外波段),则将其赋值为0;如果没有,则继续执行以下功能。我尝试过其他编写方法(基本上是相同的逻辑),同样的错误也会出现。这是我写的函数:
raster_stack <- stack("NIR.bin", "SWIR.bin")
#ndii = NIR - SWIR/NIR+SWIR
fun <- function(x) {
x[is.na(x)] <- 0;
if (x[37:72] >= x[1:36]){
0} else {
ndii <- ((x[1:36]-x[37:72]) / (x[1:36]+x[37:72]));
silent=TRUE;
return(ndii)
}
}
ndii <- calc(raster_stack, fun)错误消息总是如下所示:
setValues中的错误(out,x):值必须是数字、整数、逻辑或因子
我添加x[is.na(x)] <- 0是为了去掉NA值,但它似乎没有帮助。对解决这个问题有什么见解吗?
发布于 2016-05-30 18:55:22
有几个问题。您的if语句不太好,因为您正在比较36个值。此外,您还需要将数据视为一个矩阵。
library(raster)
raster_stack <- stack("NIR.bin", "SWIR.bin")
#ndii = NIR - SWIR/NIR+SWIR
fun <- function(x) {
y <- (x[,1:36]-x[,37:72]) / (x[,1:36] + x[,37:72])
i <- x[,37:72] >= x[,1:36]
y[i] <- 0
y
}
ndii <- calc(raster_stack, fun)https://stackoverflow.com/questions/37118800
复制相似问题