diff options
Diffstat (limited to 'scripts')
-rwxr-xr-x | scripts/match.sh | 28 |
1 files changed, 15 insertions, 13 deletions
diff --git a/scripts/match.sh b/scripts/match.sh index e76ddec..a1c7e62 100755 --- a/scripts/match.sh +++ b/scripts/match.sh | |||
@@ -4,31 +4,33 @@ | |||
4 | set -e | 4 | set -e |
5 | 5 | ||
6 | ACCESS_TOKEN=$(cat ~/settings/tokens/mapbox) | 6 | ACCESS_TOKEN=$(cat ~/settings/tokens/mapbox) |
7 | #export MAPBOX_ACCESS_TOKEN=$ACCESS_TOKEN | ||
8 | LIMIT=10 | 7 | LIMIT=10 |
9 | TRK_NAME=$(sed -nr 's/.*<name>(.*)<\/name>.*/\1/p; /<name>/q' $1) | 8 | ORIGIN_DATA=/tmp/origin |
9 | RESPONSE=/tmp/response | ||
10 | 10 | ||
11 | # Need to add pre-process for duplicated gpx trkpts | 11 | # store data of time and location into tmp file with 2 columns, format is like: |
12 | # 1970-01-01T08:00:46 [121.0179739,14.5515336] | ||
12 | paste -d' ' \ | 13 | paste -d' ' \ |
13 | <(sed -nr '/<trk>/,/<\/trk>/ { s/.*<time>(.*)<\/time>/\1/p }' $1 | cut -d'.' -f1) \ | 14 | <(sed -nr '/<trk>/,/<\/trk>/ { s/.*<time>(.*)<\/time>/\1/p }' $1 | cut -d'.' -f1) \ |
14 | <(sed -nr 's/.*lon=\"([^\"]+)\".*/\1/p' $1) \ | 15 | <(sed -nr 's/.*lon=\"([^\"]+)\".*/\1/p' $1) \ |
15 | <(sed -nr 's/.*lat=\"([^\"]+)\".*/\1/p' $1) |\ | 16 | <(sed -nr 's/.*lat=\"([^\"]+)\".*/\1/p' $1) |\ |
16 | sed -r 's/ ([^ ]+) ([^ ]+)/ [\1,\2]/' |\ | 17 | sed -r 's/ ([^ ]+) ([^ ]+)/ [\1,\2]/' |\ |
17 | awk '!_[$1]++' > origin | 18 | awk '!_[$1]++' > $ORIGIN_DATA |
19 | exit 0 | ||
18 | 20 | ||
19 | while [ -s origin ] | 21 | while [ -s $ORIGIN_DATA ] |
20 | do | 22 | do |
21 | jq --slurp '{type: "Feature", properties: {coordTimes: .[1]}, geometry: {type: "LineString", coordinates: .[0]}}' \ | 23 | jq --slurp '{type: "Feature", properties: {coordTimes: .[1]}, geometry: {type: "LineString", coordinates: .[0]}}' \ |
22 | <(head -$LIMIT origin | cut -d' ' -f2 | jq -n '[inputs]') \ | 24 | <(head -$LIMIT $ORIGIN_DATA | cut -d' ' -f2 | jq -n '[inputs]') \ |
23 | <(head -$LIMIT origin | cut -d' ' -f1 | jq -nR '[inputs]') |\ | 25 | <(head -$LIMIT $ORIGIN_DATA | cut -d' ' -f1 | jq -nR '[inputs]') |\ |
24 | curl -X POST -s --header "Content-Type:application/json" --data @- https://api.mapbox.com/matching/v4/mapbox.driving.json?access_token=$ACCESS_TOKEN > response | 26 | curl -X POST -s --header "Content-Type:application/json" --data @- https://api.mapbox.com/matching/v4/mapbox.driving.json?access_token=$ACCESS_TOKEN > $RESPONSE |
25 | 27 | ||
26 | TIMESTAMP=0 | 28 | TIMESTAMP=0 |
27 | paste -d' ' \ | 29 | paste -d' ' \ |
28 | <(jq -c '.features[0].properties.matchedPoints[]' response) \ | 30 | <(jq -c '.features[0].properties.matchedPoints[]' $RESPONSE) \ |
29 | <(jq -c '.features[0].properties.indices[]' response | xargs -I{} echo {}+1 | bc | xargs -I{} sed -n {}p origin | cut -d' ' -f1 | date -f - +%s) \ | 31 | <(jq -c '.features[0].properties.indices[]' $RESPONSE | xargs -I{} echo {}+1 | bc | xargs -I{} sed -n {}p $ORIGIN_DATA | cut -d' ' -f1 | date -f - +%s) \ |
30 | > matched | 32 | > matched |
31 | jq -c '.features[0].geometry.coordinates[]' response |\ | 33 | jq -c '.features[0].geometry.coordinates[]' $RESPONSE |\ |
32 | while read line | 34 | while read line |
33 | do | 35 | do |
34 | TIMESTAMP=$(head -1 matched | cut -d' ' -f2) | 36 | TIMESTAMP=$(head -1 matched | cut -d' ' -f2) |
@@ -36,7 +38,7 @@ do | |||
36 | done |\ | 38 | done |\ |
37 | tee /dev/tty && rm matched | 39 | tee /dev/tty && rm matched |
38 | 40 | ||
39 | sed -i "1,$LIMIT d" origin | 41 | sed -i "1,$LIMIT d" $ORIGIN_DATA |
40 | done |\ | 42 | done |\ |
41 | sed -r 's/\[([^,]+),([^,]+)\] ?(.*)/ <trkpt lon="\1" lat="\2"><time>\3<\/time><\/trkpt>/' |\ | 43 | sed -r 's/\[([^,]+),([^,]+)\] ?(.*)/ <trkpt lon="\1" lat="\2"><time>\3<\/time><\/trkpt>/' |\ |
42 | sed "1i \ | 44 | sed "1i \ |
@@ -46,7 +48,7 @@ sed "1i \ | |||
46 | xmlns:gpxtpx=\"http://www.garmin.com/xmlschemas/TrackPointExtension/v1\"\n\ | 48 | xmlns:gpxtpx=\"http://www.garmin.com/xmlschemas/TrackPointExtension/v1\"\n\ |
47 | xmlns:gpxx=\"http://www.garmin.com/xmlschemas/GpxExtensions/v3\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\n\ | 49 | xmlns:gpxx=\"http://www.garmin.com/xmlschemas/GpxExtensions/v3\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\n\ |
48 | <trk>\n\ | 50 | <trk>\n\ |
49 | <name>$TRK_NAME<\/name>\n\ | 51 | <name>$(sed -nr 's/.*<name>(.*)<\/name>.*/\1/p; /<name>/q' $1)<\/name>\n\ |
50 | <trkseg> | 52 | <trkseg> |
51 | \$a \ | 53 | \$a \ |
52 | \ \ \ \ <\/trkseg>\n\ | 54 | \ \ \ \ <\/trkseg>\n\ |