R数据点周长缓冲区
问题描述:
我需要在带有x和y坐标(图上的灰点)的data points集上创建一个缓冲区。 不幸的是,我没有从中创建缓冲区的点的周边边界。 我试图用chull
函数来计算周长,但是它不能正常工作(橙色区域)。 我可以使用max
/min
函数来计算边界点,通过某个步骤(比如说10米,红点)来计算数据,并尝试从这些点计算缓冲区。 有人知道更正确和干净的方法来计算点集的缓冲区。R数据点周长缓冲区
答
非常感谢您的建议和意见。 确实,这是我的错,省略了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)
答
你可以做点周围镶嵌。边界上的点将具有更大的多边形。
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()
是红线,你正在寻找的结果? – Thierry
我想检索所有落在由红线标识的多边形内的点。 – Vova
'chull'工作正常。你只是不想让你的船体“凸出”。 –