计算大圆距离矩阵 [英] Calculating great-circle distance matrix
问题描述
dist(coords)
使用欧几里得距离提供距离矩阵;它还提供了其他几种选择.但是它没有提供任何选项,例如Haversine公式.
dist(coords)
provides the distance matrix using Euclidean distances; it also provides several other options. But it doesn't provide any option such as the haversine formula.
distHaversine()
对于给定的两组经/纬度坐标,计算我想要的距离(大圆).我想知道是否存在使用Haversine公式计算大圆距离矩阵的软件包/函数.
distHaversine()
calculates the distance I want (great-circle) for given two set of lat/long coordinates. I am wondering if there is an existing package/function that calculates great-circle distance matrix using the haversine formulation.
推荐答案
您可能已经注意到,distHaversine()
将计算单点和两列坐标矩阵之间的距离.
As you may already have noticed, distHaversine()
will compute the distance between a single point and a two-column matrix of coordinates.
要计算两个坐标矩阵之间的所有成对距离,只需使用apply()
逐行迭代一个矩阵,计算其每个点到所有点的距离在另一个.
To compute all pairwise distances between two coordinate matrices, just use apply()
to iterate row-by-row through one of the matrices, computing each of its points' distance to all of the points in the other.
library(geosphere)
## Example coordinates (here stored in two column matrices)
cc1 <- rbind(c(0,0),c(1,1))
cc2 <- rbind(c(90,0),c(90,90), c(45,45))
## Compute matrix of distances between points in two sets of coordinates
apply(cc1, 1, FUN=function(X) distHaversine(X, cc2))
# [,1] [,2]
# [1,] 10018754 9907452
# [2,] 10018754 9907435
# [3,] 6679169 6524042
有趣的注意事项:快速浏览sp::spDists()
的内容( 计算两个矩阵之间的成对距离)表明它使用了实质上相同的apply()
基于策略.除了一些附加的错误检查和参数传递之外,主要区别在于它在应用distHaversine()
的地方应用了函数spDistsN1()
.
Interesting note: A quick glance under the hood at sp::spDists()
(which does compute pairwise distances between two matrices) reveals that it uses an essentially identical apply()
-based strategy. The main difference, beyond some additional error checking and argument passing is that it applies the function spDistsN1()
where we apply distHaversine()
.
这篇关于计算大圆距离矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!