Airport-Airline mapping with Basemap

In [4]:
from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
import pandas as pd
In [3]:
#projection indicates the 3d-2d projection type, llcrnrlat:latitude of lower left hand corner of the desired map domain
m = Basemap(projection = "merc", llcrnrlat = -80, urcrnrlat = 80, llcrnrlon = -180, urcrnrlon = 180)
In [5]:
airlines = pd.read_csv("/Users/33Phoebe/Documents/OneDrive/Data Scientist Path/Data Sets/open flight/airlines.csv")
airports = pd.read_csv("/Users/33Phoebe/Documents/OneDrive/Data Scientist Path/Data Sets/open flight/airports.csv")
routes = pd.read_csv("/Users/33Phoebe/Documents/OneDrive/Data Scientist Path/Data Sets/open flight/routes.csv")

datasets used in this proj come from OpenFlights, a tool that lets you map your flights around the world, search and filter them in all sorts of interesting ways, calculate statistics automatically, and share your flights and trips with friends and the entire world, visit website for most up-to-date info: https://openflights.org/data.html

link to the datasets I used:https://github.com/33phoebe/datasets/tree/master/open%20flight

In [7]:
airlines.head(), airports.head(), routes.head()
Out[7]:
(   id                                          name alias iata icao callsign  \
 0   1                                Private flight    \N    -  NaN      NaN   
 1   2                                   135 Airways    \N  NaN  GNL  GENERAL   
 2   3                                 1Time Airline    \N   1T  RNX  NEXTIME   
 3   4  2 Sqn No 1 Elementary Flying Training School    \N  NaN  WYT      NaN   
 4   5                               213 Flight Unit    \N  NaN  TFU      NaN   
 
           country active  
 0             NaN      Y  
 1   United States      N  
 2    South Africa      Y  
 3  United Kingdom      N  
 4          Russia      N  ,
    id                        name          city           country code  icao  \
 0   1                      Goroka        Goroka  Papua New Guinea  GKA  AYGA   
 1   2                      Madang        Madang  Papua New Guinea  MAG  AYMD   
 2   3                 Mount Hagen   Mount Hagen  Papua New Guinea  HGU  AYMH   
 3   4                      Nadzab        Nadzab  Papua New Guinea  LAE  AYNZ   
 4   5  Port Moresby Jacksons Intl  Port Moresby  Papua New Guinea  POM  AYPY   
 
    latitude   longitude  altitude  offset dst              timezone  
 0 -6.081689  145.391881      5282    10.0   U  Pacific/Port_Moresby  
 1 -5.207083  145.788700        20    10.0   U  Pacific/Port_Moresby  
 2 -5.826789  144.295861      5388    10.0   U  Pacific/Port_Moresby  
 3 -6.569828  146.726242       239    10.0   U  Pacific/Port_Moresby  
 4 -9.443383  147.220050       146    10.0   U  Pacific/Port_Moresby  ,
   airline airline_id source source_id dest dest_id codeshare  stops equipment
 0      2B        410    AER      2965  KZN    2990       NaN      0       CR2
 1      2B        410    ASF      2966  KZN    2990       NaN      0       CR2
 2      2B        410    ASF      2966  MRV    2962       NaN      0       CR2
 3      2B        410    CEK      2968  KZN    2990       NaN      0       CR2
 4      2B        410    CEK      2968  OVB    4078       NaN      0       CR2)
In [8]:
#basemap only accepts list values
longitudes = airports["longitude"].tolist()
latitudes = airports["latitude"].tolist()
x, y = m(longitudes, latitudes)
print(longitudes[:5], latitudes[:5])
print(x[:5],y[:5])
[145.391881, 145.7887, 144.295861, 146.72624199999998, 147.22005] [-6.081689, -5.207083, -5.826789, -6.569828, -9.443383]
[36181909.30105094, 36226033.53986971, 36060037.49493744, 36330283.404696316, 36385192.32317709] [14843790.192350345, 14941516.685582709, 14872287.531036133, 14789178.97017778, 14466473.84037962]
In [12]:
fig, ax = plt.subplots(figsize = (15, 20))
#use x, y in a basemap.scatter(s = markersize)
m.scatter(x, y, s=1)
#turn on the coastlines
m.drawcoastlines()
ax.set_title("Scaled Up Earth With Coastlines")
plt.show()
/Applications/anaconda3/lib/python3.6/site-packages/mpl_toolkits/basemap/__init__.py:3222: MatplotlibDeprecationWarning: The ishold function was deprecated in version 2.0.
  b = ax.ishold()
/Applications/anaconda3/lib/python3.6/site-packages/mpl_toolkits/basemap/__init__.py:3231: MatplotlibDeprecationWarning: axes.hold is deprecated.
    See the API Changes document (http://matplotlib.org/api/api_changes.html)
    for more details.
  ax.hold(b)
In [13]:
#read in geo_routes with combined source and destination airports for each route with the lat and lon, data combined by dataquest
geo_routes = pd.read_csv("/Users/33Phoebe/Documents/OneDrive/Data Scientist Path/Data Sets/open flight/geo_routes.csv")
geo_routes.info()
geo_routes.head(5)
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 67428 entries, 0 to 67427
Data columns (total 8 columns):
airline      67428 non-null object
source       67428 non-null object
dest         67428 non-null object
equipment    67410 non-null object
start_lon    67428 non-null float64
end_lon      67428 non-null float64
start_lat    67428 non-null float64
end_lat      67428 non-null float64
dtypes: float64(4), object(4)
memory usage: 4.1+ MB
Out[13]:
airline source dest equipment start_lon end_lon start_lat end_lat
0 2B AER KZN CR2 39.956589 49.278728 43.449928 55.606186
1 2B ASF KZN CR2 48.006278 49.278728 46.283333 55.606186
2 2B ASF MRV CR2 48.006278 43.081889 46.283333 44.225072
3 2B CEK KZN CR2 61.503333 49.278728 55.305836 55.606186
4 2B CEK OVB CR2 61.503333 82.650656 55.305836 55.012622
In [16]:
#because basemap doesn't handle well the routes with absolute diff larger than 180 degrees for either the latitude or longitude values.
#great circle shows the flying route that's shortest between two points on a 3D global

def create_great_circles(dataframe):
    for index, row in dataframe.iterrows():
        end_lat, start_lat = row['end_lat'], row['start_lat']
        end_lon, start_lon = row['end_lon'], row['start_lon']
        if abs(end_lat - start_lat) < 180 and abs(end_lon - start_lon) < 180:
            m.drawgreatcircle(start_lon, start_lat, end_lon, end_lat)
dfw = geo_routes[geo_routes["source"] == "DFW"]
fig, ax = plt.subplots(figsize=(15,20))
m = Basemap(projection='merc', llcrnrlat=-80, urcrnrlat=80, llcrnrlon=-180, urcrnrlon=180)
m.drawcoastlines()
create_great_circles(dfw)
plt.show()
/Applications/anaconda3/lib/python3.6/site-packages/mpl_toolkits/basemap/__init__.py:3260: MatplotlibDeprecationWarning: The ishold function was deprecated in version 2.0.
  b = ax.ishold()
/Applications/anaconda3/lib/python3.6/site-packages/mpl_toolkits/basemap/__init__.py:3269: MatplotlibDeprecationWarning: axes.hold is deprecated.
    See the API Changes document (http://matplotlib.org/api/api_changes.html)
    for more details.
  ax.hold(b)

social