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

use*_*135 4 r polygon subtraction r-sf

我有一个很大的 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")

Run Code Online (Sandbox Code Playgroud)

所以,现在我将所有 sall.out 都放在一个列表中,但是如何删除彼此重叠的内容并使列表变平?

谢谢。

Cal*_*You 7

我建议您使用 的便捷属性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"])
Run Code Online (Sandbox Code Playgroud)

non_overlaps <- st_intersection(s5) %>%
  filter(n.overlaps == 1)

plot(non_overlaps["polygon"])
Run Code Online (Sandbox Code Playgroud)

由reprex 包(v0.3.0)于 2020-07-21 创建