aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rwxr-xr-xscripts/match-road.sh38
1 files changed, 21 insertions, 17 deletions
diff --git a/scripts/match-road.sh b/scripts/match-road.sh
index bdbd288..dc014ac 100755
--- a/scripts/match-road.sh
+++ b/scripts/match-road.sh
@@ -20,16 +20,17 @@ set -e
20# put yout Mapbox token here 20# put yout Mapbox token here
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=80 23LIMIT=50
24# define the lowest confidence of accepted matched points 24# define the lowest confidence of accepted matched points
25THRESHOLD=0.3 25THRESHOLD=0.6
26 26
27if [[ -z $1 ]]; then echo "You need to give a gpx file!"; exit 1; fi 27if [[ -z $1 ]]; then echo "You need to give a gpx file!"; exit 1; fi
28ORIGIN_DATA=/tmp/$(basename $1).origin 28ORIGIN_DATA=/tmp/$(basename $1).origin
29RESPONSES=/tmp/$(basename $1).responses && true > $RESPONSES 29RESPONSES=/tmp/$(basename $1).responses && true > $RESPONSES
30MATCHED=/tmp/$(basename $1).matched
31 30
31MATCHED=/tmp/$(basename $1).matched
32# extract data from the given gpx file 32# extract data from the given gpx file
33# only keep first point and remove the rest which in the same "seconds"
33# input: [gpx format] 34# input: [gpx format]
34# output: [121.0179739,14.5515336] 1984-01-01T08:00:46 35# output: [121.0179739,14.5515336] 1984-01-01T08:00:46
35function get_data() { 36function get_data() {
@@ -52,7 +53,7 @@ function get_data() {
52function make_geojson() { 53function make_geojson() {
53 # change input to format like: [[lon, lat], time] 54 # change input to format like: [[lon, lat], time]
54 awk '{printf("[%s,\"%s\"]\n", $1, $2)}' | 55 awk '{printf("[%s,\"%s\"]\n", $1, $2)}' |
55 jq '[inputs] | {type: "Feature", properties: {coordTimes: (map(.[1]))}, geometry: {type: "LineString", coordinates: map(.[0])}}' 56 jq '[inputs] | {type: "Feature", geometry: {type: "LineString", coordinates: map(.[0])}, properties: {coordTimes: (map(.[1]))}}'
56} 57}
57 58
58# Read GeoJSON body from STDIN, and return result from Mapbox Map Matching API 59# Read GeoJSON body from STDIN, and return result from Mapbox Map Matching API
@@ -68,14 +69,20 @@ function query_matched_points() {
68# If the point is newly added, the index would be -1, like 69# If the point is newly added, the index would be -1, like
69# [121.0189339,14.5525931] -1 70# [121.0189339,14.5525931] -1
70function validate_matched_points() { 71function validate_matched_points() {
71 VALID_DATA=$(jq ".features[] | select(.properties.confidence >= $THRESHOLD)") 72 VALID_DATA=$(jq ".features[] | if(.properties.confidence < $THRESHOLD) then .geometry.coordinates=(.properties.indices|map(.+1)) else . end")
73 #VALID_DATA=$(jq ".features[] | select(.properties.confidence >= $THRESHOLD)")
72 74
73 echo $VALID_DATA | 75 echo $VALID_DATA |
74 jq -cr '.properties | [.matchedPoints, (.indices | map(.+1))] | transpose[] | "\(.[0]) \(.[1])"' > $MATCHED 76 jq -cr '.properties | [.matchedPoints, (.indices | map(.+1))] | transpose[] | "\(.[0]) \(.[1])"' > $MATCHED
75 77
76 echo $VALID_DATA | jq -c '.geometry.coordinates[]' | 78 echo $VALID_DATA | jq -c '.geometry.coordinates[]' |
77 while read point; do 79 while read point; do
78 if head -1 $MATCHED | grep -F $point; then 80 if [[ ${point:0:1} != '[' ]]; then
81 echo $(sed -n "$point p" $ORIGIN_DATA)
82 sed -i 1d $MATCHED
83 elif head -1 $MATCHED | grep -F $point > /dev/null; then
84 index=$(head -1 $MATCHED | cut -d' ' -f2)
85 echo $point $(sed -n "$index p" $ORIGIN_DATA | cut -d' ' -f2 | date -f - +%s)
79 sed -i 1d $MATCHED 86 sed -i 1d $MATCHED
80 else 87 else
81 echo $point -1 88 echo $point -1
@@ -85,13 +92,6 @@ function validate_matched_points() {
85 92
86# Put existing timestamps to matched points, and interpolate new timestamps into new points 93# Put existing timestamps to matched points, and interpolate new timestamps into new points
87function complete_data() { 94function complete_data() {
88 while read coor index; do
89 if [ $index -gt -1 ]; then
90 echo $coor $(sed -n "$index p" $ORIGIN_DATA | cut -d' ' -f2 | date -f - +%s)
91 else
92 echo $coor $index
93 fi
94 done|
95 # interpolate timestamps to newly added points 95 # interpolate timestamps to newly added points
96 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)}}}' | 96 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)}}}' |
97 while read coor unix_time; do 97 while read coor unix_time; do
@@ -124,6 +124,9 @@ function make_gpx() {
124 124
125get_data $1 > $ORIGIN_DATA 125get_data $1 > $ORIGIN_DATA
126 126
127RAW_REQUEST=$(basename $1 | tr '.' '_')_request.geojson
128cat $ORIGIN_DATA | make_geojson | jq '.properties.stroke="#ff0000"' > $RAW_REQUEST
129
127# Consume raw data with serveral request 130# Consume raw data with serveral request
128while [ -s $ORIGIN_DATA ]; do 131while [ -s $ORIGIN_DATA ]; do
129 # Take original data by limited points for each time: [121.0179739,14.5515336] 1984-01-01T08:00:46 132 # Take original data by limited points for each time: [121.0179739,14.5515336] 1984-01-01T08:00:46
@@ -135,16 +138,17 @@ while [ -s $ORIGIN_DATA ]; do
135 make_geojson | 138 make_geojson |
136 query_matched_points | 139 query_matched_points |
137 tee -a $RESPONSES | 140 tee -a $RESPONSES |
138 tee /dev/tty |
139 validate_matched_points 141 validate_matched_points
140 142
141 # Remove processed raw data 143 # Remove processed raw data
142 echo $LIMIT of $(wc -l $ORIGIN_DATA) > /dev/tty
143 sed -i "1,$LIMIT d" $ORIGIN_DATA 144 sed -i "1,$LIMIT d" $ORIGIN_DATA
144done | 145done |
145make_geojson > test.geojson 146make_geojson > test.geojson
146 147
147RAW_RESPONSE=$(basename $1 | tr '.' '_')_response.geojson 148RAW_RESPONSE=$(basename $1 | tr '.' '_')_response.geojson
148MATCHED_POINTS=$(basename $1 | tr '.' '_')_matched.geojson 149MATCHED_POINTS=$(basename $1 | tr '.' '_')_matched.geojson
149jq . $RESPONSES | jq -s '.[0].features=[.[]|.features[]] | .[0] | del(.code)' > $RAW_RESPONSE 150jq . $RESPONSES | jq -s '.[0].features=[.[]|.features[]] | .[0] | del(.code) | .features=(.features|map(.properties.stroke="#00ff00"))' > $RAW_RESPONSE
150jq '.features=(.features|map(.geometry.coordinates=.properties.matchedPoints))' $RAW_RESPONSE > $MATCHED_POINTS 151jq ".features=(.features|map(select(.properties.confidence>=$THRESHOLD).geometry.coordinates=.properties.matchedPoints)|map(.properties.stroke=\"#0000ff\"))" $RAW_RESPONSE > $MATCHED_POINTS
152
153DEBUG=$(basename $1 | tr '.' '_')_total.geojson
154cat $RAW_REQUEST $RAW_RESPONSE $MATCHED_POINTS | jq -s '{type: "FeatureCollection", features: ([.[0]] + .[1].features + .[2].features)}' > $DEBUG