diff options
author | typebrook <typebrook@gmail.com> | 2019-12-04 16:11:29 +0800 |
---|---|---|
committer | typebrook <typebrook@gmail.com> | 2019-12-05 09:33:11 +0800 |
commit | f890ee58d88070f6ec3c4a28e786cd718a28407d (patch) | |
tree | ad32586d709d9c3e70b6d6ff1239fb7fb638581c /scripts/match-road.sh | |
parent | 4434682a1e19f15461ea9e407bd15aaa589211a9 (diff) |
update
Diffstat (limited to 'scripts/match-road.sh')
-rwxr-xr-x | scripts/match-road.sh | 34 |
1 files changed, 20 insertions, 14 deletions
diff --git a/scripts/match-road.sh b/scripts/match-road.sh index 0486dbe..01fa888 100755 --- a/scripts/match-road.sh +++ b/scripts/match-road.sh | |||
@@ -26,15 +26,14 @@ RESPONSE=/tmp/response | |||
26 | # 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: |
27 | # 1970-01-01T08:00:46 [121.0179739,14.5515336] | 27 | # 1970-01-01T08:00:46 [121.0179739,14.5515336] |
28 | paste -d' ' \ | 28 | paste -d' ' \ |
29 | <(sed -nr '/<trk>/,/<\/trk>/ { s/.*<time>(.*)<\/time>/\1/p }' $1 | cut -d'.' -f1) \ | 29 | <(sed -nE '/<trk>/,/<\/trk>/ { s/.*<time>(.*)<\/time>/\1/p }' $1 | cut -d'.' -f1) \ |
30 | <(sed -nr 's/.*lon=\"([^\"]+)\".*/\1/p' $1) \ | 30 | <(sed -nE 's/.*lon=\"([^\"]+)\".*/\1/p' $1) \ |
31 | <(sed -nr 's/.*lat=\"([^\"]+)\".*/\1/p' $1) |\ | 31 | <(sed -nE 's/.*lat=\"([^\"]+)\".*/\1/p' $1) |\ |
32 | sed -r 's/ ([^ ]+) ([^ ]+)/ [\1,\2]/' |\ | 32 | sed -E 's/ ([^ ]+) ([^ ]+)/ [\1,\2]/' |\ |
33 | awk '!_[$1]++' > $ORIGIN_DATA | 33 | awk '!_[$1]++' > $ORIGIN_DATA |
34 | 34 | ||
35 | # Consume raw data with serveral request | 35 | # Consume raw data with serveral request |
36 | while [ -s $ORIGIN_DATA ] | 36 | while [ -s $ORIGIN_DATA ]; do |
37 | do | ||
38 | # Make GeoJSON object for request | 37 | # Make GeoJSON object for request |
39 | jq --slurp '{type: "Feature", properties: {coordTimes: .[1]}, geometry: {type: "LineString", coordinates: .[0]}}' \ | 38 | jq --slurp '{type: "Feature", properties: {coordTimes: .[1]}, geometry: {type: "LineString", coordinates: .[0]}}' \ |
40 | <(head -$LIMIT $ORIGIN_DATA | cut -d' ' -f2 | jq -n '[inputs]') \ | 39 | <(head -$LIMIT $ORIGIN_DATA | cut -d' ' -f2 | jq -n '[inputs]') \ |
@@ -42,14 +41,21 @@ do | |||
42 | # Mapbox Map Matching API, store response into tmp file | 41 | # Mapbox Map Matching API, store response into tmp file |
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 | 42 | curl -X POST -s --data @- --header "Content-Type:application/json" https://api.mapbox.com/matching/v4/mapbox.driving.json?access_token=$ACCESS_TOKEN > $RESPONSE |
44 | 43 | ||
45 | # Put existing timestamp to matched points, and interpolate new timestamp into new points | 44 | # Put existing timestamps to matched points, and interpolate new timestamps into new points |
46 | join -a1 \ | 45 | join -a1 \ |
47 | <(jq -c '.features[0].geometry.coordinates[]' $RESPONSE) \ | 46 | <(jq -c '.features[0].geometry.coordinates[]' $RESPONSE) \ |
48 | <(jq -cr '.features[0].properties | [.matchedPoints, .indices] | transpose[] | "\(.[0]) \(.[1])"' $RESPONSE) |\ | 47 | <(jq -cr '.features[0].properties | [.matchedPoints, (.indices | map(.+1))] | transpose[] | "\(.[0]) \(.[1])"' $RESPONSE) |\ |
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)}}}' |\ | 48 | sed '/ / !s/$/ -1/' |\ |
50 | while read coor unix_time | 49 | while read coor index; do |
51 | do | 50 | if [ $index -gt -1 ]; then |
52 | # Trasform unix timestamp into human readable time format, like following: | 51 | echo $coor $(sed -n "$index p" $ORIGIN_DATA | cut -d' ' -f1 | date -f - +%s) |
52 | else | ||
53 | echo $coor $index | ||
54 | fi | ||
55 | done |\ | ||
56 | awk '{COOR[NR][0]=$1; N++; COOR[NR][1]=$2} 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)}}}' |\ | ||
57 | while read coor unix_time; do | ||
58 | # Transform unix timestamp into human readable time format, like following: | ||
53 | # Transform [121.018088,14.5516] 18.50 | 59 | # Transform [121.018088,14.5516] 18.50 |
54 | # Into [121.018088,14.5516] 1970-01-01T08:00:18.50Z | 60 | # Into [121.018088,14.5516] 1970-01-01T08:00:18.50Z |
55 | echo $coor $(date -d @$unix_time +'%Y-%m-%dT%H:%M:%S.%2NZ') | 61 | echo $coor $(date -d @$unix_time +'%Y-%m-%dT%H:%M:%S.%2NZ') |
@@ -59,7 +65,7 @@ do | |||
59 | sed -i "1,$LIMIT d" $ORIGIN_DATA | 65 | sed -i "1,$LIMIT d" $ORIGIN_DATA |
60 | done |\ | 66 | done |\ |
61 | # Make GPX format for output | 67 | # Make GPX format for output |
62 | sed -r 's/\[([^,]+),([^,]+)\] ?(.*)/ <trkpt lon="\1" lat="\2"><time>\3<\/time><\/trkpt>/' |\ | 68 | sed -E 's/\[([^,]+),([^,]+)\] (.*)/ <trkpt lon="\1" lat="\2"><time>\3<\/time><\/trkpt>/' |\ |
63 | sed "1i \ | 69 | sed "1i \ |
64 | <gpx version=\"1.1\" creator=\"Garmin Connect\"\n\ | 70 | <gpx version=\"1.1\" creator=\"Garmin Connect\"\n\ |
65 | 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\ | 71 | 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\ |
@@ -67,7 +73,7 @@ sed "1i \ | |||
67 | xmlns:gpxtpx=\"http://www.garmin.com/xmlschemas/TrackPointExtension/v1\"\n\ | 73 | xmlns:gpxtpx=\"http://www.garmin.com/xmlschemas/TrackPointExtension/v1\"\n\ |
68 | xmlns:gpxx=\"http://www.garmin.com/xmlschemas/GpxExtensions/v3\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\n\ | 74 | xmlns:gpxx=\"http://www.garmin.com/xmlschemas/GpxExtensions/v3\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\n\ |
69 | <trk>\n\ | 75 | <trk>\n\ |
70 | <name>$(sed -nr 's/.*<name>(.*)<\/name>.*/\1/p; /<name>/q' $1)<\/name>\n\ | 76 | <name>$(sed -nE 's/.*<name>(.*)<\/name>.*/\1/p; /<name>/q' $1)<\/name>\n\ |
71 | <trkseg> | 77 | <trkseg> |
72 | \$a \ | 78 | \$a \ |
73 | \ \ \ \ <\/trkseg>\n\ | 79 | \ \ \ \ <\/trkseg>\n\ |