Reputation: 1
I have gps trajectories (longitude and latitude) with trip IDs. I would like to calculate distances between trajectories, and I may use dynamic time warping algorithm for this purpose with, for instance, "SimilarityMeasures package" in R.
The following simple code works fine, but extremely slow when the number of trips takes a large value.
Could you please suggest more efficient code if possible without using for loops? Thank you very much in advance.
library(SimilarityMeasures)
data <- data.frame(IndexNo = 1:13, Latitude = rnorm(13,130,1),Longitude = rnorm(13,30,1),TripID = c("A","A","A","B","B","B","C","C","D","E","E","E","E"))
LIST = sort(unique(data$TripID))
ddist = data.table(matrix(0,length(LIST),length(LIST)))
for(i in 1:length(LIST)){
for(j in 1:length(LIST)){
data3 = data[data$TripID==LIST[i],]
data4 = data[data$TripID==LIST[j],]
traj1=cbind(data3$Latitude,data3$Longitude)
traj2=cbind(data4$Latitude,data4$Longitude)
ddist[i,j] = as.numeric(DTW(as.matrix(traj1),as.matrix(traj2)))
}
}
ddist
Upvotes: 0
Views: 75
Reputation: 17375
You could use map matching ('snap to road') so that parts of the tracks get fixed road or junction IDs and then you can easier compare them. Just a single for-loop over such road IDs and an hash lookup, so this should scale to many tracks.
Upvotes: 1