我有时空运动数据。我将如何计算坐标之间的速度? (使用 R)
Posted
技术标签:
【中文标题】我有时空运动数据。我将如何计算坐标之间的速度? (使用 R)【英文标题】:I have spatiotemporal movement data. How would I calculate speed travelled between coordinates? (using R) 【发布时间】:2018-04-17 20:40:35 【问题描述】:我在 .csv 文件中有四列(日期/时间、ID、纬度、经度)的时空运动数据(来自超过 8000 个用户的移动网络的事件数据)。我将如何计算坐标之间的行驶速度?
坐标格式为 (11.559257, 48.093384) 等。
任何建议都将不胜感激!
【问题讨论】:
查看This question 的答案以获取以纬度表示的两点之间的距离。然后speed = distance/time
【参考方案1】:
试试这样的……
library(geosphere)
library(sp)
num_records = 6
time_max = 60*60*24*10
df <- data.frame(
date_time = sort(as.POSIXct(runif(num_records, min=0, max=time_max), origin="2010-01-01")),
ID = c("Honolulu", "Kapolei", "Sarajevo", "Krupac", "South Pole", "North Pole"),
lat = c(18.41, 18.35, 21.31, 21.33, -90, 90),
long = c(43.86, 43.78, -157.86, -158.06, 1, 1)
)
sp_points <- SpatialPoints(df[c('long', 'lat')], proj4string=CRS("+proj=longlat"))
travel_mat <- data.frame(
from = 1:(num_records - 1),
to = 2:num_records
)
travel_mat$elapsed_time <- as.numeric(df$date_time[travel_mat$to] - df$date_time[travel_mat$from], units = "hours")
travel_mat$distance <- mapply(
function(from, to) distm(sp_points[from], sp_points[to], fun=distVincentyEllipsoid)[1],
from = travel_mat$from,
to = travel_mat$to
)
# units are in meters and minutes, adjusting to kilometers per hour
travel_mat$speed_kph <- round(travel_mat$distance / travel_mat$elapsed_time / 1000, 2)
travel_mat$text <- paste(df$ID[travel_mat$from], "-", df$ID[travel_mat$to])
所以如果你的输入 csv 看起来像这样:
df
date_time ID lat long
1 2010-01-01 10:43:54 Honolulu 18.41 43.86
2 2010-01-05 01:09:28 Kapolei 18.35 43.78
3 2010-01-06 06:41:24 Sarajevo 21.31 -157.86
4 2010-01-07 09:31:51 Krupac 21.33 -158.06
5 2010-01-10 06:54:45 South Pole -90.00 1.00
6 2010-01-10 22:10:00 North Pole 90.00 1.00
您的输出数据框应如下所示:
travel_mat
from to elapsed_time distance speed_kph text
1 2 86.42614 10750.59 0.12 Honolulu - Kapolei
2 3 29.53233 15029818.36 508.93 Kapolei - Sarajevo
3 4 26.84083 20867.27 0.78 Sarajevo - Krupac
4 5 69.38162 12361579.93 178.17 Krupac - South Pole
5 6 15.25400 20003931.46 1311.39 South Pole - North Pole
【讨论】:
以上是关于我有时空运动数据。我将如何计算坐标之间的速度? (使用 R)的主要内容,如果未能解决你的问题,请参考以下文章