使用R

时间:2015-09-02 22:11:35

标签: r packages geospatial distance latitude-longitude

我有长整数格式的地理编码点,我想用R来计算它们之间的距离。这看起来非常简单,但我找不到一个能够轻松完成它的功能。我一直试图用gdistance包来做,但它似乎非常复杂并且面向图形,我只需要一个数字。类似distanceBetween(pointA,pointB)的返回数字的东西。

更新:此问题特定于R,可能的dup更为通用。虽然有一个R特定的答案,但它被埋没在其他28个答案中。

2 个答案:

答案 0 :(得分:64)

加载geosphere包,你可以使用许多不同的功能

library(geosphere)
distm(c(lon1, lat1), c(lon2, lat2), fun = distHaversine)

此外:

distHaversine()
distMeeus()
distRhumb()
distVincentyEllipsoid()
distVincentySphere()

...

答案 1 :(得分:13)

在上面的回答中同意@PereG,但认为纬度和经度的顺序是相反的:lon,lat。这将影响距离矩阵的结果。所以正确的是:

library(geosphere)
distm (c(lon1, lat1), c(lon2, lat2), fun = distHaversine)

来源:ftp://cran.r-project.org/pub/R/web/packages/geosphere/geosphere.pdf

相关问题