为什么 distHaversine return NaN 对于某些坐标对?

Why does distHaversine return NaN for some pairs of coordinates?

我正在尝试计算坐标列表中所有可能的坐标对之间的距离。但是,对于某些坐标对,我出乎意料地得到了 NaN,您绝对应该能够计算出它们之间的距离。

我的坐标是十进制经纬度。我正在使用 R 中 geodist 包中的 distHaversine 函数。

这是 distHaversine returns NaN 的一对坐标示例。我用许多其他坐标对尝试了这段代码,它工作正常。

# Create long, lat matrix with two sets of coordinates
coord_list <- matrix(c(2.5, -177.5, 5.5, -5.5), ncol=2)
coord_list

# Create a matrix with the distance between each pair of points (zero on the diagonals)
dist <- apply(coord_list, 1, 
              FUN=function(X) {
                distHaversine(X, coord_list)
              })
dist
#    [,1] [,2]
#[1,]    0  NaN
#[2,]  NaN    0

如果相关,我需要这些距离作为空间加权回归的反距离加权矩阵。但是,我宁愿弄清楚为什么 distHaversine 偶尔会返回 NaN 而不是以不同的方式计算矩阵(我知道该怎么做)。

感谢您的帮助!

感谢您的报告。这已在 geosphere 1.5-7 中修复。

我会使用非常精确的 geosphere::distGeo 函数来代替 geopshere::distHaversine(这更符合历史兴趣)

要获取所有点到所有点的距离,可以使用distm函数

coords <- matrix(c(2.5, -177.5, 5.5, -5.5, 0, 0), ncol=2)

library(geosphere)
distm(coords)

#           [,1]     [,2]       [,3]
#[1,]        0.0 19395754   693590.1
#[2,] 19395754.2        0 19703549.9
#[3,]   693590.1 19703550        0.0

或者raster包中的pointDistance函数(算法和distGeo一样):

library(raster)
pointDistance(coords, lonlat=TRUE)

#           [,1]     [,2] [,3]
#[1,]        0.0       NA   NA
#[2,] 19395754.2        0   NA
#[3,]   693590.1 19703550    0