From ba8adbd905b3b1b9378c9e1958cfff4f0bb27c15 Mon Sep 17 00:00:00 2001 From: typebrook Date: Wed, 4 Dec 2019 09:13:00 +0800 Subject: update --- scripts/match.sh | 44 +++++++++++++++++++++++++++++++++----------- 1 file changed, 33 insertions(+), 11 deletions(-) (limited to 'scripts/match.sh') 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 @@ -#! /bin/bash +#!/usr/bin/env bash +# +# Author: Pham +# +# This script accepts a single GPX file as parameter and +# output the processed GPX body to STDOUT, using Mapbox Map Matching API v4. +# read doc at: https://docs.mapbox.com/api/legacy/map-matching-v4/ +# +# Example: +# +# match.sh raw.gpx > new.gpx +# +# Hint: +# +# Remember to put Mapbox Access Token at the top! #set -x set -e -ACCESS_TOKEN=$(cat ~/settings/tokens/mapbox) -LIMIT=10 +ACCESS_TOKEN=$(cat ~/settings/tokens/mapbox) # put yout Mapbox token here +LIMIT=10 # number of coordinates for each Mapbox Map Matching API request, Maximum value is 100 + ORIGIN_DATA=/tmp/origin RESPONSE=/tmp/response +MATCHED=/tmp/matched # store data of time and location into tmp file with 2 columns, format is like: # 1970-01-01T08:00:46 [121.0179739,14.5515336] @@ -16,30 +32,36 @@ paste -d' ' \ <(sed -nr 's/.*lat=\"([^\"]+)\".*/\1/p' $1) |\ sed -r 's/ ([^ ]+) ([^ ]+)/ [\1,\2]/' |\ awk '!_[$1]++' > $ORIGIN_DATA -exit 0 +# Consume raw data with serveral request while [ -s $ORIGIN_DATA ] do + # Make GeoJSON object for request jq --slurp '{type: "Feature", properties: {coordTimes: .[1]}, geometry: {type: "LineString", coordinates: .[0]}}' \ <(head -$LIMIT $ORIGIN_DATA | cut -d' ' -f2 | jq -n '[inputs]') \ <(head -$LIMIT $ORIGIN_DATA | cut -d' ' -f1 | jq -nR '[inputs]') |\ - curl -X POST -s --header "Content-Type:application/json" --data @- https://api.mapbox.com/matching/v4/mapbox.driving.json?access_token=$ACCESS_TOKEN > $RESPONSE + # Mapbox Map Matching API + curl -X POST -s --data @- --header "Content-Type:application/json" https://api.mapbox.com/matching/v4/mapbox.driving.json?access_token=$ACCESS_TOKEN > $RESPONSE - TIMESTAMP=0 + # Put matched points and indices into tmp file paste -d' ' \ <(jq -c '.features[0].properties.matchedPoints[]' $RESPONSE) \ <(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) \ - > matched + > $MATCHED + + # For each coodinates from Map Matching API, add timestamp at the end and print it out to tty jq -c '.features[0].geometry.coordinates[]' $RESPONSE |\ while read line do - TIMESTAMP=$(head -1 matched | cut -d' ' -f2) - (head -1 matched | grep -F $line && sed -i 1d matched) || echo $line $TIMESTAMP jojo + TIMESTAMP=$(head -1 $MATCHED | cut -d' ' -f2) + (head -1 $MATCHED | grep -F $line && sed -i 1d $MATCHED) || echo $line $TIMESTAMP jojo done |\ - tee /dev/tty && rm matched + tee /dev/tty && rm $MATCHED + # Remove processed raw data sed -i "1,$LIMIT d" $ORIGIN_DATA done |\ +# Make GPX format for output sed -r 's/\[([^,]+),([^,]+)\] ?(.*)/