本文介绍了分割单个SpatialPolygons对象的多边形部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在R中,我有一个包含数百个多边形的SpatialPolygons对象(即多多边形).我想将此SpatialPolygons对象拆分为Polygons的列表(即孔应保持与父多边形相连).

In R, I have single SpatialPolygons object (i.e. multi-polygons) containing several hundred polygons. I would like to split this SpatialPolygons object into a list of Polygons (i.e. holes should remain attached to the parent polygon).

有什么想法吗?

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

Using the following example provided in the sp package:

# 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).

Then running out = lapply(SpP@polygons, slot, "Polygons"). I get a list of three Polygons (i.e. Srs1, Srs2, Srs3).

但是,我要解决的情况与此示例有些不同.我尝试拆分的SpatialPolygons对象是使用gUnaryUnion函数(在RGEOS包中)完成的几何并集的结果.如果我应用out <- lapply(merged.polygons@polygons, slot, "Polygons"),则会得到Polygon对象的唯一列表(n.b.不是Polygons对象的列表).换句话说,每个多边形都与其孔分开.

However, the case I am trying to solve is a bit different from this example. The SpatialPolygons object I am trying to split is the result of a geometric union done with the gUnaryUnion function (in the RGEOS package). If I apply out <- lapply(merged.polygons@polygons, slot, "Polygons"), I get a unique list of Polygon objects (n.b. not a list of Polygons objects). In other words, each polygon is separated from its hole(s).

运行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 ):

According to the RGEOS v0.3-2 manual (http://cran.r-project.org/web/packages/rgeos/rgeos.pdf):

因此RGEOS中的createSPComment()函数可能是重新聚合多边形和孔的一种解决方法.

So the createSPComment() function in RGEOS is likely to be a workaround to reaggregate Polygons and holes.

推荐答案

要将多面对象分离为单个多边形(如果有孔,则可以有孔)

To separate multipolygon objects into single polygons (with holes if present) you can do

d <- disaggregate(p)

其中pSpatialPolygons对象.之后,您可以使用d@polygons.

Where p is a SpatialPolygons object. After that, you can use 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)

这篇关于分割单个SpatialPolygons对象的多边形部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-10 21:35