docker-bastion/examples/osm-taxiways/tests/select-aerodrome.test.sh

69 lines
4.2 KiB
Bash
Raw Normal View History

#!/usr/bin/env bash
# Tests for select-aerodrome.jq (needs only jq): bash examples/osm-taxiways/tests/select-aerodrome.test.sh
set -euo pipefail
DIR="$(cd "$(dirname "$0")/.." && pwd)"
JQ="$DIR/select-aerodrome.jq"
fail=0
select_() { # select_ <lat|null> <lon|null> <<< geojson
jq -c --argjson lat "$1" --argjson lon "$2" --argjson nearkm 15 --argjson keepkm 5 --arg icao "${ICAO:-ABCD}" -f "$JQ"
}
check() { # check <name> <expected> <actual>
if [ "$2" = "$3" ]; then echo "ok $1"; else echo "FAIL $1: expected $2, got $3"; fail=1; fi
}
poly() { # poly <lon> <lat> <props-json>: a ~1 km square
jq -nc --argjson x "$1" --argjson y "$2" --argjson p "$3" \
'{type:"Feature",properties:$p,geometry:{type:"MultiPolygon",coordinates:[[[[$x,$y],[$x+0.01,$y],[$x+0.01,$y+0.01],[$x,$y+0.01],[$x,$y]]]]}}'
}
point() { # point <lon> <lat> <props-json>
jq -nc --argjson x "$1" --argjson y "$2" --argjson p "$3" '{type:"Feature",properties:$p,geometry:{type:"Point",coordinates:[$x,$y]}}'
}
fc() { jq -sc '{type:"FeatureCollection",features:.}'; }
# 1) Our field: an aerodrome polygon + its point; the same ICAO on a helipad ~1400 km away.
field=$(poly -46.50 -22.71 '{"aeroway":"aerodrome","name":"Our field"}')
fieldpt=$(point -46.495 -22.705 '{"other_tags":"\"aeroway\"=>\"aerodrome\",\"icao\"=>\"ABCD\""}')
helipad=$(point -35.0 -15.0 '{"other_tags":"\"aeroway\"=>\"helipad\",\"icao\"=>\"ABCD\""}')
out=$(printf '%s\n%s\n%s\n' "$helipad" "$field" "$fieldpt" | fc | select_ -22.70 -46.50)
check "anchored: the field near our coordinates, not the far helipad" '2 aerodrome' "$(jq -r '"\(.features|length) \(.match.aeroway)"' <<< "$out")"
check "anchored: distance is reported" 'true' "$(jq -r '.match.distance_km < 2' <<< "$out")"
# 2) Only the far helipad carries the ICAO: it is returned with its distance, so the caller
# can see the match is not at our coordinates and search around them instead.
out=$(printf '%s\n' "$helipad" | fc | select_ -22.70 -46.50)
check "far only: nearest returned, distance shows it is far" 'helipad true' "$(jq -r '"\(.match.aeroway) \(.match.distance_km > 1000)"' <<< "$out")"
# 3) Near our coordinates both an airfield and a helipad: the airfield wins even if a bit farther.
nearheli=$(point -46.501 -22.701 '{"aeroway":"helipad"}')
out=$(printf '%s\n%s\n' "$nearheli" "$field" | fc | select_ -22.70 -46.50)
check "near: aerodrome before helipad" 'aerodrome' "$(jq -r '.match.aeroway' <<< "$out")"
# 4) Two namesake aerodromes: the one at our coordinates.
other=$(poly 10.0 50.0 '{"aeroway":"aerodrome","name":"Namesake"}')
out=$(printf '%s\n%s\n' "$other" "$field" | fc | select_ -22.70 -46.50)
check "namesakes: the one at our coordinates" 'Our field' "$(jq -r '.features[0].properties.name' <<< "$out")"
# 5) No coordinates: aerodrome before helipad, polygon before point.
out=$(printf '%s\n%s\n%s\n' "$helipad" "$fieldpt" "$field" | fc | select_ null null)
check "no anchor: aerodrome polygon first" 'MultiPolygon aerodrome false' "$(jq -r '"\(.features[0].geometry.type) \(.match.aeroway) \(.match.anchored)"' <<< "$out")"
# 6) Searching around our coordinates (wrong coordinates on our side, say): an aerodrome there
# that carries ANOTHER ICAO is another airfield and is never chosen; an untagged one may be.
otherfield=$(poly -46.50 -22.71 '{"aeroway":"aerodrome","name":"Someone else","other_tags":"\"icao\"=>\"WXYZ\""}')
out=$(printf '%s\n' "$otherfield" | fc | select_ -22.70 -46.50)
check "another ICAO: not chosen" '0' "$(jq -r '.features | length' <<< "$out")"
untagged=$(poly -46.50 -22.71 '{"aeroway":"airstrip","name":"Untagged strip"}')
out=$(printf '%s\n%s\n' "$otherfield" "$untagged" | fc | select_ -22.70 -46.50)
check "untagged strip: chosen" 'Untagged strip' "$(jq -r '.features[0].properties.name' <<< "$out")"
ours=$(poly -46.50 -22.71 '{"aeroway":"aerodrome","name":"Ours","icao":"abcd"}')
out=$(printf '%s\n' "$ours" | fc | select_ -22.70 -46.50)
check "same ICAO in any case: chosen" 'Ours' "$(jq -r '.features[0].properties.name' <<< "$out")"
# 7) Empty and geometry-less input stays empty.
out=$(echo '{"type":"FeatureCollection","features":[{"type":"Feature","properties":{},"geometry":null}]}' | select_ 1 1)
check "empty: no features, no match" '0 null' "$(jq -r '"\(.features|length) \(.match)"' <<< "$out")"
exit $fail