【发布时间】:2012-08-22 19:07:10
【问题描述】:
我想将 Voronoi 多边形与地图结合起来,以便稍后将其用于空间分析。我有许多点和要组合的 shapefile,然后保存为 shapefile/空间多边形。要获得 voronoi 多边形,我使用来自 this topic 的函数。
我的代码如下:
coords<-data.frame(LONG=c(16.9252,16.9363,16.9408,16.8720,16.9167,16.9461,16.9093,16.9457,16.9171,16.8506,16.9471,16.8723,16.9444,16.9212,16.8809,16.9191,16.8968,16.8719,16.9669,16.8845),
LAT=c(52.4064,52.4266,52.3836,52.3959,52.4496,52.3924,52.4012,52.3924,52.3777,52.4368,52.4574,52.3945,52.4572,52.3962,52.3816,52.3809,52.3956,52.3761,52.4236,52.4539))
我的地图在这里:https://docs.google.com/file/d/0B-ZJyVlQBsqlSURiN284dF9YNUk/edit
library(rgdal)
voronoipolygons <- function(x) {
require(deldir)
if (.hasSlot(x, 'coords')) {
crds <- x@coords
} else crds <- x
z <- deldir(crds[,1], crds[,2])
w <- tile.list(z)
polys <- vector(mode='list', length=length(w))
require(sp)
for (i in seq(along=polys)) {
pcrds <- cbind(w[[i]]$x, w[[i]]$y)
pcrds <- rbind(pcrds, pcrds[1,])
polys[[i]] <- Polygons(list(Polygon(pcrds)), ID=as.character(i))
}
SP <- SpatialPolygons(polys)
voronoi <- SpatialPolygonsDataFrame(SP, data=data.frame(x=crds[,1],
y=crds[,2], row.names=sapply(slot(SP, 'polygons'),
function(x) slot(x, 'ID'))))
}
还有我获取 voronoipolygons 的代码:
pzn.coords<-voronoipolygons(coords)
plot(pznall)
plot(pzn.coords,add=T)
points(coords$LONG,coords$LAT)
结果:
我想在我的地图中使用这个 voronoi 多边形作为新的空间多边形。
我将不胜感激!
编辑:
明确地说,我想实现这样的目标(这条线应该由 voronoi 多边形创建):
【问题讨论】:
-
不清楚你想要什么。您将 voronoi 多边形作为 pzn.coords 对象中的 spatialpolygonsdataframe。
-
您提供的链接是
.rdata文件。您可以使用save()创建一个新的myvoronietc.rdata文件,例如save('pzn.coords','lotsa_other_data',file='myvoronietc.rdata'). -
我想合并这两个文件,在我的地图中包含 voronoi 多边形,而不是在我的地图边界中包含 voronoi 多边形的正方形边界。
-
您必须将 voronoi 多边形扩展为大于您的多边形,可能通过向 deldir 添加虚拟点,然后使用 rgeos 进行剪辑。也许稍后我会做一个完整的答案,或者自己尝试一下。
-
只是一个评论(不是答案)该行中有一个错字: rw = as.numeric(t(bbox(pznall))) 用 poly 替换 pznall 感谢您的修改,干杯,杰德