使用R中的distm()计算数据帧中两个GPS位置之间的距离

jua*_*lix 6 r geosphere

这个问题以前被问过,但从来没有用以下数据排列。下面是它的一个示例:

> head(datagps)
   Date & Time [Local]  Latitude Longitude
1: 2018-06-18 03:01:00 -2.434901  34.85359
2: 2018-06-18 03:06:00 -2.434598  34.85387
3: 2018-06-18 03:08:00 -2.434726  34.85382
4: 2018-06-18 03:12:00 -2.434816  34.85371
5: 2018-06-18 03:16:00 -2.434613  34.85372
6: 2018-06-18 03:20:00 -2.434511  34.85376
Run Code Online (Sandbox Code Playgroud)

如您所见,我有一Date & Time [Local]列平均每 4 分钟记录一次 GPS 位置。我想计算两个连续记录之间的距离(以米为单位)并将此度量存储在新列中Step。我一直在尝试distm()对我的数据实施:

> datagps$Step<-distm(c(datagps$Longitude, datagps$Latitude), c(datagps$Longitude+1, datagps$Latitude+1), fun = distHaversine)
Error in .pointsToMatrix(x) : Wrong length for a vector, should be 2
Run Code Online (Sandbox Code Playgroud)

尽管我不确定语法以及这是否是填充函数参数的正确方法。我对 R 很陌生,所以我希望能得到一些帮助。

任何输入表示赞赏!

小智 3

我想你已经快到了。n假设您想将上一次录音 ( ) 和当前录音 ( n+1)之间的距离存储在 处n+1,您可以使用:

library(geosphere)
date <- c("2018-06-18 03:01.00","2018-06-18 03:06.00","2018-06-18 03:08.00","2018-06-18 03:12.00","2018-06-18 03:16.00","2018-06-18 03:20.00")
latitude <- c(-2.434901,-2.434598,-2.434726,-2.434816,-2.434613,-2.434511)  
longitude <- c(34.85359,34.85387,34.85382,34.85371,34.85372,34.85376)
datagps <- data.frame(date,lat,lon)

datagps$length <- distm(x=datagps[,2:3], fun = distHaversine)[,1]
Run Code Online (Sandbox Code Playgroud)

第一个结果为 0,其余为连续点之间的距离