Skip to content

Instantly share code, notes, and snippets.

@liddiard
Last active February 21, 2020 05:12
Show Gist options
  • Save liddiard/cb9306bc441f93f98c0c67ab68eb2b1d to your computer and use it in GitHub Desktop.
Save liddiard/cb9306bc441f93f98c0c67ab68eb2b1d to your computer and use it in GitHub Desktop.
Take airport info from https://ourairports.com/data/ and transform it into a table that contains pertinent info like frequencies, runways, and elevation for diversions. Run on SQLite 3.
-- requires airports, runways, and airport_frequencies tables to be present with the correct data types
-- example output row:
-- ident name ATIS CTAF TWR GND elevation_ft runways
-- KHWD Hayward Executive Airport 126.7 120.2 118.9 121.4 52 10L-28R 3107'x75', 10R-28L 5694'x150'
SELECT
ident,
name,
-- "pivot" frequency data from rows to columns
MAX(CASE WHEN airport_frequencies.type = 'ATIS' THEN airport_frequencies.frequency_mhz END) ATIS,
MAX(CASE WHEN airport_frequencies.type = 'CTAF' THEN airport_frequencies.frequency_mhz END) CTAF,
MAX(CASE WHEN airport_frequencies.type = 'TWR' THEN airport_frequencies.frequency_mhz END) TWR,
MAX(CASE WHEN airport_frequencies.type = 'GND' THEN airport_frequencies.frequency_mhz END) GND,
elevation_ft,
-- only runways identifiers in one direction are stored, so we need to derive the reciprocal heading
GROUP_CONCAT(
DISTINCT ' ' || runways.le_ident || '-' ||
-- reciprocal heading formula
((runways.le_ident + 18) % 36) ||
-- swap "L" and "R" where applicable
CASE SUBSTR(runways.le_ident, -1)
WHEN 'L' THEN 'R'
WHEN 'R' THEN 'L'
ELSE ''
END ||
' ' || runways.length_ft || "'x" || runways.width_ft || "'") AS runways
FROM airports
JOIN airport_frequencies
ON airports.id = airport_frequencies.airport_ref
JOIN runways
ON airports.id = runways.airport_ref
-- list of airports to include, replace with your own
WHERE ident IN ('KMRY', 'KSNS', 'KCVH', 'KWVI', 'KE16', 'KRHV', 'KSJC', 'KNUQ', 'KPAO', 'KSQL', 'KHAF', 'KSFO', 'KOAK', 'KHWD', 'KLVK', 'KTCY', 'KC83', 'KCCR', 'KDVO', 'KO69', 'KSTS', 'KAPC', 'KSUU', 'KVCB', 'KO88', 'KEDU', 'KSMF', 'KSAC', 'KMCC', 'KMHR', 'KJAQ', 'KCPU', 'KSCK', 'KMOD', 'KKIC', 'K1C9', 'K1Q4', 'KLSN', 'K3O1', 'KMCE', 'KO15', 'KO27', 'KL53', 'KO20', 'K1O3', 'KF72', 'KJAQ', 'KRIU', 'KL36', 'KDWA', 'KO41', 'K0Q3', 'K0Q9')
GROUP BY ident
ORDER BY ident;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment