我有一个包含 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 放在一个列表中,但是如何删除相互重叠的那些并将列表展平?谢谢你。
最佳答案
建议大家使用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.
这基本上“分割”了平面,并为每段返回一个几何图形。当您将多边形转换为
sf
时而不是 sfc
,这也意味着你得到 n.overlaps
和 origins
描述每个几何图形在原始输入中来自何处的列。然后您可以简单地过滤并查看重叠区域已被删除。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"])
创建于 2020-07-21 由 reprex package (v0.3.0)
关于R SF : Find polygons outside of multiple overlapping polygons,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63024545/