This is an example of plotting flight paths from the OpenFlights dataset with the gcmap Python package.
The OpenFlights dataset contains three files, airports.dat
, routes.dat
, and airlines.dat
. We are interested in the first two. We'll use airports.dat
to get the coordinates of each airport, and routes to determine which airports are paired by a route.
First, we use pandas to load the data.
import pandas as pd
import numpy as np
ROUTE_COLS = ('airline_name', 'airline_id', 'source_code', 'source_id', 'dest_code', 'dest_id', 'codeshare', 'stops', 'equiptment')
AIRPORT_COLS = ('airport_id', 'airport_name', 'city', 'country', 'iata', 'icao', 'latitude', 'longitude', 'altitude', 'timezone', 'dst')
routes = pd.read_csv('routes.dat', header=None, names=ROUTE_COLS, na_values=['\\N'])
airports = pd.read_csv('airports.dat', header=None, names=AIRPORT_COLS)
There may be multiple routes between two airports. Here we combine them into one row per pair, with a column cnt
counting the number of times each pair appears.
airport_pairs = routes.groupby(('source_id', 'dest_id')).size()
airport_pairs = airport_pairs.reset_index()
airport_pairs.columns = ('source_id', 'dest_id', 'cnt')
Merge the airport_pairs
table with the airports
table to get the geocoordinates for each airport.
airport_pairs = airport_pairs.merge(airports, left_on='source_id', right_on='airport_id') \
.merge(airports, left_on='dest_id', right_on='airport_id', suffixes=('_source', '_dest'))
Now, bring in GCMapper
from the gcmap
package to render the visualization.
from gcmap import GCMapper
gcm = GCMapper()
gcm.set_data(airport_pairs.longitude_source, airport_pairs.latitude_source, airport_pairs.longitude_dest, airport_pairs.latitude_dest, airport_pairs.cnt)
img = gcm.draw()
The result is a Python Image Library image, which we can save as a png
(among a handful of other formats).
img.save('test.png')
from IPython.core import display
display.Image(filename='test.png')