docker-bastion/examples/osm-taxiways/export-icao-geojson.sh

526 lines
24 KiB
Bash
Raw Normal View History

#!/usr/bin/env bash
set -e
AIRPORT="${1:-}"
# Optional: the caller's coordinates of this aerodrome (decimal degrees). With them the export
# picks the airfield there instead of whatever carries the ICAO tag worldwide (see 3b below).
ANCHOR_LAT="${2:-}"
ANCHOR_LON="${3:-}"
if [ -z "$AIRPORT" ]; then
echo "Usage: $0 ICAO_CODE [LAT LON]"
exit 1
fi
# The aeroway PBF (all aerodromes/runways/taxiways/aprons). Default: inside the osm-maps dir;
# a symlink there may point at a PBF mounted elsewhere in the container.
PBF="${OSM_PBF:-/osm-maps/aerodromes-world.osm.pbf}"
WORKDIR="work"
# The selection program next to this script (one airfield out of every match, see 3b).
SELECT_JQ="$(cd "$(dirname "$0")" && pwd)/select-aerodrome.jq"
# A match farther than this from the caller's coordinates is not "at our coordinates".
NEAR_KM=15
# Companions of the chosen object (its boundary polygon and its point) kept within this radius.
KEEP_KM=5
valid_coord() { # valid_coord <value> <max-abs>
[[ "$1" =~ ^-?[0-9]{1,3}(\.[0-9]+)?$ ]] && awk -v v="$1" -v m="$2" 'BEGIN { exit !(v >= -m && v <= m) }'
}
if [ -n "$ANCHOR_LAT$ANCHOR_LON" ]; then
if valid_coord "$ANCHOR_LAT" 90 && valid_coord "$ANCHOR_LON" 180; then
echo "Anchor: $ANCHOR_LAT, $ANCHOR_LON"
else
echo "⚠️ Ignoring invalid coordinates '$ANCHOR_LAT' '$ANCHOR_LON' (exporting by ICAO only)."
ANCHOR_LAT=""
ANCHOR_LON=""
fi
fi
# FAILSAFE: never export more than this area around the aerodrome (km^2). A broken/oversized
# aerodrome match — or a bbox-extract miss — must NEVER fall back to the whole (world) dataset,
# which is what produced the "entire Europe" blow-up. Everything gets clamped to a box centered
# on the aerodrome of at most this area. ~30 km^2 comfortably holds even large international
# airports while stopping a runaway export.
MAX_AREA_KM2=30
echo "Using PBF: $PBF"
echo "Airport: $AIRPORT"
echo "Work dir: $WORKDIR"
echo
mkdir -p "$WORKDIR"
# Clean up any stale intermediate files for this airport
rm -f "$WORKDIR"/*"$AIRPORT"* "$WORKDIR"/aerodrome-"$AIRPORT".geojson 2>/dev/null
rm -f "${AIRPORT}-taxiways.geojson" 2>/dev/null
# -------------------------
# 1-3) Extract THIS aerodrome directly by ICAO from the PBF.
# -------------------------
# Matches both Europe-style tagging (ref=ICAO, e.g. EDDF) AND ICAO-tag style (icao=ICAO
# with ref empty, e.g. US airports like KJFK), then converts its polygon(s) + point(s) to
# one GeoJSON. This replaces the old "build a whole-world aerodrome index, then jq-select
# by ICAO" approach, which was slow, and matched only top-level ref/iata/icao (so airports
# whose ICAO lives in other_tags — e.g. KJFK — were never found). GDAL's OSM driver needs
# the layer named explicitly (multipolygons/points) + interleaved reading.
AERO_GJ="$WORKDIR/aerodrome-$AIRPORT.geojson"
if [ ! -f "$AERO_GJ" ]; then
echo "1) Extracting aerodrome '$AIRPORT' by ICAO from $PBF ..."
osmium tags-filter "$PBF" nwr/icao="$AIRPORT" nwr/ref="$AIRPORT" \
-o "$WORKDIR/aerodrome-$AIRPORT.osm.pbf" --overwrite
echo "2) Converting aerodrome geometry (polygons + points) to GeoJSON ..."
ogr2ogr -f GeoJSON "$WORKDIR/aerodrome-$AIRPORT-mp.geojson" "$WORKDIR/aerodrome-$AIRPORT.osm.pbf" \
--config OGR_INTERLEAVED_READING YES multipolygons 2>/dev/null || true
ogr2ogr -f GeoJSON "$WORKDIR/aerodrome-$AIRPORT-pt.geojson" "$WORKDIR/aerodrome-$AIRPORT.osm.pbf" \
--config OGR_INTERLEAVED_READING YES points 2>/dev/null || true
for f in "$WORKDIR/aerodrome-$AIRPORT-mp.geojson" "$WORKDIR/aerodrome-$AIRPORT-pt.geojson"; do
[ -s "$f" ] || echo '{"type":"FeatureCollection","features":[]}' > "$f"
done
echo "3) Merging aerodrome polygon + point into $AERO_GJ ..."
jq -s '{ type:"FeatureCollection", features: ((.[0].features // []) + (.[1].features // [])) }' \
"$WORKDIR/aerodrome-$AIRPORT-mp.geojson" "$WORKDIR/aerodrome-$AIRPORT-pt.geojson" \
> "$AERO_GJ" 2>/dev/null || echo '{"type":"FeatureCollection","features":[]}' > "$AERO_GJ"
else
echo "1-3) Reusing existing $AERO_GJ"
fi
# -------------------------
# 3b) ONE airfield, not every object carrying this ICAO.
# -------------------------
# OSM reuses ICAO tags: a helipad or a namesake field on another continent can carry the same
# one. Merged together they centre the export box between them, or on the wrong object. Keep
# the match nearest the caller's coordinates (aerodrome/airstrip before helipads); without
# coordinates, aerodrome/airstrip polygons first. If no tagged object lies near the caller's
# coordinates, look for an aerodrome/airstrip around those coordinates instead, tag or not.
# The whole bbox logic below then centres on that one airfield.
select_aerodrome() { # select_aerodrome <in.geojson> <out.geojson>
local lat="${ANCHOR_LAT:-null}" lon="${ANCHOR_LON:-null}"
jq --argjson lat "$lat" --argjson lon "$lon" --argjson nearkm "$NEAR_KM" --argjson keepkm "$KEEP_KM" --arg icao "$AIRPORT" \
-f "$SELECT_JQ" "$1" > "$2"
}
match_km() { jq -r '.match.distance_km // "null"' "$1"; }
if [ ! -f "$SELECT_JQ" ]; then
echo "⚠️ $SELECT_JQ missing: keeping every object tagged $AIRPORT (old behaviour)."
else
SELECTED_GJ="$WORKDIR/aerodrome-$AIRPORT-selected.geojson"
select_aerodrome "$AERO_GJ" "$SELECTED_GJ"
echo "3b) Chosen: $(jq -c '.match' "$SELECTED_GJ") of $(jq '.features | length' "$AERO_GJ") tagged feature(s)"
KM=$(match_km "$SELECTED_GJ")
FAR=0
if [ -n "$ANCHOR_LAT" ]; then
if [ "$KM" = "null" ] || awk -v k="$KM" -v n="$NEAR_KM" 'BEGIN { exit !(k > n) }'; then FAR=1; fi
fi
if [ "$FAR" -eq 1 ]; then
echo "3c) No $AIRPORT-tagged object within ${NEAR_KM} km of $ANCHOR_LAT,$ANCHOR_LON: searching aerodromes there ..."
read -r MINLON MINLAT MAXLON MAXLAT <<< "$(awk -v la="$ANCHOR_LAT" -v lo="$ANCHOR_LON" -v km="$NEAR_KM" 'BEGIN {
c = cos(la * 3.14159265358979 / 180); if (c < 0.05) c = 0.05;
dla = km / 111.32; dlo = km / (111.32 * c);
printf "%.6f %.6f %.6f %.6f", lo - dlo, la - dla, lo + dlo, la + dla }')"
NEAR="$WORKDIR/near-$AIRPORT"
NEAR_GJ="$NEAR.geojson"
echo '{"type":"FeatureCollection","features":[]}' > "$NEAR_GJ"
if osmium extract --bbox "$MINLON,$MINLAT,$MAXLON,$MAXLAT" "$PBF" -o "$NEAR-area.osm.pbf" --overwrite 2>/dev/null \
&& osmium tags-filter "$NEAR-area.osm.pbf" nwr/aeroway=aerodrome,airstrip -o "$NEAR.osm.pbf" --overwrite 2>/dev/null; then
ogr2ogr -f GeoJSON "$NEAR-mp.geojson" "$NEAR.osm.pbf" --config OGR_INTERLEAVED_READING YES multipolygons 2>/dev/null || true
ogr2ogr -f GeoJSON "$NEAR-pt.geojson" "$NEAR.osm.pbf" --config OGR_INTERLEAVED_READING YES points 2>/dev/null || true
for f in "$NEAR-mp.geojson" "$NEAR-pt.geojson"; do
[ -s "$f" ] || echo '{"type":"FeatureCollection","features":[]}' > "$f"
done
jq -s '{ type:"FeatureCollection", features: ((.[0].features // []) + (.[1].features // [])) }' \
"$NEAR-mp.geojson" "$NEAR-pt.geojson" > "$NEAR_GJ" 2>/dev/null || true
fi
NEAR_SELECTED="$NEAR-selected.geojson"
select_aerodrome "$NEAR_GJ" "$NEAR_SELECTED"
NEAR_KM_FOUND=$(match_km "$NEAR_SELECTED")
if [ "$NEAR_KM_FOUND" != "null" ] && awk -v k="$NEAR_KM_FOUND" -v n="$NEAR_KM" 'BEGIN { exit !(k <= n) }'; then
echo " → Using the aerodrome at our coordinates: $(jq -c '.match' "$NEAR_SELECTED")"
SELECTED_GJ="$NEAR_SELECTED"
else
echo " → No aerodrome within ${NEAR_KM} km of our coordinates either; keeping the tagged match."
fi
fi
cp "$SELECTED_GJ" "$AERO_GJ"
fi
# -------------------------
# Validate geometry (avoid jq crashes later)
# -------------------------
HAS_GEOMETRY=$(jq '.features | length' "$AERO_GJ")
if [ "$HAS_GEOMETRY" -eq 0 ]; then
# No node/way/relation tagged icao=/ref=<ICAO> in the source PBF: this aerodrome simply
# isn't in OSM under this ICAO. That is EXPECTED, not an infra failure — signal it
# DISTINCTLY (exit 44) and on STDERR (broker mode only forwards the wrapper's stderr as the
# error) so the backend classifies it as `unavailable` (no admin notice) instead of a hard
# failure. The "No aerodrome geometry found" marker is also matched by the backend as a
# fallback for boxes still on the old exit-1 script.
msg="No aerodrome geometry found for $AIRPORT (not in OSM under this ICAO)"
echo "❌ $msg"
echo "$msg" >&2
exit 44
fi
# Check if polygon-like geometry exists
HAS_POLY=$(jq '
.features
| map(select(.geometry.type == "Polygon" or .geometry.type == "MultiPolygon"))
| length
' "$AERO_GJ")
if [ "$HAS_POLY" -eq 0 ]; then
echo "⚠️ Aerodrome '$AIRPORT' has NO polygon geometry (tiny or point aerodrome)."
echo " → Using 5km radius box around center point instead."
echo
fi
# -------------------------
# 4) Extract all taxiways, runways, and aprons once
# -------------------------
if [ ! -f "$WORKDIR/taxiways.osm.pbf" ]; then
echo "4) Extracting all taxiways..."
osmium tags-filter "$PBF" nwr/aeroway=taxiway -o "$WORKDIR/taxiways.osm.pbf"
else
echo "4) Reusing existing $WORKDIR/taxiways.osm.pbf"
fi
if [ ! -f "$WORKDIR/runways.osm.pbf" ]; then
echo "4b) Extracting all runways..."
osmium tags-filter "$PBF" nwr/aeroway=runway -o "$WORKDIR/runways.osm.pbf"
else
echo "4b) Reusing existing $WORKDIR/runways.osm.pbf"
fi
# If runways extract exists but is empty, regenerate (quick check; uses counts via fileinfo -e)
RUNWAY_WAYS=$(osmium fileinfo -e -g data.count.ways "$WORKDIR/runways.osm.pbf" 2>/dev/null || echo 0)
if [ "$RUNWAY_WAYS" -eq 0 ]; then
echo " ⚠️ runways extract is empty, regenerating from source PBF..."
osmium tags-filter "$PBF" nwr/aeroway=runway -o "$WORKDIR/runways.osm.pbf" --overwrite
RUNWAY_WAYS=$(osmium fileinfo -e -g data.count.ways "$WORKDIR/runways.osm.pbf" 2>/dev/null || echo 0)
echo " Runway ways after regen: $RUNWAY_WAYS"
if [ "$RUNWAY_WAYS" -eq 0 ]; then
echo " ⚠️ Still zero runways. Likely no aeroway=runway data in source PBF or wrong PBF path."
fi
fi
if [ ! -f "$WORKDIR/aprons.osm.pbf" ]; then
echo "4c) Extracting all aprons..."
osmium tags-filter "$PBF" nwr/aeroway=apron -o "$WORKDIR/aprons.osm.pbf"
else
echo "4c) Reusing existing $WORKDIR/aprons.osm.pbf"
fi
APRON_WAYS=$(osmium fileinfo -e -g data.count.ways "$WORKDIR/aprons.osm.pbf" 2>/dev/null || echo 0)
if [ "$APRON_WAYS" -eq 0 ]; then
echo " ⚠️ aprons extract is empty, regenerating from source PBF..."
osmium tags-filter "$PBF" nwr/aeroway=apron -o "$WORKDIR/aprons.osm.pbf" --overwrite
APRON_WAYS=$(osmium fileinfo -e -g data.count.ways "$WORKDIR/aprons.osm.pbf" 2>/dev/null || echo 0)
echo " Apron ways after regen: $APRON_WAYS"
if [ "$APRON_WAYS" -eq 0 ]; then
echo " ⚠️ Still zero aprons. Likely no aeroway=apron data in source PBF or wrong PBF path."
fi
fi
# -------------------------
# 5) Extract taxiways, runways, and aprons for the specific ICAO
# -------------------------
TAXI_OSM="$WORKDIR/taxiways_${AIRPORT}.osm.pbf"
RUNWAY_OSM="$WORKDIR/runways_${AIRPORT}.osm.pbf"
APRON_OSM="$WORKDIR/aprons_${AIRPORT}.osm.pbf"
BUILDING_OSM="$WORKDIR/buildings_${AIRPORT}.osm.pbf"
# Compute a bbox that is ALWAYS centered on the aerodrome and NEVER larger than MAX_AREA_KM2.
# Gathers every coordinate of the matched aerodrome feature(s) (any geometry type), takes the
# centroid, and clamps each half-extent to a centered box of <= MAX_AREA_KM2 — with a small
# margin so runways just outside the polygon are still caught, and a floor so point-only
# aerodromes still get a usable box. Prints "MINLON MINLAT MAXLON MAXLAT", or "" if no coords.
calculate_capped_bbox() {
local aero_geojson="$1"
jq -r \
--argjson maxarea "$MAX_AREA_KM2" \
--argjson margin 0.6 \
--argjson minhalf 1.2 '
def coords: [ .features[].geometry.coordinates | .. | arrays
| select(length == 2 and (.[0]|type) == "number" and (.[1]|type) == "number") ];
coords as $p
| if ($p | length) == 0 then "" else
($p | map(.[0])) as $lo | ($p | map(.[1])) as $la
| ($lo | min) as $mnlo | ($lo | max) as $mxlo | ($la | min) as $mnla | ($la | max) as $mxla
| (($mnlo + $mxlo) / 2) as $clon
| (($mnla + $mxla) / 2) as $clat
| (($maxarea | sqrt) / 2) as $maxhalf # max half-side (km)
| (($clat * 3.14159265358979 / 180) | cos) as $coslat
| ((($mxla - $mnla) / 2) * 111.32) as $rawhalflat # raw half-extent (km)
| ((($mxlo - $mnlo) / 2) * 111.32 * $coslat) as $rawhalflon
| ([ [ ($rawhalflat + $margin), $maxhalf ] | min, $minhalf ] | max) as $hlat
| ([ [ ($rawhalflon + $margin), $maxhalf ] | min, $minhalf ] | max) as $hlon
| ($hlat / 111.32) as $dlat
| ($hlon / (111.32 * $coslat)) as $dlon
| "\($clon - $dlon) \($clat - $dlat) \($clon + $dlon) \($clat + $dlat)"
end
' "$aero_geojson" 2>/dev/null
}
# A tiny out-of-data bbox → a valid, EMPTY .osm.pbf. Used instead of copying the whole
# (world) dataset on any miss — an empty layer is correct; the whole planet is not.
extract_empty() {
osmium extract --bbox "0,0,0.001,0.001" "$1" -o "$2" --overwrite 2>/dev/null || true
}
# Extract features within the capped, aerodrome-centered bbox. NEVER copies the whole dataset.
extract_with_bbox() {
local input_pbf="$1"
local output_pbf="$2"
local aero_geojson="$3"
local feature_type="$4"
BBOX_STR=$(calculate_capped_bbox "$aero_geojson")
if [ -z "$BBOX_STR" ] || [ "$BBOX_STR" = "null" ]; then
echo " ⚠️ No usable coordinates for $feature_type; emitting EMPTY extract (refusing whole-dataset copy)."
extract_empty "$input_pbf" "$output_pbf"
return
fi
read MINLON MINLAT MAXLON MAXLAT <<< "$BBOX_STR"
echo " → Capped bbox (<= ${MAX_AREA_KM2} km2, centered on aerodrome): $MINLON,$MINLAT,$MAXLON,$MAXLAT"
osmium extract --bbox "$MINLON","$MINLAT","$MAXLON","$MAXLAT" \
"$input_pbf" -o "$output_pbf" --overwrite 2>/dev/null || {
echo " ⚠️ bbox extraction failed for $feature_type; emitting EMPTY (not the whole dataset)."
extract_empty "$input_pbf" "$output_pbf"
}
}
if [ ! -f "$TAXI_OSM" ]; then
echo "5) Extracting taxiways for '$AIRPORT' to $TAXI_OSM ..."
extract_with_bbox "$WORKDIR/taxiways.osm.pbf" "$TAXI_OSM" "$AERO_GJ" "taxiways"
else
echo "5) Reusing existing $TAXI_OSM"
fi
if [ ! -f "$RUNWAY_OSM" ]; then
echo "5b) Extracting runways for '$AIRPORT' to $RUNWAY_OSM ..."
extract_with_bbox "$WORKDIR/runways.osm.pbf" "$RUNWAY_OSM" "$AERO_GJ" "runways"
# Check result — 0 within the capped box means this airport simply has no mapped runways
# (or they use different tagging). Leave it EMPTY; never fall back to the whole dataset.
RUNWAY_EXTRACTED_WAYS=$(osmium fileinfo -e -g data.count.ways "$RUNWAY_OSM" 2>/dev/null || echo 0)
if [ "$RUNWAY_EXTRACTED_WAYS" -eq 0 ]; then
echo " ⚠️ No runways within the capped bbox — leaving empty (NOT copying the whole dataset)."
fi
else
echo "5b) Reusing existing $RUNWAY_OSM"
fi
if [ ! -f "$APRON_OSM" ]; then
echo "5c) Extracting aprons for '$AIRPORT' to $APRON_OSM ..."
extract_with_bbox "$WORKDIR/aprons.osm.pbf" "$APRON_OSM" "$AERO_GJ" "aprons"
# Check result — 0 within the capped box means no mapped aprons for this airport. Leave it
# EMPTY; never fall back to the whole dataset.
APRON_EXTRACTED_WAYS=$(osmium fileinfo -e -g data.count.ways "$APRON_OSM" 2>/dev/null || echo 0)
if [ "$APRON_EXTRACTED_WAYS" -eq 0 ]; then
echo " ⚠️ No aprons within the capped bbox — leaving empty (NOT copying the whole dataset)."
fi
else
echo "5c) Reusing existing $APRON_OSM"
fi
# 5d) Buildings (terminals / hangars / generic footprints) for immersion. Unlike the aeroway
# layers we CANNOT tags-filter the whole PBF for nwr/building first (buildings are a huge global
# set) — so we slice the capped aerodrome bbox out of the PBF, then filter buildings from that
# small slice. Empty is fine (e.g. if the source PBF was filtered to aeroway only).
if [ ! -f "$BUILDING_OSM" ]; then
echo "5d) Extracting buildings for '$AIRPORT' to $BUILDING_OSM ..."
BBOX_STR=$(calculate_capped_bbox "$AERO_GJ")
if [ -z "$BBOX_STR" ] || [ "$BBOX_STR" = "null" ]; then
echo " ⚠️ No usable bbox for buildings; emitting EMPTY."
extract_empty "$PBF" "$BUILDING_OSM"
else
read MINLON MINLAT MAXLON MAXLAT <<< "$BBOX_STR"
AREA_OSM="$WORKDIR/area_${AIRPORT}.osm.pbf"
osmium extract --bbox "$MINLON","$MINLAT","$MAXLON","$MAXLAT" "$PBF" -o "$AREA_OSM" --overwrite 2>/dev/null || extract_empty "$PBF" "$AREA_OSM"
osmium tags-filter "$AREA_OSM" nwr/building nwr/aeroway=terminal nwr/aeroway=hangar nwr/aeroway=gate -o "$BUILDING_OSM" --overwrite 2>/dev/null || extract_empty "$PBF" "$BUILDING_OSM"
fi
BUILDING_WAYS=$(osmium fileinfo -e -g data.count.ways "$BUILDING_OSM" 2>/dev/null || echo 0)
echo " Building ways: $BUILDING_WAYS"
else
echo "5d) Reusing existing $BUILDING_OSM"
fi
# -------------------------
# 6) Convert taxiways, runways, and aprons to GeoJSON and merge into one
# -------------------------
OUT_GJ="${AIRPORT}-taxiways.geojson"
# Temp files for individual conversions
TAXI_GJ="$WORKDIR/taxiways_${AIRPORT}.geojson"
RUNWAY_GJ="$WORKDIR/runways_${AIRPORT}.geojson"
APRON_GJ="$WORKDIR/aprons_${AIRPORT}.geojson"
BUILDING_GJ="$WORKDIR/buildings_${AIRPORT}.geojson"
# Helper function to check if GeoJSON has features
has_features() {
local file="$1"
if [ -f "$file" ]; then
jq '.features | length' "$file" 2>/dev/null || echo 0
else
echo 0
fi
}
if [ ! -f "$TAXI_GJ" ] || [ ! -s "$TAXI_GJ" ]; then
echo "6) Generating taxiways GeoJSON ..."
ogr2ogr \
-f GeoJSON "$TAXI_GJ" \
"$TAXI_OSM" \
--config OGR_INTERLEAVED_READING YES \
-nln "taxiways"
TAXIWAY_FEATURE_COUNT=$(has_features "$TAXI_GJ")
echo " → Found $TAXIWAY_FEATURE_COUNT taxiway features"
fi
if [ ! -f "$RUNWAY_GJ" ] || [ ! -s "$RUNWAY_GJ" ]; then
echo "6b) Generating runways GeoJSON ..."
ogr2ogr \
-f GeoJSON "$RUNWAY_GJ" \
"$RUNWAY_OSM" \
--config OGR_INTERLEAVED_READING YES \
-nln "runways"
# Check if runways GeoJSON is empty after export
RUNWAY_FEATURE_COUNT=$(has_features "$RUNWAY_GJ")
echo " → Found $RUNWAY_FEATURE_COUNT runway features"
if [ "$RUNWAY_FEATURE_COUNT" -eq 0 ]; then
echo "⚠️ WARNING: runways_${AIRPORT}.geojson is empty (no features found)"
echo " This may indicate runways aren't available for this airport or use different tagging."
fi
fi
if [ ! -f "$APRON_GJ" ] || [ ! -s "$APRON_GJ" ]; then
echo "6c) Generating aprons GeoJSON ..."
ogr2ogr \
-f GeoJSON "$APRON_GJ" \
"$APRON_OSM" \
--config OGR_INTERLEAVED_READING YES \
-nln "aprons"
# Check if aprons GeoJSON is empty after export
APRON_FEATURE_COUNT=$(has_features "$APRON_GJ")
echo " → Found $APRON_FEATURE_COUNT apron features"
if [ "$APRON_FEATURE_COUNT" -eq 0 ]; then
echo "⚠️ WARNING: aprons_${AIRPORT}.geojson is empty (no features found)"
echo " This may indicate aprons aren't available for this airport or use different tagging."
fi
fi
if [ ! -f "$BUILDING_GJ" ] || [ ! -s "$BUILDING_GJ" ]; then
echo "6e) Generating buildings GeoJSON ..."
# Buildings are closed ways → the GDAL OSM driver emits them on the multipolygons layer.
ogr2ogr \
-f GeoJSON "$BUILDING_GJ" \
"$BUILDING_OSM" \
--config OGR_INTERLEAVED_READING YES \
multipolygons \
-nln "buildings" 2>/dev/null || echo '{"type":"FeatureCollection","features":[]}' > "$BUILDING_GJ"
echo " → Found $(has_features "$BUILDING_GJ") building features"
fi
# Always regenerate the merged output with feature_type properties
TAXI_COUNT=$(has_features "$TAXI_GJ")
RUNWAY_COUNT=$(has_features "$RUNWAY_GJ")
APRON_COUNT=$(has_features "$APRON_GJ")
echo "6d) Merging all layers into $OUT_GJ ..."
# Ensure all files exist and have valid GeoJSON; create empty ones if missing
for gj_file in "$TAXI_GJ" "$RUNWAY_GJ" "$APRON_GJ" "$BUILDING_GJ"; do
if [ ! -f "$gj_file" ] || [ ! -s "$gj_file" ]; then
echo '{"type":"FeatureCollection","features":[]}' > "$gj_file"
fi
done
jq -s '
{
type: "FeatureCollection",
features: (
[
(.[0].features // [])[]
| .properties.layer = "taxiway"
| if (.properties.other_tags // "" | contains("holding_position")) then
.properties.feature_type = "holding_point"
else
.properties.feature_type = "taxiway"
end
| if (.properties.ref // "") == "" and (.properties.other_tags // "" | test("\"ref\"\\s*=>\\s*\"([^\"]+)\"")) then
.properties.ref = (.properties.other_tags | match("\"ref\"\\s*=>\\s*\"([^\"]+)\"") | .captures[0].string)
else
.
end
| if (.properties.name // "") == "" and (.properties.other_tags // "" | test("\"name\"\\s*=>\\s*\"([^\"]+)\"")) then
.properties.name = (.properties.other_tags | match("\"name\"\\s*=>\\s*\"([^\"]+)\"") | .captures[0].string)
else
.
end
] +
[
(.[1].features // [])[]
| .properties.layer = "runway"
| .properties.feature_type = "runway"
| if (.properties.ref // "") == "" and (.properties.other_tags // "" | test("\"ref\"\\s*=>\\s*\"([^\"]+)\"")) then
.properties.ref = (.properties.other_tags | match("\"ref\"\\s*=>\\s*\"([^\"]+)\"") | .captures[0].string)
else
.
end
] +
[
(.[2].features // [])[]
| .properties.layer = "apron"
| .properties.feature_type = "apron"
] +
[
(.[3].features // [])[]
| select(.geometry.type == "Polygon" or .geometry.type == "MultiPolygon")
| .properties.layer = "aerodrome"
| .properties.feature_type = "aerodrome"
] +
[
(.[4].features // [])[]
| select(.geometry.type == "Polygon" or .geometry.type == "MultiPolygon")
| .properties.layer = "building"
| .properties.feature_type = "building"
]
)
}
' "$TAXI_GJ" "$RUNWAY_GJ" "$APRON_GJ" "$AERO_GJ" "$BUILDING_GJ" > "$OUT_GJ"
# Validate merged output
TOTAL_FEATURES=$(jq '.features | length' "$OUT_GJ")
TAXIWAY_FINAL=$(jq '[.features[] | select(.properties.feature_type == "taxiway")] | length' "$OUT_GJ")
HOLDING_FINAL=$(jq '[.features[] | select(.properties.feature_type == "holding_point")] | length' "$OUT_GJ")
RUNWAY_FINAL=$(jq '[.features[] | select(.properties.feature_type == "runway")] | length' "$OUT_GJ")
APRON_FINAL=$(jq '[.features[] | select(.properties.feature_type == "apron")] | length' "$OUT_GJ")
BUILDING_FINAL=$(jq '[.features[] | select(.properties.feature_type == "building")] | length' "$OUT_GJ")
BOUNDARY_FINAL=$(jq '[.features[] | select(.properties.feature_type == "aerodrome")] | length' "$OUT_GJ")
echo "✓ Merged successfully: $TOTAL_FEATURES total features"
echo " - Taxiways: $TAXIWAY_FINAL"
echo " - Holding Points: $HOLDING_FINAL"
echo " - Runways: $RUNWAY_FINAL"
echo " - Aprons: $APRON_FINAL"
echo " - Buildings: $BUILDING_FINAL"
echo " - Boundary: $BOUNDARY_FINAL"
echo "Done. Complete airport data for '$AIRPORT' is in:"
echo " - $OUT_GJ"