As a child, nothing was more exciting to me than a chance to ride on an airplane. And after enjoying playing with the seatbelt buckle and feeling the crazy push of take-off acceleration, I would usually settle in and page to the back of the in-flight magazine where the airline route maps were: where were we going today, and where could we go tomorrow?
We can see every destination available starting from Vancouver, Canada (airport code “YVR”) by making some custom SQL to join the airports table to the routes table and restricting to just the “YVR” routes:
That’s the data we want, but without the flight lines it lacks a sense of movement.
Simple Route Map
Our query is already joining the airports twice: once for the origin and once for the destination airport, so we can turn the end points into a line very easily using the ST_MakeLine() function:
That looks much better! But there’s something wrong about this map – actually two things wrong.
First, the routes are all straight lines, and they should be great circle routes, that’s how the airplanes fly!
Second, some of the routes go the wrong way around the world: no airline would fly from Vancouver to Sydney via Africa!
Great Circle Route Map
If we convert our lines into great circle routes, we can maybe kill both of these birds with one stone, since the great circle routes will go the right direction.
This is a bit complex, but reading the nested functions outwards starting from the ST_MakeLine(), we:
Cast the point-to-point line to the “geography” type, which understands edges (also known as connections between nodes) as great circles; then
Use the ST_Segmentize(geography) function to add points to the line along the great circle (so when we put it back on the flat map, it’ll appear curved); then
Cast the line back into the “geometry” type we use for flat mapping; and finally
Transform the coordinates of the line using ST_Transform(geometry, srid) to the web mercator projection we use on our flat maps.
The end result is really, really close!
But what is going on with those horizontal lines?
Great Circle Route Map with Dateline Fix
There’s a gap, right where the horizontal line appears.
Everything is fine until an edge on the great circle route tries to cross the dateline. Then it zings around the world in order to hook up to the next edge. Fundamentally our map still does not understand the circularity of the world, even though the edges we built do understand it. We have to work around the limitations of the flat map, by chopping our data at the dateline to avoid having edges that cross it.
And it works!
Of course, this is a route map of all flights leaving Vancouver (YVR), so it’s not exactly the kind of map you’d find in a in-flight magazine. However, it’s easy to build such a map, just by changing set of input airports we use to build the routes.
Where the existing query says:
Replace the airport filter with an airline filter of “AC” to get an Air Canada route map:
Or try “UA” for a United map, or “DL” for a Delta map.