R sf:查找多个重叠多边形之外的多边形

R sf: Find polygons outside of multiple overlapping polygons

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

我对如何找到没有任何重叠的外部区域感到困惑。你能帮忙吗?

这是一个可重现的例子。

#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,但是我怎样才能删除相互重叠的那些并展平列表?

谢谢。

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

When called with missing y, the sfc method for st_intersection returns all non-empty intersections of the geometries of x; an attribute idx contains a list-column with the indexes of contributing geometries.

这基本上是“分段”平面,returns 每段一个几何体。当您将多边形转换为 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-07-21 创建