如何使用distHaversine函数? [英] How to use distHaversine function?
问题描述
我正在尝试在循环中使用R中的distHavrsine函数来计算几百行的某些经纬度坐标之间的距离.在我的循环中,我有以下代码:
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. In my loop I have this code:
if ((distHaversine(c(file[i,"long"], file[i,"lat"]),
c(file[j,"long"], file[j,"lat"]))) < 50 )
在此之后,如果距离小于50米,我希望它记录这些行,并且它所参考的经纬度坐标如下所示:
after which if the distance is less than 50 meters i want it to record those rows, and where the latitude and longitude coordinates it is referencing look like:
0.492399367 30.42530045
和
0.496899361 30.42497045
但是我得到这个错误
.pointsToMatrix(p1)中的错误:纬度> 90
Error in .pointsToMatrix(p1) : latitude > 90
推荐答案
我收到此错误".pointsToMatrix(p1)中的错误:纬度> 90".能有人解释为什么以及如何解决吗?
i get this error "Error in .pointsToMatrix(p1) : latitude > 90". Can anyone explain why and how to solve?
该错误告诉您纬度值大于90,超出范围:
The error tells you that you got latitude values greater than 90, which is out of scope:
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
提供坐标在可接受范围内的坐标来解决此问题.
You can solve this issue by only feeding distHaversine
with coordinates inside the accepted ranges.
我正在尝试在R中的distHavrsine函数中使用循环计算一些经纬度坐标之间的距离几百行.(...)如果距离小于50米,我要它记录那些行
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米更近的数据帧行:
Have a look at the distm
function, which calculates a distance matrix for your few hundred rows easily (i.e. without loops). It uses distHaversine
by default. For example, to get the data frame rows that are closer then 650000 meters:
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
这篇关于如何使用distHaversine函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!