如何使用 distHaversine 函数?

ale*_*exk 3 r distance

我试图在 R 中使用 distHavrsine 函数,在循环内计算几百行的某些纬度和经度坐标之间的距离。在我的循环中,我有这个代码:

if ((distHaversine(c(file[i,"long"], file[i,"lat"]),
                   c(file[j,"long"], file[j,"lat"]))) < 50 )
Run Code Online (Sandbox Code Playgroud)

之后,如果距离小于 50 米,我希望它记录这些行,以及它所引用的纬度和经度坐标的位置:

0.492399367 30.42530045
Run Code Online (Sandbox Code Playgroud)

0.496899361 30.42497045
Run Code Online (Sandbox Code Playgroud)

但我收到这个错误

.pointsToMatrix(p1) 中的错误:纬度 > 90

luk*_*keA 5

我收到此错误“.pointsToMatrix(p1) 中的错误:纬度 > 90”。谁能解释为什么以及如何解决?

该错误告诉您纬度值大于 90,这超出了范围:

library(geosphere)
distHaversine(c(4,52), c(13,52))
# [1] 616422
distHaversine(c(4,52), c(1,91))
# Error in .pointsToMatrix(p2) : latitude > 90
Run Code Online (Sandbox Code Playgroud)

您可以通过仅distHaversine输入可接受范围内的坐标来解决此问题。

我试图在 R 中使用 distHavrsine 函数,在循环内计算几百行的某些纬度和经度坐标之间的距离。(...) 如果距离小于 50 米,我希望它记录这些行

看看这个distm函数,它可以轻松地计算几百行的距离矩阵(即没有循环)。它distHaversine默认使用。例如,要获取距离 650000 米更近的数据框行:

df <- read.table(sep=",", col.names=c("lon", "lat"), text="
4,52
13,52 
116,39")
(d <- distm(df))
#         [,1]    [,2]    [,3]
# [1,]       0  616422 7963562
# [2,]  616422       0 7475370
# [3,] 7963562 7475370       0

d[upper.tri(d, T)] <- NA
( idx <- which(d < 650000, arr.ind = T) )
#      row col
# [1,]   2   1
cbind(df[idx[, 1], ], df[idx[, 2], ])
#   lon lat lon lat
# 2  13  52   4  52
Run Code Online (Sandbox Code Playgroud)