【发布时间】:2021-12-06 06:09:11
【问题描述】:
因此,使用 dplyr 中的 st_union 和 group_by() 找到两个以上多边形的并集是很简单的。找到与 st_intersection 的交集并不是那么简单,因为 st_intersection 仅适用于寻找两个对象的交集。按组查找多个多边形的交集的最佳方法是什么? 这是一些示例数据
s1 <- rbind(c(1, 1), c(10, 1), c(10, 10), c(1, 10), c(1, 1))
s2 <- s1 + 4
s3 <- s1 - 4
group <- c(rep(1, 3), rep(2, 3))
df <- data.frame("group" = c(rep(1, 3), rep(2, 3)))
df <- data.frame(cbind(df, rep((c(st_sfc(st_polygon(list(s1))), st_sfc(st_polygon(list(s2))), st_sfc(st_polygon(list(s3))))), 2)))
我想要的是将几何列替换为每个组中的交集,即由
创建的对象
p <- list(s1 = s1, s2 = s2, s3 = s3)
p <- lapply(p, function(x) st_sfc(st_polygon(list(x))))
intersection <- accumulate(p, st_intersection)$s3
intersection <- st_sfc(st_polygon(intersection))
所以最终的数据看起来像
df <- data.frame("group" = c(rep(1, 3), rep(2, 3)))
df <- data.frame(st_sf(cbind(df, rep(st_sfc(st_polygon(intersection)), 6))))
【问题讨论】:
标签: r dplyr intersection sf