【问题标题】:How to get the max distance from a point within a polygon in R如何从R中的多边形内的点获得最大距离
【发布时间】:2020-01-06 17:59:35
【问题描述】:

我有一系列多边形和点,每个多边形都包含一个点。我想确定每个点到包含它的多边形边缘的最大距离包含在 R 中。

我查看了使用 rgeos gDistance 函数,但对于多边形内的点,它返回 0。

使用示例多边形和多边形内的一个点,这是我迄今为止编码的内容,但我得到的距离为 0,而不是从一个点到多边形边缘的距离。

pt1 = readWKT("POINT(0.5 0.25)")
p1 = readWKT("POLYGON((0 0,1 0,1 1,0 1,0 0))")

gDistance(pt1, p1)
# 0

R 或 R 包中是否存在可以确定多边形内的点到多边形边缘的距离的函数?

提前非常感谢。

【问题讨论】:

  • 不是r 特定的,但一般来说,为什么不遍历定义多边形的每个点,并测量到多边形内点的欧几里得距离?并且只需跟踪您遇到的最大距离
  • 我认为@jean 是对的,因为最远的点总是角落之一。
  • @jean 好主意!我会试一试,谢谢

标签: r spatial


【解决方案1】:

使用spatstat和内置数据集chorley的解决方案:

library(spatstat)
W <- Window(chorley) # Polygonal window of the choley dataset
p <- list(x = c(350, 355), y = c(415, 425)) # Two points in polygon
plot(W, main = "")
points(p, col = c("red", "blue"), cex = 1.5)

v <- vertices(W) # Polygon vertices
d <- crossdist(v$x, v$y, p$x, p$y) # 2-column matrix of cross distances
i1 <- which.max(d[,1]) # Index of max dist for first (red) point
i2 <- which.max(d[,2]) # Index of max dist for second (blue) point

plot(W, main = "")
points(p, col = c("red", "blue"), cex = 1.5)
points(v$x[c(i1,i2)], v$y[c(i1,i2)], col = c("red", "blue"), cex = 1.5)

d[i1,1] # Max dist for first (red) point
#> [1] 21.35535
d[i2,2] # Max dist for second (blue) point
#> [1] 15.88226

【讨论】:

    猜你喜欢
    • 2010-10-28
    • 2020-06-17
    • 2013-05-03
    • 2021-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-03
    • 2020-05-10
    相关资源
    最近更新 更多