有效提取由MultiPolygon中的自相交特征生成的所有子多边形 [英] Efficient extraction of all sub-polygons generated by self-intersecting features in a MultiPolygon
问题描述
从一个包含相当多(大约20000个)可能部分重叠的多边形的shapefile开始,我需要提取所有与它们不同的边界"相交而来的子多边形.
Starting from a shapefile containing a fairly large number (about 20000) of potentially partially-overlapping polygons, I'd need to extract all the sub-polygons originated by intersecting their different "boundaries".
在实践中,从一些模型数据开始:
In practice, starting from some mock-up data:
library(tibble)
library(dplyr)
library(sf)
ncircles <- 9
rmax <- 120
x_limits <- c(-70,70)
y_limits <- c(-30,30)
set.seed(100)
xy <- data.frame(
id = paste0("id_", 1:ncircles),
x = runif(ncircles, min(x_limits), max(x_limits)),
y = runif(ncircles, min(y_limits), max(y_limits))) %>%
as_tibble()
polys <- st_as_sf(xy, coords = c(2,3)) %>%
st_buffer(runif(ncircles, min = 1, max = 20))
plot(polys[1])
我需要导出一个sf
或sp
多边形,其中包含ALL,并且仅包含由交集生成的多边形,例如:
I'd need to derive an sf
or sp
multipolygon containing ALL and ONLY the polygons generated by the intersections, something like:
(请注意,这里的颜色只是为了说明预期的结果,其中每个不同颜色"的区域都是一个单独的多边形,不覆盖任何其他多边形)
(note that the colors are there just to exemplify the expected result, in which each "differently colored" area is a separate polygon which doesn't overlay any other polygon)
我知道我可以通过一次分析一个多边形,识别并保存其所有交点,然后擦除"这些区域形成完整的多面体并循环进行来解决问题,但是这很慢.
I know I could work my way out by analyzing one polygon at a time, identifying and saving all its intersections and then "erase" those areas form the full multipolygon and proceed in a cycle, but that is quite slow.
我觉得应该为此提供一个更有效的解决方案,但我无法弄清楚,因此将不胜感激!
(欢迎使用基于sf
和sp
的解决方案)
I feel there should be a more efficient solution for this, but I am not able to figure it out, so any help would be appreciated!
(Both sf
and sp
based solutions are welcome)
更新:
最后,我发现即使一次一个多边形",任务也绝非易事!我真的很努力解决这个看似简单"的问题!有什么提示吗?甚至慢速的解决方案或提示在正确的路径上开始的提示也将不胜感激!
In the end, I found out that even going "one polygon at a time" the task is far from simple! I'm really struggling on this apparently "easy" problem! Any hints? Even a slow solution or hints for starting on a proper path would be appreciated!
更新2 :
也许这可以使事情变得澄清:所需的功能将类似于此处描述的功能:
Maybe this will clarify things: the desired functionality would be similar to the one described here:
更新3 :
我将赏金授予了@ shuiping-chen(谢谢!),他的回答正确地解决了所提供的示例数据集上的问题.但是,必须将方法"推广到可能存在四重"或"n-uple"相交的情况.我会在未来几天内尝试解决这个问题,如果可以的话,请发布一个更通用的解决方案!
I awarded the bounty to @shuiping-chen (thanks !), whose answer correctly solved the problem on the example dataset provided. The "method" has however to be generalized to situations were "quadruple" or "n-uple" intersections are possible. I'll try to work on that in the coming days and post a more general solution if I manage !
推荐答案
当使用单个参数(sf或sfc)调用st_intersection
时,这已在R包sf中作为默认结果实现,请参见 https://r-spatial.github.io/sf/reference/geos_binary_ops.html 作为示例. (我不确定origins
字段是否包含有用的索引;理想情况下,它们应该仅指向x
中的索引,现在它们是自引用的.)
This has now been implemented in R package sf as the default result when st_intersection
is called with a single argument (sf or sfc), see https://r-spatial.github.io/sf/reference/geos_binary_ops.html for the examples. (I'm not sure the origins
field contains useful indexes; ideally they should point to indexes in x
only, right now they kind of self-refer).
这篇关于有效提取由MultiPolygon中的自相交特征生成的所有子多边形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!