# One airfield out of every OSM object that carries the requested ICAO (or out of the # aerodromes found around the caller's coordinates). OSM reuses tags: a helipad or a # namesake field on another continent can carry the same ICAO, and merging them all # centres the export box somewhere between them. # # Input: a GeoJSON FeatureCollection (polygons + points, as ogr2ogr writes them). # Args: --argjson lat --argjson lon the caller's coordinates # --argjson nearkm radius in which a match counts as "at our coordinates" # --argjson keepkm companions of the chosen object kept with it (its # boundary polygon and its point are separate features) # --arg icao the requested code: an object tagged icao= # is another airfield and never chosen ("" = no check) # Output: the chosen object (+ companions) as a FeatureCollection, plus `match`: # {distance_km, aeroway, anchored, candidates}. Empty input → empty collection. # # Choice: with coordinates, the objects within `nearkm` of them, aerodrome/airstrip before # other kinds before heliport/helipad, then the nearest; if none is that close, simply the # nearest. Without coordinates: aerodrome/airstrip first, polygons before points. def aeroway: (.properties.aeroway // ((.properties.other_tags // "") | (capture("\"aeroway\"=>\"(?[^\"]*)\"") // {}) | .v) // ""); def rank: aeroway as $a | if ($a == "aerodrome" or $a == "airstrip") then 0 elif ($a == "heliport" or $a == "helipad") then 2 else 1 end; def points: [ .geometry.coordinates? | .. | arrays | select(length >= 2 and (.[0] | type) == "number" and (.[1] | type) == "number") ]; def centre: points as $p | if ($p | length) == 0 then null else [ ($p | map(.[0]) | add / length), ($p | map(.[1]) | add / length) ] end; # Equirectangular distance in km between two [lon, lat] points: plenty at airfield scale. def km($a; $b): ((($a[1] + $b[1]) / 2) * 3.14159265358979 / 180 | cos) as $c | (((($a[0] - $b[0]) * $c) | . * .) + (($a[1] - $b[1]) | . * .)) | sqrt * 111.32; def ispoly: (.geometry.type == "Polygon" or .geometry.type == "MultiPolygon"); # The object's own icao tag (top level or in other_tags), "" when it has none. `ref` is left # out on purpose: it often holds a national code, not the ICAO. def icaotag: (.properties.icao // ((.properties.other_tags // "") | (capture("\"icao\"=>\"(?[^\"]*)\"") // {}) | .v) // "") | ascii_upcase; (if $lat != null and $lon != null then [$lon, $lat] else null end) as $anchor | [ (.features // [])[] | select(.geometry != null) | select($icao == "" or icaotag == "" or icaotag == ($icao | ascii_upcase)) | . + {_c: centre, _r: rank} | select(._c != null) | . + {_d: (if $anchor != null then km(._c; $anchor) else 0 end)} ] as $all | ($all | map(select($anchor == null or ._d <= $nearkm))) as $near | (if ($near | length) > 0 then $near | sort_by(._r, ._d, (if ispoly then 0 else 1 end)) else $all | sort_by(._d, ._r) end) as $ranked | if ($ranked | length) == 0 then { type: "FeatureCollection", features: [], match: null } else $ranked[0] as $best | { type: "FeatureCollection", features: [ $ranked[] | select(._r <= $best._r and km(._c; $best._c) <= $keepkm) | del(._c, ._r, ._d) ], match: { distance_km: (if $anchor != null then ($best._d * 10 | round / 10) else null end), aeroway: ($best | aeroway), anchored: ($anchor != null), candidates: ($all | length) } } end