如何使用R包从点创建泰森多边形?

时间:2022-09-23 12:15:29

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

我有多个点(不同年份~20)

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

我想用r空间包为每一组点生成thiessen多边形。

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

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

helpful.

有帮助的。

2 个解决方案

#1


16  

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 Farmer在他的博客中描述的方法。希望它能让你开始……

# 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'))))
}

Example 1: Input is a SpatialPointsDataFrame:

示例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)

如何使用R包从点创建泰森多边形?

Example 2: Input is vectors of x, y coordinates:

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

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

如何使用R包从点创建泰森多边形?

#2


1  

Same principle as shown by jbaums, but simpler code:

与jbaums相同的原则,但是更简单的代码:

library(dismo)
library(rgdal)
cities <- shapefile(file.path(system.file("vectors", package = "rgdal")[1], "cities"))

v <- voronoi(cities)
plot(v)

#1


16  

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 Farmer在他的博客中描述的方法。希望它能让你开始……

# 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'))))
}

Example 1: Input is a SpatialPointsDataFrame:

示例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)

如何使用R包从点创建泰森多边形?

Example 2: Input is vectors of x, y coordinates:

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

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

如何使用R包从点创建泰森多边形?

#2


1  

Same principle as shown by jbaums, but simpler code:

与jbaums相同的原则,但是更简单的代码:

library(dismo)
library(rgdal)
cities <- shapefile(file.path(system.file("vectors", package = "rgdal")[1], "cities"))

v <- voronoi(cities)
plot(v)