Load the airport network data into networkx


In [1]:
import numpy as np
import networkx as nx
import matplotlib.pyplot as plt
%matplotlib inline
import scipy.optimize as opt

Load the adjacency matrix (csv file to numpy array):


In [5]:
A = np.loadtxt('../data/airport/air500matrix.csv', dtype=int, delimiter=',')

Load the airport names (IATA codes) and lat/long information:


In [39]:
latlong_data=np.loadtxt('../data/airport/latlongs.dat', dtype='str', delimiter=',')
node_labels=[str(i[2:-1]).strip() for i in latlong_data[:,0]]
node_pos=[(float(i[3:-2]), float(j[3:-1])) for i,j in latlong_data[:,1:]]

Create a networkx graph from the adjacency matrix:


In [43]:
T = nx.Graph(A)

Label the nodes with the airport names:


In [44]:
N = len(node_labels)
label_mapping = dict(zip(range(N), node_labels))
T = nx.relabel_nodes(T, label_mapping)

Add the latitudes and longitudes as attributes:


In [45]:
nx.set_node_attributes(T, "latlon", dict(zip(node_labels, node_pos)))

In [47]:
nx.write_gpickle(T, "../data/airport/big_airportnet.gpickle")

Check it out:


In [20]:
T.node['FRA']['latlon']


Out[20]:
(8.5431249999999999, 50.026420999999999)

Plot the network:


In [21]:
nx.draw(T)


/usr/lib/pymodules/python2.7/networkx/drawing/layout.py:514: RuntimeWarning: divide by zero encountered in longlong_scalars
  pos[:,i]*=scale/lim

In [22]:
nx.draw_networkx(T, with_labels=False, node_size=10, alpha=0.6, width=0.1)



In [23]:
nx.draw_networkx(T, with_labels=False, node_size=10, alpha=0.6, width=0.07, pos=nx.random_layout(T))


There are many other layouts available. please expore nx.layouts

Now let's plot the airports over a map projection:


In [24]:
from mpl_toolkits.basemap import Basemap
import matplotlib.colors as colors

In [25]:
bmap=Basemap(llcrnrlon=-170, llcrnrlat=-48.7,urcrnrlon=179.99, urcrnrlat=70.8, lat_0=45, lon_0=10, resolution='h')

In [26]:
def plot_over_basemap(G, node_colors=None):
    fig = plt.figure(figsize=(25,25), dpi=100)
    ax = fig.add_axes([0.1, 0.1, 0.8, 0.8])
    dummy=bmap.drawcoastlines(color='white')
    dummy2=bmap.drawcountries(color='white')
    dummy3=bmap.fillcontinents(color='0.8', alpha=0.6)
    
    node_labels=G.nodes()
    pos_on_map=[bmap(*G.node[label]['latlon']) for label in node_labels]
    posdict=dict(zip(node_labels, pos_on_map))
    
    if node_colors is not None:
        nodes=nx.draw_networkx_nodes(G, with_labels=False, node_size=25, alpha=1.0, width=0.1, pos=posdict, node_color=node_colors, cmap=plt.get_cmap('cool'))
        nx.draw_networkx_edges(G, pos=posdict, alpha=0.4)
        plt.colorbar(nodes, orientation='horizontal', pad=0.001, aspect=40)
        plt.axis('off')
    else:
        nx.draw_networkx(G, with_labels=False, node_size=25, alpha=1.0, width=0.1, pos=posdict)

In [27]:
plot_over_basemap(T)


This is way too big. Let's consider only the airports that fall in a lat/lon box:


In [5]:
airports_eurasia=np.loadtxt("data/eurasia_airport_labels.txt",dtype=str)

In [29]:
G=T.subgraph(airports_eurasia)

In [30]:
bmap=Basemap(llcrnrlon=-18.32, llcrnrlat=-49,urcrnrlon=179.99, urcrnrlat=71, lat_0=45, lon_0=10, resolution='h')

In [31]:
plot_over_basemap(G)