gpt4 book ai didi

r - 组合 Voronoi 多边形和 map

转载 作者:行者123 更新时间:2023-12-02 02:52:32 25 4
gpt4 key购买 nike

我想将 Voronoi 多边形与 map 结合起来,以便稍后使用它进行空间分析。我有一些想要组合的点和形状文件,然后保存为形状文件/空间多边形。为了获得 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))

我的 map 可以在这里找到: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)

结果: enter image description here

我想将这个 voronoi 多边形作为新的空间多边形放入我的 map 中。

如果您能提供答案,我将不胜感激!

编辑:

要明确的是,我想实现这样的目标(这条线应该从 voronoi 多边形创建):

enter image description here

最佳答案

稍微修改了函数,采用额外的空间多边形参数并扩展到该框:

voronoipolygons <- function(x,poly) {
require(deldir)
if (.hasSlot(x, 'coords')) {
crds <- x@coords
} else crds <- x
bb = bbox(poly)
rw = as.numeric(t(bb))
z <- deldir(crds[,1], crds[,2],rw=rw)
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'))))

return(voronoi)

}

然后执行:

pzn.coords<-voronoipolygons(coords,pznall)
library(rgeos)
gg = gIntersection(pznall,pzn.coords,byid=TRUE)
plot(gg)

请注意,gg 是一个 SpatialPolygons 对象,您可能会收到有关 proj4 字符串不匹配的警告。您可能需要将 proj4 字符串分配给一个或其他对象。

关于r - 组合 Voronoi 多边形和 map ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12156475/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com