diff options
author | typebrook <typebrook@gmail.com> | 2019-12-04 09:16:44 +0800 |
---|---|---|
committer | typebrook <typebrook@gmail.com> | 2019-12-04 09:16:44 +0800 |
commit | 4947dd920b87a585b7be84e6169f5d4bf685aba8 (patch) | |
tree | 87a46d4539f9528377778b712179c69b8729d75d /scripts/match-road.sh | |
parent | ba8adbd905b3b1b9378c9e1958cfff4f0bb27c15 (diff) |
update
Diffstat (limited to 'scripts/match-road.sh')
-rwxr-xr-x | scripts/match-road.sh | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/scripts/match-road.sh b/scripts/match-road.sh new file mode 100755 index 0000000..8281709 --- /dev/null +++ b/scripts/match-road.sh | |||
@@ -0,0 +1,79 @@ | |||
1 | #!/usr/bin/env bash | ||
2 | # | ||
3 | # Author: Pham | ||
4 | # | ||
5 | # This script accepts a single GPX file as parameter and | ||
6 | # output the processed GPX body to STDOUT, using Mapbox Map Matching API v4. | ||
7 | # read doc at: https://docs.mapbox.com/api/legacy/map-matching-v4/ | ||
8 | # | ||
9 | # Example: | ||
10 | # | ||
11 | # match-road.sh raw.gpx > new.gpx | ||
12 | # | ||
13 | # Hint: | ||
14 | # | ||
15 | # Remember to put Mapbox Access Token at the top! | ||
16 | |||
17 | #set -x | ||
18 | set -e | ||
19 | |||
20 | ACCESS_TOKEN=$(cat ~/settings/tokens/mapbox) # put yout Mapbox token here | ||
21 | LIMIT=10 # number of coordinates for each Mapbox Map Matching API request, Maximum value is 100 | ||
22 | |||
23 | ORIGIN_DATA=/tmp/origin | ||
24 | RESPONSE=/tmp/response | ||
25 | MATCHED=/tmp/matched | ||
26 | |||
27 | # 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] | ||
29 | paste -d' ' \ | ||
30 | <(sed -nr '/<trk>/,/<\/trk>/ { s/.*<time>(.*)<\/time>/\1/p }' $1 | cut -d'.' -f1) \ | ||
31 | <(sed -nr 's/.*lon=\"([^\"]+)\".*/\1/p' $1) \ | ||
32 | <(sed -nr 's/.*lat=\"([^\"]+)\".*/\1/p' $1) |\ | ||
33 | sed -r 's/ ([^ ]+) ([^ ]+)/ [\1,\2]/' |\ | ||
34 | awk '!_[$1]++' > $ORIGIN_DATA | ||
35 | |||
36 | # Consume raw data with serveral request | ||
37 | while [ -s $ORIGIN_DATA ] | ||
38 | do | ||
39 | # Make GeoJSON object for request | ||
40 | jq --slurp '{type: "Feature", properties: {coordTimes: .[1]}, geometry: {type: "LineString", coordinates: .[0]}}' \ | ||
41 | <(head -$LIMIT $ORIGIN_DATA | cut -d' ' -f2 | jq -n '[inputs]') \ | ||
42 | <(head -$LIMIT $ORIGIN_DATA | cut -d' ' -f1 | jq -nR '[inputs]') |\ | ||
43 | # Mapbox Map Matching API | ||
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 | ||
45 | |||
46 | # Put matched points and indices into tmp file | ||
47 | paste -d' ' \ | ||
48 | <(jq -c '.features[0].properties.matchedPoints[]' $RESPONSE) \ | ||
49 | <(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) \ | ||
50 | > $MATCHED | ||
51 | |||
52 | # For each coodinates from Map Matching API, add timestamp at the end and print it out to tty | ||
53 | jq -c '.features[0].geometry.coordinates[]' $RESPONSE |\ | ||
54 | while read line | ||
55 | do | ||
56 | TIMESTAMP=$(head -1 $MATCHED | cut -d' ' -f2) | ||
57 | (head -1 $MATCHED | grep -F $line && sed -i 1d $MATCHED) || echo $line $TIMESTAMP jojo | ||
58 | done |\ | ||
59 | tee /dev/tty && rm $MATCHED | ||
60 | |||
61 | # Remove processed raw data | ||
62 | sed -i "1,$LIMIT d" $ORIGIN_DATA | ||
63 | done |\ | ||
64 | # Make GPX format for output | ||
65 | sed -r 's/\[([^,]+),([^,]+)\] ?(.*)/ <trkpt lon="\1" lat="\2"><time>\3<\/time><\/trkpt>/' |\ | ||
66 | sed "1i \ | ||
67 | <gpx version=\"1.1\" creator=\"Garmin Connect\"\n\ | ||
68 | xsi:schemaLocation=\"http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd http://www.garmin.com/xmlschemas/GpxExtensions/v3 http://www.garmin.com/xmlschemas/GpxExtensionsv3.xsd http://www.garmin.com/xmlschemas/TrackPointExtension/v1 http://www.garmin.com/xmlschemas/TrackPointExtensionv1.xsd\"\n\ | ||
69 | xmlns=\"http://www.topografix.com/GPX/1/1\"\n\ | ||
70 | xmlns:gpxtpx=\"http://www.garmin.com/xmlschemas/TrackPointExtension/v1\"\n\ | ||
71 | xmlns:gpxx=\"http://www.garmin.com/xmlschemas/GpxExtensions/v3\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\n\ | ||
72 | <trk>\n\ | ||
73 | <name>$(sed -nr 's/.*<name>(.*)<\/name>.*/\1/p; /<name>/q' $1)<\/name>\n\ | ||
74 | <trkseg> | ||
75 | \$a \ | ||
76 | \ \ \ \ <\/trkseg>\n\ | ||
77 | <\/trk>\n\ | ||
78 | <\/gpx>\n\ | ||
79 | " | ||