r - 拆分单个 SpatialPolygons 对象的多边形部分

标签 r spatial polygons

在 R 中,我有一个 SpatialPolygons包含数百个多边形的对象(即多多边形)。我想拆分这个 SpatialPolygons对象放入 Polygons 的列表中(即孔应保持连接到父多边形)。

知道如何做到这一点吗?

编辑:

使用 sp 中提供的以下示例包:

# simple example, from vignette("sp"):
Sr1 = Polygon(cbind(c(2,4,4,1,2),c(2,3,5,4,2)))
Sr2 = Polygon(cbind(c(5,4,2,5),c(2,3,2,2)))
Sr3 = Polygon(cbind(c(4,4,5,10,4),c(5,3,2,5,5)))
Sr4 = Polygon(cbind(c(5,6,6,5,5),c(4,4,3,3,4)), hole = TRUE)

Srs1 = Polygons(list(Sr1), "s1")
Srs2 = Polygons(list(Sr2), "s2")
Srs3 = Polygons(list(Sr3, Sr4), "s3/4")
SpP = SpatialPolygons(list(Srs1,Srs2,Srs3), 1:3)

然后运行 ​​out = lapply(SpP@polygons, slot, "Polygons") .我得到了三个列表 Polygons (即 Srs1Srs2Srs3 )。

但是,我试图解决的案例与此示例略有不同。 SpatialPolygons我试图分割的对象是用 gUnaryUnion 完成的几何联合的结果函数(在 RGEOS 包中)。如果我申请 out <- lapply(merged.polygons@polygons, slot, "Polygons") , 我得到一个唯一的列表 Polygon对象(注意不是 Polygons 对象的列表)。换句话说,每个多边形与其孔分开。

运行 topol <- sapply(unlist(out), function(x) x@hole)
我得到:
> length(topol)
[1] 4996


> sum(topol, na.rm=TRUE)
[1] 469

根据RGEOS v0.3-2 手册 ( http://cran.r-project.org/web/packages/rgeos/rgeos.pdf ):

In order for rgeos to function properly it is necessary that all holes within a given POLYGON or MULTIPOLYGON geometry must belong to a specific polygon. The SpatialPolygons class implementation does not currently include this information. To work around this limitation rgeos uses an additional comment attribute on the Polygons class that indicates which hole belongs to which polygon. Under the current implementation this comment is a text string of numbers separated by spaces where the order of the numbers corresponds to the order of the Polygon objects in the Polygons slot of the Polygons object. A 0 implies the Polygon object is a polygon, a non-zero number implies that the Polygon object is a hole with the value indicating the index of the Polygon that “owns” the hole.



所以createSPComment()函数在 RGEOS可能是重新聚合多边形和孔的解决方法。

最佳答案

要将多多边形对象分成单个多边形(如果存在孔),您可以执行以下操作

d <- disaggregate(p)

哪里pSpatialPolygons对象。之后,您可以使用 d@polygons .

例如
library(sp)
library(raster)
### example data
p1 <- rbind(c(-180,-20), c(-140,55), c(10, 0), c(-140,-60), c(-180,-20))
hole <- rbind(c(-150,-20), c(-100,-10), c(-110,20), c(-150,-20))
p1 <- list(p1, hole)
p2 <- rbind(c(-10,0), c(140,60), c(160,0), c(140,-55), c(-10,0))
p3 <- rbind(c(-125,0), c(0,60), c(40,5), c(15,-45), c(-125,0))
pols <- spPolygons(p1, p2, p3)
###

a <- aggregate(pols, dissolve=FALSE)
d <- disaggregate(a)

关于r - 拆分单个 SpatialPolygons 对象的多边形部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19315832/

相关文章:

r - 为什么 rasterToPoints 在第一次调用而不是第二次调用时生成错误?

r - 如何在箱线图中为每个组绘制额外的统计数据?

mysql - 空间索引减慢查询mysql

algorithm - 将多边形分成 2 个相等的部分

r - R 中的相关性,当我执行 "pairwise.complet.obs"时出现错误 "standard deviation is 0"

r - 查找矩阵中非零元素的索引

r - 将 OS National Grid 名称/代码添加到 R 中的网格

mysql - 包含 MySQL 函数 - 意外结果

unity-game-engine - Unity3D 带孔动态网格

python - 在 Python 中将多多边形转换为多边形