terra::extract 当多边形小于栅格像元时给出 NaN
terra::extract gives NaN when polygon is smaller than raster cell
在下面的示例中,提取函数正确地告诉我们多边形 x2 中 r 的平均值为 5.14。但是,对于像 x1 这样小于栅格的多边形,提取 returns 值“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
如何获得 x1 处的 r 值?
您可以使用exact=TRUE
示例数据
library(terra)
r <- rast(nrows = 10, ncols = 10, nlyrs = 1, vals =1:100, 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), cbind(object=2, part=1, x2))
colnames(z)[3:4] <- c('x', 'y')
p <- vect(z, "polygons")
默认值
extract(r, p, fun = mean)
# ID temp
#1 1 NaN
#2 2 53
使用 touches=TRUE
你会得到所有被触摸的单元格
extract(r, p, fun = mean, touches=TRUE)
# ID temp
#1 1 51.50000
#2 2 52.62069
或者你也可以
e <- extract(r, p, exact=TRUE)
head(e)
# ID temp fraction
#1 1 51 0.007539715
#2 1 52 0.030169815
#3 2 19 0.104219078
#4 2 28 0.282198174
#5 2 29 0.883159178
#6 2 30 0.043386000
x <- by(e[,2:3], e[,1], function(x) weighted.mean(x[,1], x[,2]))
as.vector(x)
# [1] 51.80006 52.21312
(如果您熟悉该语法,则使用 dplyr 或 data.table)
使用 开发 版本 (1.3.11),可从
install.packages('terra', repos='https://rspatial.r-universe.dev')
,你得到:
extract(r, p, fun=mean)
# ID temp
#1 1 51.5
#2 2 53.0
你可以做到
extract(r, p, fun=mean, exact=TRUE)
# ID temp
#[1,] 1 51.80006
#[2,] 2 52.21312
在下面的示例中,提取函数正确地告诉我们多边形 x2 中 r 的平均值为 5.14。但是,对于像 x1 这样小于栅格的多边形,提取 returns 值“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
如何获得 x1 处的 r 值
您可以使用exact=TRUE
示例数据
library(terra)
r <- rast(nrows = 10, ncols = 10, nlyrs = 1, vals =1:100, 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), cbind(object=2, part=1, x2))
colnames(z)[3:4] <- c('x', 'y')
p <- vect(z, "polygons")
默认值
extract(r, p, fun = mean)
# ID temp
#1 1 NaN
#2 2 53
使用 touches=TRUE
你会得到所有被触摸的单元格
extract(r, p, fun = mean, touches=TRUE)
# ID temp
#1 1 51.50000
#2 2 52.62069
或者你也可以
e <- extract(r, p, exact=TRUE)
head(e)
# ID temp fraction
#1 1 51 0.007539715
#2 1 52 0.030169815
#3 2 19 0.104219078
#4 2 28 0.282198174
#5 2 29 0.883159178
#6 2 30 0.043386000
x <- by(e[,2:3], e[,1], function(x) weighted.mean(x[,1], x[,2]))
as.vector(x)
# [1] 51.80006 52.21312
(如果您熟悉该语法,则使用 dplyr 或 data.table)
使用 开发 版本 (1.3.11),可从
install.packages('terra', repos='https://rspatial.r-universe.dev')
,你得到:
extract(r, p, fun=mean)
# ID temp
#1 1 51.5
#2 2 53.0
你可以做到
extract(r, p, fun=mean, exact=TRUE)
# ID temp
#[1,] 1 51.80006
#[2,] 2 52.21312