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

时间:2020-07-21 23:28:11

标签: r polygon subtraction 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")

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

谢谢。

1 个答案:

答案 0 :(得分: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-07-21创建