标签基于R / MATLAB中GPS坐标的路径

时间:2018-04-02 04:58:38

标签: matlab routes gps label coordinates

我有三个固定点说A,B和C以及它们之间可以通勤的几条路线,它们可以按任意顺序排列。旅行开始和结束于A. Image of routes, fixed points and reference points. 我有1000个GPS文件,我需要在其中标记所拍摄的路线。 GPS文件记录纬度,经度(以度为单位),速度,每秒行进的距离和累积距离。

我想到了以下算法:在A,B和C点的每个固定距离(比如d)处识别每条路线上的参考点。现在扫描GPS文件,查看A点的第一次出现, B或C.由于它是GPS测量,可能没有与A,B或C相对应的精确点。因此,我们可以在A,B或C的0.1英里内找到第一个出现的点。

当我们向B或C方向移动时,当累积距离接近d时(再次可能不是d),我们开始计算A点的累积量,我们记录该点的坐标并找到最接近的参考点它。最近的参考点将给我路线。我需要将此路线从起点指定为A,B或C中的第二次出现。例如,在从A,B或C中的任何一个行驶'd'之后,我们发现坐标最接近点3图中,我们说路线是BA。如果最近的点是2,则路线是AB。

现在我们转到第二次出现A,B或C,然后在距离d后找到一个点并找到到该点的最近参考点。分配路线等。

请帮我解释一下代码。无论是R还是MATLAB。

1 个答案:

答案 0 :(得分:0)

所以,我写了一个包含很多for循环的代码。它适用于我,没有错误。大约需要9秒来处理15000行的单个文件并识别我需要的所有8条路径。到现在为止还挺好。只是想找到一个更优化的程序或更快的程序。我是编程的初学者。

gpsdata <- read.csv(file = filename, header = TRUE)
####lat and long of route start/end points

# Route Start Point: Routes A and C on Cates Avenue
cateslat = 35.781999
cateslon = -78.668252
#Route Mid: Intersection of AC13, Morning Dove Road
morlat = 35.882035
morlon = -78.645653
#Route End: RTP 
rtplat = 35.895431
rtplon = -78.868958
}

#REFERENCE POINTS FOR ROUTE IDENTIFICATION ON EACH ROUTE 
{
routea_lat = 35.795400
routea_lon = -78.641243
routec_lat = 35.821031
routec_lon = -78.687069
route1_lat = 35.913527
route1_lon = -78.714348
route3_lat = 35.922331
route3_lon = -78.842865
}

##### ROUTE START POINT TOLERANCE in miles
route_tol = 0.02
##### Reference POINT TOLERANCE for Identifying Routes (in miles)
ref_tol = 0.5

##Calculation Starts Here
n = length(gpsdata$Timestamp) ##File length

#Find point q where we first reach starting route (within 0.01 miles)
for (i in 1:n){
if((distm(c(gpsdata$Longitude[i], gpsdata$Latitude[i]),c(cateslon, 
cateslat),fun = distHaversine)*0.000621371)<route_tol){
  q=i
  break
}
else
{
  q=1
}
} 

### First Route: Either A-Out or C-Out
for (i in q:n){
if((distm(c(gpsdata$Longitude[i], gpsdata$Latitude[i]),c(morlon, morlat),fun 
= distHaversine)*0.000621371)<route_tol){
r=i
break
}
else
{
r=1
}
}

gpsdata1=gpsdata[(q+1):r,]
for (i in 1:length(gpsdata1$Timestamp)){
if((distm(c(gpsdata1$Longitude[i], gpsdata1$Latitude[i]),c(routea_lon, 
routea_lat),fun = distHaversine)*0.000621371)<ref_tol){
gpsdata1$Route='A-Out'
break
} else if((distm(c(gpsdata1$Longitude[i], 
gpsdata1$Latitude[i]),c(routec_lon, routec_lat),fun = 
distHaversine)*0.000621371)<ref_tol){
  gpsdata1$Route='C-Out'
  break
} else{
  gpsdata1$Route='Error-Check Data'
}
}

#Second Route: possibilities- 1-Out, 3-Out, A-In, C-In
#From Here we can either go back to Cates Avenue or go to RTP

for (i in r:n){
if((distm(c(gpsdata$Longitude[i], gpsdata$Latitude[i]),c(rtplon, rtplat),fun 
= distHaversine)*0.000621371<route_tol) || +
 (distm(c(gpsdata$Longitude[i], gpsdata$Latitude[i]),c(cateslon, 
cateslat),fun = distHaversine)*0.000621371<route_tol)){
s=i
break
}
else
{
s=1
}
}

gpsdata2=gpsdata[(r+1):s,]
for (i in 1:length(gpsdata2$Timestamp)){
if((distm(c(gpsdata2$Longitude[i], gpsdata2$Latitude[i]),c(route1_lon, 
route1_lat),fun = distHaversine)*0.000621371)<ref_tol){
gpsdata2$Route='1-Out'
break
} else if((distm(c(gpsdata2$Longitude[i], 
gpsdata2$Latitude[i]),c(route3_lon, route3_lat),fun = 
distHaversine)*0.000621371)<ref_tol){
gpsdata2$Route='3-Out'
break
} else if((distm(c(gpsdata2$Longitude[i], 
gpsdata2$Latitude[i]),c(routea_lon, routea_lat),fun = 
distHaversine)*0.000621371)<ref_tol){
gpsdata2$Route='A-In'
break
} else if((distm(c(gpsdata2$Longitude[i], 
gpsdata2$Latitude[i]),c(routec_lon, routec_lat),fun = 
distHaversine)*0.000621371)<ref_tol){
gpsdata2$Route='C-In'
break
} else{
gpsdata2$Route='Error-Check Data'
}
}