【问题标题】:How to make MCP polygon from points in R如何从R中的点制作MCP多边形
【发布时间】:2014-07-07 11:54:02
【问题描述】:

我在尝试从 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)

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

【问题讨论】:

    标签: r polygon point


    【解决方案1】:

    您可以只使用基本 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,])
    

    【讨论】:

    • 这正是我所需要的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-20
    • 2015-11-03
    • 2013-02-02
    • 2016-04-08
    • 1970-01-01
    • 2013-10-03
    相关资源
    最近更新 更多