Playing with Airports in Python


Airports are pretty cool. As a kid, every time I went to SFO, I'd invariably end up in a cooler place on the other side (think MCO, MDW, or BOS). I began to associate airports with vacation, family, and fun. Today, they're just as fascinating! Here, we play with most of them on the globe!

Step 1: Get Airport Information

OpenFlights is a great resource for airport information. Here, we're simply interested in the longitude, latitude, and ICAO code of each airport. And there are about 7,000 airports! Not all of them have ICAO codes, so some are quite obscure. We first read the data with Python:

import pandas as pd

# read data from airports.dat (
airports = pd.read_csv('airports.dat', header=None)
airports.set_index(5, inplace=True)

Step 2: Plot the Airports

We use the matplotlib extension Basemap to plot each airport on a map of the globe.

from mpl_toolkits.basemap import Basemap
from matplotlib import pyplot as plt

# generate map on which to plot airports
map = Basemap(projection='mill', resolution='i')
map.fillcontinents(color = 'coral', lake_color='aqua')

# plot point for each airport
for idx, val in airports.iterrows():
    icao = idx
    latitude = val[6]
    longitude = val[7]
    x,y = map(longitude, latitude)
    map.plot(x, y, 'ro', markersize=2)

# view plot

Step 3: It's All About Location

Next, we calculate the great circle distance (GCD) between each airport. Note that we are only considering airports with ICAO codes. That's almost 25 million calculations (if you treat A to B = B to A)! Luckily, it's not too bad. In fact, with geopy, it's a breeze. Here's how it's done:

from geopy.distance import great_circle

# calculate GCD between each airport
gcds = {} # example gcds['KSFO']['KLAX'] = GCD in miles 
for icao_origin, val_origin in airports.iterrows():
    gcds[icao_origin] = {}
    for icao_destination, val_destination in airports.iterrows():
        origin_lon = val_origin[7]
        origin_lat = val_origin[6]        
        destination_lon = val_destination[7]
        destination_lat = val_destination[6]
        origin_coords = (origin_lat, origin_lon)
        destination_coords = (destination_lat, destination_lon)
        if gcds.has_key(icao_destination):
            if gcds[icao_destination].has_key(icao_origin):
                gcds[icao_origin][icao_destination] = gcds[icao_destination][icao_origin]
            gcds[icao_origin][icao_destination] = great_circle(origin_coords, destination_coords).miles

Now, we can have some fun! I exported the "gcds" dictionary created above into an Excel spreadsheet. Using the spreadsheet, I was able to answer the following questions.

What two airports are the farthest apart from each other?

SKNV (Neiva, Colombia) and WIPP (Palembang, South Sumatra) are 12,437 miles apart. Even the Airbus A380 could not cover this distance!

What is the most isolated airport?

SCIP (Mataveri International Airport on Easter Island) takes the prize!  It is 1,615 miles away from the closest airport. The second most isolated is Wilkins Runway in Antarctica (1,313 miles away from the closest airport). Coincidentally, if you take the GCDs from Wilkins Runway to all other airports in the world and calculate the median, the result is 8,873 miles— the highest value of all airports. This is a more holistic measure of isolation, but it undoubtedly underpins Antarctica's isolation from the rest of the continents.