Calculate total miles traveled from vectors of lat / lon

*爱你&永不变心* 提交于 2019-11-28 02:03:49

How about this?

## Setup
library(geosphere)
metersPerMile <- 1609.34
pts <- df1[c("lon", "lat")]

## Pass in two derived data.frames that are lagged by one point
segDists <- distVincentyEllipsoid(p1 = pts[-nrow(df),], 
                                  p2 = pts[-1,])
sum(segDists)/metersPerMile
# [1] 1013.919

(To use one of the faster distance calculation algorithms, just substitute distCosine, distVincentySphere, or distHaversine for distVincentyEllipsoid in the call above.)

Be VERY careful with missing data, as distVincentyEllipsoid() returns 0 for distance between any two points with missing coordinates c(NA, NA), c(NA, NA).

library(geodist)
geodist(df, sequential = TRUE, measure = "geodesic") # sequence of distance increments
sum(geodist(df, sequential = TRUE, measure = "geodesic")) # total distance in metres
sum(geodist(df, sequential = TRUE, measure = "geodesic")) * 0.00062137 # total distance in miles

Geodesic distances are necessary here because of the long distances involved. The result is 1013.915, slightly different from less-accurate Vincenty distances of geosphere. Street network distances can also be calculated with

library(dodgr)
dodgr_dists(from = df)

... but there has to be a street network, which is not the case for (lat = 76, lon = -110). Where there is a street network, that will by default give you all pair-wise distances routed through the street network , from which the sequential increments are the off-diagonal.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!