R数据点周长缓冲区

时间:2015-11-02 16:02:48

标签: r computational-geometry

我需要在data points的集合上创建一个缓冲区,其中包含x和y坐标(图中的灰点)。 不幸的是,我没有点的边界边界,从中创建缓冲区。 我试图使用chull函数计算周长,但它无法正常工作(橙色区域)。 我可以使用max / min函数为数据计算边界点一些步骤(比如说10米,红点),并尝试从这些点计算缓冲区。 是否有人知道更正确和干净的方法来计算点集的缓冲区。

enter image description here

2 个答案:

答案 0 :(得分:1)

你可以围绕这些点进行一次细分。边界处的点将具有更大的多边形。

library(deldir)
library(ggplot2)
triang <- deldir(data$x, data$y)
border <- triang$summary
border$Selected <- border$dir.area > 260
ggplot(border[order(border$Selected), ], aes(x = x, y = y, colour = Selected)) + geom_point()

答案 1 :(得分:0)

非常感谢您的建议和意见。 实际上,省略alphahull包是我的错。

ashape识别边界后,我创建了一个缓冲区多边形,并识别出缓冲区内外的数据。挑战是从ashap正确提取多边形,但RPubs的解决方案让我安全。 您还可以看到图形示例here.

最佳

## load
library(ggplot2); library(alphahull); 
library(igraph); library(rgeos)
## Load the data
data.df<-read.csv("Data/Cencus/Lyford_meta.csv",sep=",",header=TRUE)

#Remove the duplicates in the data to do the chull calculation
data <- data.df[!duplicated(paste(data.df$xsite, data.df$ysite, sep ="_")), c("xsite","ysite") ]

#calculate the chull with alpha 20
data.chull <- ashape(data, alpha = 20)


## Below is the code to extract polygon from the ashape chull function 
## credit to: http://rpubs.com/geospacedman/alphasimple
order.chull <- graph.edgelist(cbind(as.character(data.chull$edges[, "ind1"]), as.character(data.chull$edges[,"ind2"])), directed = FALSE)
cutg <- order.chull - E(order.chull)[1]
ends <- names(which(degree(cutg) == 1))
path <- get.shortest.paths(cutg, ends[1], ends[2])[[1]]
pathX <- as.numeric(V(order.chull)[unlist(path[[1]])]$name)
pathX = c(pathX, pathX[1])
data.chull <- as.data.frame(data.chull$x[pathX, ])


## Create a spatial object from the polygon and apply a buffer to
## Then extract the data to the dataframe.
data.chull.poly <- SpatialPolygons(list(Polygons(list(Polygon(as.matrix(data.chull))),"s1")))
data.chull.poly.buff <- gBuffer(data.chull.poly, width = -10)
data.buffer <- fortify(data.chull.poly.buff)[c("long","lat")]

## Identidfy the data that are inside the buffer polygon
data$posit <- "Outside"
data$posit[point.in.polygon(data$x,data$y,data.buffer$long,data.buffer$lat) %in% c(1,2,3)] <- "Inside"


## Plot the results
ggplot()+
  theme_bw()+xlab("X coordinates (m)")+ylab("Y coordinates (m)") +
  geom_point(data = data, aes(xsite, ysite, color = posit))+
  geom_polygon(data = data.chull, aes(V1, V2), color = "black", alpha = 0)+
  geom_polygon(data = data.buffer, aes(long, lat), color = "blue", alpha = 0)