函数用于计算两点(lat,long)之间的地理空间距离

我有很长的,纬度格式的地理编码点,我想用 R 来计算它们之间的距离。这看起来很简单,但是我找不到一个容易实现的函数。我一直在尝试用 gdistance 软件包做这件事,但它似乎非常复杂和面向图形,我只需要一个数字。返回一个数字的类似 distanceBetween(pointA,pointB)的东西。

130850 次浏览

Loading the geosphere package you can use a number of different functions

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

Also:

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

...

Agree with @PereG on answer above, but think that the order of latitude and longitude is the other way around: lon, lat. This will affect your results for distance matrix. So correct is:

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

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