如何使用 R 包从点创建泰森多边形? [英] How to create thiessen polygons from points using R packages?

查看:29
本文介绍了如何使用 R 包从点创建泰森多边形?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有多组积分(不同年份~20)

I have multiple sets of points (for different years ~20)

我想使用 r 个空间包为每组点生成泰森多边形.

I want to generate thiessen polygons for each set of points using r spatial packages.

我知道这可以使用 GIS 来完成,但是因为我想要一个批处理,所以 R 中的东西会是

I know this can be done using GIS but as i want a batch process something in R would be

有帮助.

推荐答案

您还没有让我们访问您的数据,但这里有一个代表世界城市的点的示例,使用 Carson Farmer 在 他的博客.希望它能让你开始......

You haven't given us access to your data, but here's an example for points representing cities of the world, using an approach described by Carson Farmer on his blog. Hopefully it'll get you started...

# Carson's Voronoi polygons function
voronoipolygons <- function(x) {
  require(deldir)
  require(sp)
  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))
  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'))))
}

示例 1:输入为 SpatialPointsDataFrame:

# Read in a point shapefile to be converted to a Voronoi diagram
library(rgdal)
dsn <- system.file("vectors", package = "rgdal")[1]
cities <- readOGR(dsn=dsn, layer="cities")

v <- voronoipolygons(cities)

plot(v)

示例 2:输入是 x、y 坐标的向量:

dat <- data.frame(x=runif(100), y=runif(100))
v2 <- voronoipolygons(dat)
plot(v2)

这篇关于如何使用 R 包从点创建泰森多边形?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆