如何从R中的点制作MCP多边形

时间:2014-05-19 13:31:08

标签: r polygon point

尝试从R 中的多个点创建 MCP时出现问题。

library(shapefiles)

# no problem when only three points...
dd <- data.frame(Id=c(1,1,1,1),X=c(3,5,8,3),Y=c(9,8,3,9))
ddTable <- data.frame(Id=c(1),Name=c("Item1"))
ddShapefile <- convert.to.shapefile(dd, ddTable, "Id", 5)
write.shapefile(ddShapefile, "/directory.../pgn_test", arcgis=T)
my.pgn <- readOGR("/directory...","pgn_test")
plot(my.pgn)
points(dd$X, dd$Y, cex = 0.7, pch = 1)

上面的代码只有三个点才能正常工作 ,但在我的情况下有很多点...

# when some points are inside the polygon
dd <- data.frame(Id=c(rep(1, times = 6)),X=c(1,2,3,5,5,1),Y=c(1,5,3,5,1,1))
ddTable <- data.frame(Id=c(1),Name=c("Item1"))
ddShapefile <- convert.to.shapefile(dd, ddTable, "Id", 5)
write.shapefile(ddShapefile, "/directory.../pgn_test", arcgis=T)
my.pgn <- readOGR("/directory...","pgn_test")
plot(my.pgn)
points(dd$X, dd$Y, cex = 0.7, pch = 1)

enter image description here

有谁知道如何解决这种情况?

1 个答案:

答案 0 :(得分:4)

您可以使用基本R函数chull(),它“计算位于指定点集的凸包上的点子集”:

dd <- data.frame(X = c(1,2,3,5,5,1), Y = c(1,5,3,5,1,1))

ii <- with(dd, chull(X,Y))
ii <- c(ii, ii[1])

plot(Y~X, data=dd)
lines(Y~X, data=dd[ii,])

enter image description here