我正在尝试在循环中使用 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/