R中有X个函数来计算两个多边形之间的最短距离,但是我似乎找不到一种使函数返回该距离在两个位置之间的方法?!
假设多边形A和B之间的距离为10个单位。我的问题是,我可以在A和B的两个点之间绘制10个单位的线。
圣诞节的所有食物都阻塞了我的大脑吗?
sf::st_nearest_points()
将完成此工作。下面是带有玩具数据的示例:
library(sf)
p1 <- matrix(c(0, 0, 1, 0, 1, 1, 0, 1, 0, 0), ncol = 2, byrow = TRUE)
p2 <- p1 + 3
pts1 <- list(p1)
pts2 <- list(p2)
poly1 <- st_polygon(pts1)
poly2 <- st_polygon(pts2)
near_points <- st_nearest_points(poly1, poly2)
近点返回一个带有两个点(1,1)和(3,3)的SF LINESTRING对象:
Geometry set for 1 feature geometry type: LINESTRING dimension: XY bbox: xmin: 1 ymin: 1 xmax: 3 ymax: 3 epsg (SRID): NA proj4string: NA LINESTRING (1 1, 3 3)
ggplot() + geom_sf(data = poly1, color = 'blue') +
geom_sf(data = poly2, color = 'orange') +
geom_sf(data = near_points, color = 'red')
[
st_distance()
将返回两个最近点之间的距离。
st_distance(poly1, poly2) [,1] [1,] 2.828427