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

时间:2019-09-03 20:21:29

标签: r spatial

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

我看过使用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包中是否存在可以确定多边形内点到多边形边缘的距离的函数?

非常感谢。

1 个答案:

答案 0 :(得分:0)

使用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
相关问题