如何从R中的多边形内的点获取最大距离

How to get the max distance from a point within a polygon in R

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

我查看了使用 rgeos gDistance 函数,但是对于多边形内的点,此 returns 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 包中是否存在可以确定多边形内的点到多边形边的距离的函数?

提前感谢。

使用 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