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]:
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])
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()
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)
Out[13]:
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()