R中地球上两个地方之间的距离

时间:2016-08-25 15:16:35

标签: r geosphere

我正在尝试用R计算地球上两点之间的距离。

library(geosphere)
library(oce)

geodDist(StoreLocations$latitude[c(26)], StoreLocations$longitude[c(26)], 
      CustomerLocations$latitude[c(5)], CustomerLocations$longitude[c(5)]) 

在谷歌地图中放入Lat / Long(测量距离功能)。我得到总距离:1.88公里(1.17英里),但这不是我使用上面的代码得到的。我很难找到能帮助我的人。

Store        51.68108210    -0.09732268 
Customer     51.66665932    -0.08300349 

1 个答案:

答案 0 :(得分:3)

从阅读geosphere文档看起来你已经颠倒了参数顺序。 geoDist(或2016年6月15日发布的最新版本套餐中的distGeo)首先预测经度,然后是纬度。你似乎先拥有纬度。

df <- data.frame(long = c(-0.09732268, -0.08300349), lat = c(51.68108210, 51.66665932))

         long      lat
1 -0.09732268 51.68108
2 -0.08300349 51.66666

distGeo(df[1, ], df[2, ])

1885.795 (1.88 km)

您还应该检查以确保您使用的是最新版本的软件包。