如何扩展多边形以到达r中的附近线?

时间:2016-06-10 00:33:27

标签: r polygon spatial

假设我有一个包含3个多边形数据名groupexc的SpatialPolygons对象:

library(raster) 
p1 <- matrix(c(2, 3, 4, 5, 6, 5, 4, 3, 2, 4, 5, 6, 5, 4, 3, 2, 3, 4), ncol=2)
p2 <- matrix(c(8, 9, 10, 11, 12, 11, 10, 9, 8, 4, 5, 6, 5, 4, 3, 2, 3, 4), ncol=2)
p3 <- matrix(c(5, 6, 7, 8, 9, 8, 7, 6, 5, 9, 10, 11, 10, 9, 8, 7, 8, 9), ncol=2)
groupexc <- spPolygons(p1, p2, p3)

代表单个区域的SpatialPolygons对象zoneexc

zoneexc = spPolygons(matrix(c(2,1,3,4,6,8,10,13,14,14,12,10,8,6,4,2,1,3,7,10,12,14,12,6,4,3,1,1,1,1,1,1), ncol=2))

我有没有办法将输出从groupexc扩展到zoneexc达到分数?

plot(zoneexc, border='red', lwd=3)
plot(groupexc, add=TRUE, border='blue', lwd=2)
text(groupexc, letters[1:3]) 

enter image description here

后:

enter image description here

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:3)

这是一个近似的解决方案。这种方法可能会因大问题而中断,这取决于每个多边形中是否有足够数量的节点。但它可能足以满足您的目的。

# example data 
library(raster) 
p1 <- matrix(c(2, 3, 4, 5, 6, 5, 4, 3, 2, 4, 5, 6, 5, 4, 3, 2, 3, 4), ncol=2)
p2 <- matrix(c(8, 9, 10, 11, 12, 11, 10, 9, 8, 4, 5, 6, 5, 4, 3, 2, 3, 4), ncol=2)
p3 <- matrix(c(5, 6, 7, 8, 9, 8, 7, 6, 5, 9, 10, 11, 10, 9, 8, 7, 8, 9), ncol=2)
groups <- spPolygons(p1, p2, p3, attr=data.frame(name=c('a', 'b', 'c')))
zone <- spPolygons(matrix(c(2,1,3,4,6,8,10,13,14,14,12,10,8,6,4,2,1,3,7,10,12,14,12,6,4,3,1,1,1,1,1,1), ncol=2))

现在创建最近邻多边形。要使其工作如下,您需要 dismo版本1.1-1 (或更高版本)

library(dismo)
# get the coordinates of the polygons    
g <- unique(geom(groups))
v <- voronoi(g[, c('x', 'y')], ext=extent(zone))
# plot(v)
# assign group id to the new polygons
v$group <- g[v$id, 1]

# aggregate (dissolve) polygons by group id
a <- aggregate(v, 'group')
# remove areas outside of the zone
i <- crop(a, zone)

# add another identifier 
i$name <- groups$name[i$group]

plot(i, col=rainbow(3))
text(i, "name", cex=2)
plot(groups, add=TRUE, lwd=2, border='white', lty=2)

white lines show original areas, now expanded to fill up the zone

要了解它的工作原理:

points(g[, c('x', 'y')], pch=20, cex=2)
plot(v, add=TRUE)