在下面的例子中,extract 函数正确地告诉我们多边形 x2 内 r 的平均值是 5.14。但是,对于像 x1 这样小于栅格的多边形,提取返回值“NaN”
r <- rast(nrows = 10, ncol = 10, nlyrs = 1, vals = sample(1:10, 100, replace = TRUE), names = "temp")
x1 <- rbind(c(-145,-10), c(-145,-5), c(-140, -5), c(-140,-10))
x2 <- rbind(c(-10,0), c(140,60), c(160,0), c(140,-55))
z <- rbind(cbind(object=1, part=1, x1, hole=0),
cbind(object=3, part=1, x2, hole=0))
colnames(z)[3:4] <- c('x', 'y')
p <- vect(z, "polygons")
plot(r)
plot(p, add = T)
test <- terra::extract(r, p, fun = mean, cell = TRUE)
test
ID temp
1 1 NaN
2 2 5.142857