aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authortypebrook <typebrook@gmail.com>2019-12-04 09:13:00 +0800
committertypebrook <typebrook@gmail.com>2019-12-04 09:13:00 +0800
commitba8adbd905b3b1b9378c9e1958cfff4f0bb27c15 (patch)
tree700a531566c77de39780768108c88bbe5e5adeda
parente8d03c43ddeabfbff4934a69722cf7f125b5c55e (diff)
update
-rwxr-xr-xscripts/match.sh44
1 files changed, 33 insertions, 11 deletions
diff --git a/scripts/match.sh b/scripts/match.sh
index a1c7e62..135fc81 100755
--- a/scripts/match.sh
+++ b/scripts/match.sh
@@ -1,12 +1,28 @@
1#! /bin/bash 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.sh raw.gpx > new.gpx
12#
13# Hint:
14#
15# Remember to put Mapbox Access Token at the top!
2 16
3#set -x 17#set -x
4set -e 18set -e
5 19
6ACCESS_TOKEN=$(cat ~/settings/tokens/mapbox) 20ACCESS_TOKEN=$(cat ~/settings/tokens/mapbox) # put yout Mapbox token here
7LIMIT=10 21LIMIT=10 # number of coordinates for each Mapbox Map Matching API request, Maximum value is 100
22
8ORIGIN_DATA=/tmp/origin 23ORIGIN_DATA=/tmp/origin
9RESPONSE=/tmp/response 24RESPONSE=/tmp/response
25MATCHED=/tmp/matched
10 26
11# store data of time and location into tmp file with 2 columns, format is like: 27# 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] 28# 1970-01-01T08:00:46 [121.0179739,14.5515336]
@@ -16,30 +32,36 @@ paste -d' ' \
16 <(sed -nr 's/.*lat=\"([^\"]+)\".*/\1/p' $1) |\ 32 <(sed -nr 's/.*lat=\"([^\"]+)\".*/\1/p' $1) |\
17sed -r 's/ ([^ ]+) ([^ ]+)/ [\1,\2]/' |\ 33sed -r 's/ ([^ ]+) ([^ ]+)/ [\1,\2]/' |\
18awk '!_[$1]++' > $ORIGIN_DATA 34awk '!_[$1]++' > $ORIGIN_DATA
19exit 0
20 35
36# Consume raw data with serveral request
21while [ -s $ORIGIN_DATA ] 37while [ -s $ORIGIN_DATA ]
22do 38do
39 # Make GeoJSON object for request
23 jq --slurp '{type: "Feature", properties: {coordTimes: .[1]}, geometry: {type: "LineString", coordinates: .[0]}}' \ 40 jq --slurp '{type: "Feature", properties: {coordTimes: .[1]}, geometry: {type: "LineString", coordinates: .[0]}}' \
24 <(head -$LIMIT $ORIGIN_DATA | cut -d' ' -f2 | jq -n '[inputs]') \ 41 <(head -$LIMIT $ORIGIN_DATA | cut -d' ' -f2 | jq -n '[inputs]') \
25 <(head -$LIMIT $ORIGIN_DATA | cut -d' ' -f1 | jq -nR '[inputs]') |\ 42 <(head -$LIMIT $ORIGIN_DATA | cut -d' ' -f1 | jq -nR '[inputs]') |\
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 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
27 45
28 TIMESTAMP=0 46 # Put matched points and indices into tmp file
29 paste -d' ' \ 47 paste -d' ' \
30 <(jq -c '.features[0].properties.matchedPoints[]' $RESPONSE) \ 48 <(jq -c '.features[0].properties.matchedPoints[]' $RESPONSE) \
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) \ 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) \
32 > matched 50 > $MATCHED
51
52 # For each coodinates from Map Matching API, add timestamp at the end and print it out to tty
33 jq -c '.features[0].geometry.coordinates[]' $RESPONSE |\ 53 jq -c '.features[0].geometry.coordinates[]' $RESPONSE |\
34 while read line 54 while read line
35 do 55 do
36 TIMESTAMP=$(head -1 matched | cut -d' ' -f2) 56 TIMESTAMP=$(head -1 $MATCHED | cut -d' ' -f2)
37 (head -1 matched | grep -F $line && sed -i 1d matched) || echo $line $TIMESTAMP jojo 57 (head -1 $MATCHED | grep -F $line && sed -i 1d $MATCHED) || echo $line $TIMESTAMP jojo
38 done |\ 58 done |\
39 tee /dev/tty && rm matched 59 tee /dev/tty && rm $MATCHED
40 60
61 # Remove processed raw data
41 sed -i "1,$LIMIT d" $ORIGIN_DATA 62 sed -i "1,$LIMIT d" $ORIGIN_DATA
42done |\ 63done |\
64# Make GPX format for output
43sed -r 's/\[([^,]+),([^,]+)\] ?(.*)/ <trkpt lon="\1" lat="\2"><time>\3<\/time><\/trkpt>/' |\ 65sed -r 's/\[([^,]+),([^,]+)\] ?(.*)/ <trkpt lon="\1" lat="\2"><time>\3<\/time><\/trkpt>/' |\
44sed "1i \ 66sed "1i \
45<gpx version=\"1.1\" creator=\"Garmin Connect\"\n\ 67<gpx version=\"1.1\" creator=\"Garmin Connect\"\n\
@@ -54,4 +76,4 @@ sed "1i \
54\ \ \ \ <\/trkseg>\n\ 76\ \ \ \ <\/trkseg>\n\
55 <\/trk>\n\ 77 <\/trk>\n\
56<\/gpx>\n\ 78<\/gpx>\n\
57 " | tee output.gpx 79 "