summaryrefslogtreecommitdiffhomepage
path: root/scripts/match-road.sh
diff options
context:
space:
mode:
Diffstat (limited to 'scripts/match-road.sh')
-rwxr-xr-xscripts/match-road.sh27
1 files changed, 20 insertions, 7 deletions
diff --git a/scripts/match-road.sh b/scripts/match-road.sh
index 8de23ed..d59064a 100755
--- a/scripts/match-road.sh
+++ b/scripts/match-road.sh
@@ -21,9 +21,11 @@ set -e
21ACCESS_TOKEN=$(cat ~/settings/tokens/mapbox) 21ACCESS_TOKEN=$(cat ~/settings/tokens/mapbox)
22# number of coordinates for each Mapbox Map Matching API request, Maximum value is 100 22# number of coordinates for each Mapbox Map Matching API request, Maximum value is 100
23LIMIT=10 23LIMIT=10
24# define the lowest confidence of accepted matched points
25THRESHOLD=0.9
24 26
25ORIGIN_DATA=/tmp/$(basename $1).origin 27ORIGIN_DATA=/tmp/$(basename $1).origin
26RESPONSE=/tmp/$(basename $1).response 28RESPONSE=$(basename $1).response
27 29
28# extract data from the given gpx file, dump coordindate and time with the following format: 30# extract data from the given gpx file, dump coordindate and time with the following format:
29# [121.0179739,14.5515336] 1984-01-01T08:00:46.234 31# [121.0179739,14.5515336] 1984-01-01T08:00:46.234
@@ -44,18 +46,30 @@ function get_data() {
44# Read data like the following to make GeoJSON object for Map Matching API: 46# Read data like the following to make GeoJSON object for Map Matching API:
45# [121.0179739,14.5515336] 1984-01-01T08:00:46.234 47# [121.0179739,14.5515336] 1984-01-01T08:00:46.234
46function make_geojson() { 48function make_geojson() {
47 awk '{printf("[%s,\"%s\"]\n", $1, $2)}' |\ # change input to format like: [[lon, lat], time] 49 # change input to format like: [[lon, lat], time]
50 awk '{printf("[%s,\"%s\"]\n", $1, $2)}' |\
48 jq '[inputs] | {type: "Feature", properties: {coordTimes: (map(.[1]))}, geometry: {type: "LineString", coordinates: map(.[0])}}' 51 jq '[inputs] | {type: "Feature", properties: {coordTimes: (map(.[1]))}, geometry: {type: "LineString", coordinates: map(.[0])}}'
49} 52}
50 53
54# Read GeoJSON body from input, and return result from Mapbox Map Matching API
55function query_matched_road() {
56 curl -X POST -s --data @- \
57 --header "Content-Type:application/json" \
58 https://api.mapbox.com/matching/v4/mapbox.driving.json?access_token=$ACCESS_TOKEN
59}
60
61function get_valid_data() {
62 jq ".features[] | select(.properties.confidence >= $THRESHOLD)"
63}
64
51get_data $1 > $ORIGIN_DATA 65get_data $1 > $ORIGIN_DATA
52 66
53# Consume raw data with serveral request 67# Consume raw data with serveral request
54while [ -s $ORIGIN_DATA ]; do 68while [ -s $ORIGIN_DATA ]; do
55 head -$LIMIT $ORIGIN_DATA |\ 69
56 make_geojson |\ 70 head -$LIMIT $ORIGIN_DATA | make_geojson | query_matched_road > $RESPONSE
57 # Mapbox Map Matching API, store response into tmp file 71 cat $RESPONSE | get_valid_data
58 curl -X POST -s --data @- --header "Content-Type:application/json" https://api.mapbox.com/matching/v4/mapbox.driving.json?access_token=$ACCESS_TOKEN > $RESPONSE 72 exit 0
59 73
60 # Put existing timestamps to matched points, and interpolate new timestamps into new points 74 # Put existing timestamps to matched points, and interpolate new timestamps into new points
61 join -a1 \ 75 join -a1 \
@@ -96,4 +110,3 @@ sed "1i \
96 <\/trk>\n\ 110 <\/trk>\n\
97<\/gpx>\n\ 111<\/gpx>\n\
98 " 112 "
99