这是一些示例数据
f <- system.file("ex/lux.shp", package="terra")
v <- vect(f)
x <- lapply(1:nrow(v), \(i)rescale(v[i,], 0.2))
vv <- vect(x)
r <- rast(v, ncols=10, nrows=10)
b <- as.lines(r)
标准的rasterize 方法会遗漏很多多边形,如果它们相对于像元大小来说很小。
x <- rasterize(vv, r, "ID_2")
plot(x)
lines(b, col="light gray")
lines(vv)
touches=TRUE 的参数在这种情况下会有所帮助
xx <- rasterize(vv, r, "ID_2", touches=TRUE)
plot(xx)
lines(b, col="light gray")
lines(vv)
但如果它们的多边形更小,它们仍然可能被遗漏。解决这个问题的一种方法是也栅格化多边形的质心(栅格化可能应该有一个参数来自动执行此操作)。
cnt <- centroids(vv)
# leaving out touches=T otherwise the example is not interesting
# with these data
x <- rasterize(vv, r, "ID_2")
y <- rasterize(cnt, r, "ID_2")
z <- cover(x, y)
plot(z)
lines(b, col="light gray")
lines(vv)
你也可以考虑使用
x <- rasterize(vv, r, cover=TRUE)
x <- x > 0