r语言 - 使用st_intersection的原点值聚合层内重叠多边形的值



我想取一个具有重叠多边形的空间层并对其进行转换,以便创建一个没有任何重叠的层,并且重叠多边形的属性聚合为列表或总和(将根据分析的领域和目标而变化)。基于这些帖子https://github.com/r-spatial/sf/issues/1230,总结sf::st_intersection()中几何图形重叠的属性我认为我应该能够做到这一点,使用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))

,但这实际上是通过链接回原来的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)) 

最新更新