简体   繁体   English

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

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

I have a series of polygons and points with each polygon containing a point. 我有一系列的多边形和点,每个多边形都包含一个点。 I want to determine the maximum distance of each point to the edge of the polygon containing it is contained within in R. 我想确定R中包含每个点到包含它的多边形边缘的最大距离。 具有内部点的示例多边形

I looked at using the rgeos gDistance function but this returns 0 for points within polygons. 我看过使用rgeos gDistance函数,但是对于多边形内的点,它返回0。

Using an example polygon and a point that falls within the polygon this is what i've coded so far but i'm getting a distance of 0 rather than the distance from a point to polygon edges. 到目前为止,我已经使用一个示例多边形和一个落入该多边形的点进行了编码,但是我得到的距离为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

Does a function exist in R or an R package that can determine distances for points within polygons to the polygon edge? R或R包中是否存在可以确定多边形内点到多边形边缘的距离的函数?

Much appreciated within advance. 非常感谢提前。

Solution using spatstat and the built-in dataset chorley : 使用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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM