r - 使用来自 st_intersection 的原点值聚合图层内重叠多边形的值
问题描述
我想采用具有重叠多边形的空间层并对其进行转换,以便创建一个没有任何重叠的层,并将重叠多边形的属性聚合为列表或总和(将根据分析的领域和目标而有所不同)。根据这些帖子https://github.com/r-spatial/sf/issues/1230,总结 sf::st_intersection() 几何重叠的属性 我认为我应该能够使用创建时创建的 origins 字段来做到这一点st_intersection() 与单个输入一起使用。
带有一些虚拟数据的示例:
set.seed(123)
p1 = st_cast(st_sfc(st_multipoint(cbind(runif(10),runif(10)))),"POINT")
b1 =st_buffer(p1, .15)
b1d = data.frame(id=1:10, class=rep(c("high", "low"), times=5), value= rep(c(10,5),times=5))
b1d$geometry = b1
b1d = st_as_sf(b1d)
b1d_intersect <- st_intersection(b1d)%>%
mutate(id_int=seq(from=1, to=nrow(.), by=1)) %>%
st_collection_extract()
ggplot(b1d)+geom_sf(aes(fill=class), alpha=0.5)
ggplot(b1d_intersect)+geom_sf(aes(fill=class), alpha=0.5)
在我想要的输出中,每个多边形的属性会加剧重叠的属性值,例如:
b1d_intersect_values <- b1d_intersect %>%
group_by(rownames(.)) %>%
summarise(Class_List=paste0(class, collapse = "; "), value_sum=sum(value))
但这实际上是通过使用 origins 字段链接回原始 b1d 层来实现的?我觉得我需要在那里的某个地方使用 map() 。
我也尝试过使用 st_join 来解决它,但这并不完全正确,因为它为 n.overlaps=1 的多边形分配了多个类,而且在应用于大型数据集,因为它需要使用 st_join 进行额外的空间处理步骤。
b1d_intersect_values_2 <- st_join(b1d_intersect, b1d) %>%
group_by(id_int, n.overlaps) %>%
summarise(class_list=paste0((class.y),collapse="; "), value_sum=sum(value.y))
任何有关如何进行的建议将不胜感激。
解决方案
最终想通了,回答自己,这样没有人会浪费时间或精力,以防万一对其他人有所帮助。
b1d_intersect_values <- b1d_intersect %>%
mutate(class_list=map_chr(origins, ~ paste0(class[.], collapse = "; ")),
value_sum=map_dbl(origins, ~ sum(value[.])))
ggplot(b1d_intersect_values)+geom_sf(aes(fill=class_list, alpha=value_sum))
推荐阅读
- excel - 录制的宏以删除空行但不能始终如一地工作
- android - 如何在具有重力的 Android Sceneform 级别中定向 GLTF
- python-3.x - 循环遍历字符串中的每个字符,然后用首字母缩写词替换所述字符,而不使用 replace() 或 find() 函数
- php - 为什么只向 mysql 数据库插入一列不会在 codeigniter 中显示错误?
- php - 当有人仅使用 PHP 和 HTML 在表单中输入内容时,如何创建新的表格行?
- java - 如何在 webclient 中返回 Flux.just 状态
- python - Python相当于r中的子集函数
- python - 'numpy.ndarray' 对象没有使用 reshape 的属性 'values'
- html - 离子从一页导航到另一页,不起作用
- php - 如何从数据库中列出 DISTINCT ALL 产品