docker-bastion/examples/osm-taxiways/tests/export-anchor.test.sh

51 lines
2.7 KiB
Bash
Executable File

#!/usr/bin/env bash
# The anchoring glue of export-icao-geojson.sh (steps 3b/3c), with osmium and ogr2ogr stubbed:
# the tagged match is a helipad far away, the aerodrome sits at the caller's coordinates.
# Needs bash + jq + awk only: bash examples/osm-taxiways/tests/export-anchor.test.sh
set -euo pipefail
DIR="$(cd "$(dirname "$0")/.." && pwd)"
T="$(mktemp -d)"
trap 'rm -rf "$T"' EXIT
mkdir -p "$T/bin" "$T/run"
cp "$DIR/export-icao-geojson.sh" "$DIR/select-aerodrome.jq" "$T/run/"
touch "$T/run/world.osm.pbf"
# osmium: every write just creates its -o file; counts are 0.
cat > "$T/bin/osmium" <<'EOF'
#!/usr/bin/env bash
[ "$1" = "fileinfo" ] && { echo 0; exit 0; }
while [ $# -gt 0 ]; do [ "$1" = "-o" ] && { touch "$2"; exit 0; }; shift; done
EOF
# ogr2ogr: by output name, the tagged far helipad, the aerodrome near the anchor, or nothing.
cat > "$T/bin/ogr2ogr" <<'EOF'
#!/usr/bin/env bash
out="$3"
case "$out" in
*aerodrome-*-pt.geojson)
echo '{"type":"FeatureCollection","features":[{"type":"Feature","properties":{"other_tags":"\"aeroway\"=>\"helipad\""},"geometry":{"type":"Point","coordinates":[-35.0,-15.0]}}]}' > "$out" ;;
*near-*-mp.geojson)
echo '{"type":"FeatureCollection","features":[{"type":"Feature","properties":{"aeroway":"aerodrome","name":"Field at our coordinates"},"geometry":{"type":"MultiPolygon","coordinates":[[[[-46.51,-22.71],[-46.49,-22.71],[-46.49,-22.69],[-46.51,-22.69],[-46.51,-22.71]]]]}}]}' > "$out" ;;
*) echo '{"type":"FeatureCollection","features":[]}' > "$out" ;;
esac
EOF
chmod +x "$T/bin/osmium" "$T/bin/ogr2ogr"
fail=0
check() { if [ "$2" = "$3" ]; then echo "ok $1"; else echo "FAIL $1: expected '$2', got '$3'"; fail=1; fi; }
cd "$T/run"
log=$(PATH="$T/bin:$PATH" OSM_PBF="$T/run/world.osm.pbf" bash ./export-icao-geojson.sh ABCD -22.700000 -46.500000 2>&1 || true)
check "falls back to the aerodrome at our coordinates" 1 "$(grep -c 'Using the aerodrome at our coordinates' <<< "$log")"
check "the export is centred on that field" 'Field at our coordinates' "$(jq -r '.features[0].properties.name' work/aerodrome-ABCD.geojson)"
rm -rf work ABCD-taxiways.geojson
log=$(PATH="$T/bin:$PATH" OSM_PBF="$T/run/world.osm.pbf" bash ./export-icao-geojson.sh ABCD 2>&1 || true)
check "without coordinates: no search, the tagged object stays" 'helipad' "$(jq -r '.match.aeroway' work/aerodrome-ABCD.geojson)"
check "without coordinates: no fallback line" 0 "$(grep -c 'searching aerodromes there' <<< "$log" || true)"
rm -rf work
log=$(PATH="$T/bin:$PATH" OSM_PBF="$T/run/world.osm.pbf" bash ./export-icao-geojson.sh ABCD '1;rm' -46.5 2>&1 || true)
check "invalid coordinates are ignored" 1 "$(grep -c 'Ignoring invalid coordinates' <<< "$log")"
exit $fail