r - 如何使用 distHaversine 函数?

标签 r distance

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

if ((distHaversine(c(file[i,"long"], file[i,"lat"]),
                   c(file[j,"long"], file[j,"lat"]))) < 50 )

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

0.492399367 30.42530045

0.496899361 30.42497045

但是我得到了这个错误

Error in .pointsToMatrix(p1) : latitude > 90

最佳答案

i get this error "Error in .pointsToMatrix(p1) : latitude > 90". Can anyone explain why and how to solve?

错误告诉您纬度值大于 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

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

I am trying to use the distHavrsine function in R, inside a loop to calculate the distance between some latitude and longitude coordinates for a several hundred rows. (...) if the distance is less than 50 meters i want it to record those rows

看看 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

关于r - 如何使用 distHaversine 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36110815/

相关文章:

vb.net - 按连续点之间的距离对点列表进行排序

r - 如何在连接之前识别两个数据帧之间不匹配的 id?

r - 如何使图例显示 R 中 tm_bubbles 的大小和颜色?

r - 绘制 Gamma 概率密度函数

r - 将 geom_pointrange 中点和线的大小与 ggplot 分开

algorithm - 连接二维空间中的任意两点

r - 如何在 ggplot 命令中激活两个不同的 scale_fill_manual

sql - 计算一列中不同条目之间的距离

java - 方法将另一个对象作为参数

swift - 在 Swift 中通过路线计算从用户到几个点的每个距离