使用geosphere包有效地计算距离

时间:2017-05-19 09:54:44

标签: r data.table geosphere

我有以下格式的数据(行数:~100万)

head(dt)
   pickup_longitude pickup_latitude dropoff_longitude dropoff_latitude
1:        -74.00394        40.74289         -73.99337         40.73425
2:        -73.97386        40.75219         -73.95870         40.77253
3:        -73.95441        40.76442         -73.97078         40.75835
4:        -73.96234        40.76722         -73.97551         40.75687
5:        -74.00466        40.70743         -73.99937         40.72152
6:        -73.99557        40.71602         -73.99997         40.74332

library(geosphere)
dt = data.table(pickup_longitude = c(-74.00394, -73.97386, -73.95441, -73.96234, -74.00466, -73.99557),
            pickup_latitude = c(40.74289, 40.75219, 40.76442, 40.76722, 40.70743, 40.71602), 
            dropoff_longitude = c(-73.99337, -73.95870, -73.97078, -73.97551, -73.99937, -73.99997),
            dropoff_latitude = c(40.73425, 40.77253, 40.75835, 40.75687, 40.72152, 40.74332))
dt[, distance := apply(dt, 1, function(t) distm(x = c(t[1], t[2]), y = c(t[3], t[4])))]

我使用apply使用了上述代码,因为distm包中的函数geosphere未进行矢量化。但是,上述代码中的apply需要花费大量时间。

我也尝试过:

dt[, distance := distm(x = c(pickup_longitude, pickup_latitude), y = c(dropoff_longitude, dropoff_latitude)), by = 1:nrow(dt)]

还有什么可以更好,更快地计算距离?

1 个答案:

答案 0 :(得分:0)

我试过this

dt[, distance := distHaversine(matrix(c(pickup_longitude, pickup_latitude), ncol = 2),
                        matrix(c(dropoff_longitude, dropoff_latitude), ncol = 2))]

这完全没问题。