【问题标题】:R sf: Find polygons outside of multiple overlapping polygonsR sf:查找多个重叠多边形之外的多边形
【发布时间】:2020-07-21 23:28:11
【问题描述】:

我有一个包含 1000 多个重叠多边形的大型 shapefile。我正在尝试在这些不与图层中的任何多边形重叠的多个重叠多边形之外的组合区域。这些实际上是火灾多次燃烧的区域,我正在寻找火灾只燃烧一次的区域。

我不知道如何找到没有任何重叠的外部区域。你能帮忙吗?

这是一个可重现的例子。

#make some overlapping polygons
m = rbind(c(0,0), c(1,0), c(1,1), c(0,1), c(0,0))
p = st_polygon(list(m))
n = 100
l = vector("list", n)
for (i in 1:n)
  l[[i]] = p + 2 * runif(2)
s = st_sfc(l)

#select just a few of these
s5 <- s[1:5]

#now try to step through and get the non-overlapping areas
counter <- 0
sall.out <- list()
for (i in 1:length(s5)) {
    print(i)
    s5.sel <- s5[i]
    s5.out <- s5[!(s5 == s5.sel)] #select all the polygons outside
    s5.int <- st_intersection(s5.out, s5.sel) #intersect the outside polygons with the one selected

    #step through and find all differences between the selected region and the intersected
    for (j in 1:length(s5.int)) {
        print(j)
        s5.out <- st_difference(s5.sel, s5.int[j])
    
        counter <- counter+1
        sall.out[[counter]] <- s5.out
    }
}

plot(s5)
plot(s5.sel, add=T, col="red")
plot(s5.int, add=T, col="blue")
plot(s5.out, add=T, col="pink")

所以,现在我将所有的 sall.out 都放在了一个列表中,但是如何删除彼此重叠的那些并展平列表呢?

谢谢。

【问题讨论】:

    标签: r polygon subtraction sf


    【解决方案1】:

    我建议你使用st_intersection这个方便的属性。来自文档:

    当调用缺少 y 时,st_intersection 的 sfc 方法返回 x 几何的所有非空交集;属性 idx 包含一个列表列,其中包含贡献几何的索引。

    这基本上“分段”了平面,并为每个分段返回一个几何图形。当您将多边形转换为sf 而不是sfc 时,这也意味着您将获得n.overlapsorigins 列,这些列描述了原始输入中每个几何图形的来源。然后您可以简单地过滤并查看重叠区域已被删除。

    library(tidyverse)
    library(sf)
    #> Linking to GEOS 3.8.1, GDAL 3.1.1, PROJ 7.1.0
    set.seed(1)
    m = rbind(c(0,0), c(1,0), c(1,1), c(0,1), c(0,0))
    p = st_polygon(list(m))
    n = 100
    l = vector("list", n)
    for (i in 1:n)
      l[[i]] = p + 2 * runif(2)
    
    
    s = st_sf(polygon = 1:n, geometry = st_sfc(l))
    s5 <- s[1:5, ]
    plot(s5["polygon"])
    

    non_overlaps <- st_intersection(s5) %>%
      filter(n.overlaps == 1)
    
    plot(non_overlaps["polygon"])
    

    reprex package (v0.3.0) 于 2020 年 7 月 21 日创建

    【讨论】:

    • 天哪,这看起来可以满足我的要求!当我运行 non_overlaps 行时,出现以下错误: st_geometry.sf(x) 中的错误:attr(obj, "sf_column") 不指向几何列。有什么遗漏吗?
    • 嗯,我知道这一定是我的 GEOS 版本的问题。或者,我认为至少。这适用于另一台机器。谢谢!!
    猜你喜欢
    • 2012-04-22
    • 2021-11-12
    • 2016-07-28
    • 2021-09-22
    • 2016-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多