summaryrefslogtreecommitdiffhomepage
path: root/scripts
diff options
context:
space:
mode:
Diffstat (limited to 'scripts')
-rwxr-xr-xscripts/match-road.sh35
1 files changed, 12 insertions, 23 deletions
diff --git a/scripts/match-road.sh b/scripts/match-road.sh
index 499f6e0..0486dbe 100755
--- a/scripts/match-road.sh
+++ b/scripts/match-road.sh
@@ -22,7 +22,6 @@ LIMIT=10 # number of coordinates for each Mapbox Map Matching API request, Maxim
22 22
23ORIGIN_DATA=/tmp/origin 23ORIGIN_DATA=/tmp/origin
24RESPONSE=/tmp/response 24RESPONSE=/tmp/response
25MATCHED=/tmp/matched
26 25
27# store data of time and location into tmp file with 2 columns, format is like: 26# store data of time and location into tmp file with 2 columns, format is like:
28# 1970-01-01T08:00:46 [121.0179739,14.5515336] 27# 1970-01-01T08:00:46 [121.0179739,14.5515336]
@@ -40,31 +39,21 @@ do
40 jq --slurp '{type: "Feature", properties: {coordTimes: .[1]}, geometry: {type: "LineString", coordinates: .[0]}}' \ 39 jq --slurp '{type: "Feature", properties: {coordTimes: .[1]}, geometry: {type: "LineString", coordinates: .[0]}}' \
41 <(head -$LIMIT $ORIGIN_DATA | cut -d' ' -f2 | jq -n '[inputs]') \ 40 <(head -$LIMIT $ORIGIN_DATA | cut -d' ' -f2 | jq -n '[inputs]') \
42 <(head -$LIMIT $ORIGIN_DATA | cut -d' ' -f1 | jq -nR '[inputs]') |\ 41 <(head -$LIMIT $ORIGIN_DATA | cut -d' ' -f1 | jq -nR '[inputs]') |\
43 # Mapbox Map Matching API 42 # Mapbox Map Matching API, store response into tmp file
44 curl -X POST -s --data @- --header "Content-Type:application/json" https://api.mapbox.com/matching/v4/mapbox.driving.json?access_token=$ACCESS_TOKEN > $RESPONSE 43 curl -X POST -s --data @- --header "Content-Type:application/json" https://api.mapbox.com/matching/v4/mapbox.driving.json?access_token=$ACCESS_TOKEN > $RESPONSE
45 44
46 # Put matched points and indices into tmp file 45 # Put existing timestamp to matched points, and interpolate new timestamp into new points
47 paste -d' ' \ 46 join -a1 \
48 <(jq -c '.features[0].properties.matchedPoints[]' $RESPONSE) \ 47 <(jq -c '.features[0].geometry.coordinates[]' $RESPONSE) \
49 <(jq -c '.features[0].properties.indices[]' $RESPONSE | xargs -I{} echo {}+1 | bc | xargs -I{} sed -n {}p $ORIGIN_DATA | cut -d' ' -f1) \ 48 <(jq -cr '.features[0].properties | [.matchedPoints, .indices] | transpose[] | "\(.[0]) \(.[1])"' $RESPONSE) |\
50 > $MATCHED 49 awk '{COOR[NR][0]=$1; N++; if(NF>1) COOR[NR][1]=$2; else COOR[NR][1]=-1} END{for (i=1; i<=N; i++) {printf COOR[i][0]; if (COOR[i][1] != -1) {print " "COOR[i][1]; LAST=i} else {while(COOR[i+n][1] == -1) n++; print " "COOR[LAST][1]+(COOR[i+n][1]-COOR[LAST][1])*(i-LAST)/(i+n-LAST)}}}' |\
51 50 while read coor unix_time
52 # FIXME temporary solution for timestamp to unmatched points
53 DURATION=$(jq '.features[0].properties.duration' $RESPONSE)
54 INTERVAL=$(echo "scale=2;" $DURATION / $LIMIT | tee >(cat >> log) | bc -l)
55
56 # For each coodinates from Map Matching API, add timestamp at the end and print it out to tty
57 jq -c '.features[0].geometry.coordinates[]' $RESPONSE |\
58 while read line
59 do 51 do
60 if head -1 $MATCHED | grep -F $line; then 52 # Trasform unix timestamp into human readable time format, like following:
61 TIMESTAMP=$(head -1 $MATCHED | cut -d' ' -f2) 53 # Transform [121.018088,14.5516] 18.50
62 sed -i 1d $MATCHED 54 # Into [121.018088,14.5516] 1970-01-01T08:00:18.50Z
63 else 55 echo $coor $(date -d @$unix_time +'%Y-%m-%dT%H:%M:%S.%2NZ')
64 echo $line "$TIMESTAMP.5" 56 done | tee /dev/tty
65 fi
66 done |\
67 tee /dev/tty && rm $MATCHED
68 57
69 # Remove processed raw data 58 # Remove processed raw data
70 sed -i "1,$LIMIT d" $ORIGIN_DATA 59 sed -i "1,$LIMIT d" $ORIGIN_DATA