【问题标题】:Aggregating values of overlapping polygons within a layer using the origins value from st_intersection使用来自 st_intersection 的原点值聚合图层内重叠多边形的值
【发布时间】:2023-03-29 00:14:01
【问题描述】:

我想获取具有重叠多边形的空间层并对其进行转换,以便创建没有任何重叠的层,并将重叠多边形的属性聚合为列表或总和(将根据字段和分析目标)。基于这些帖子https://github.com/r-spatial/sf/issues/1230summarise attributes from sf::st_intersection() where geometries overlaps 我认为我应该能够使用 st_intersection() 与单个输入一起使用时创建的 origins 字段来执行此操作。

带有一些虚拟数据的示例:

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))

任何关于如何进行的建议将不胜感激。

【问题讨论】:

    标签: r spatial sf


    【解决方案1】:

    最终想通了,回答我自己,这样没有人会浪费时间或精力,以防万一对其他人有所帮助。

    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)) 
    

    【讨论】:

      猜你喜欢
      • 2022-01-03
      • 1970-01-01
      • 2018-06-25
      • 1970-01-01
      • 2020-12-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-13
      相关资源
      最近更新 更多