# Importing Airports2 Dataset which
# represent american flight travel data.
from bokeh.io import show
from bokeh.tile_providers import STAMEN_TERRAIN, STAMEN_TONER
from bokeh.plotting import figure
from pyproj import Proj, transform
import pandas as pd
df = pd.read_csv("./Airports2.csv")
# Hence Dataset is huge so selecting fewer rows.
df = df[1:1000]
# Converting Longitude and Latitudes to x,y coordinates
inProj = Proj(init='epsg:3857')
outProj = Proj(init='epsg:4326')
cols = ['Dest_airport_long', 'Dest_airport_lat',
'Org_airport_long', 'Org_airport_lat']
lines_x, lines_y = [], []
lons, lats = [], []
for lon_dest, lat_dest, lon_orig, lat_orig in df[cols].values:
lon_orig, lat_orig = transform(outProj, inProj, lon_orig, lat_orig)
lon_dest, lat_dest = transform(outProj, inProj, lon_dest, lat_dest)
# Append converted Coordinates.
lons.append(lon_dest)
lats.append(lat_dest)
# Creating Source and Destination points for connections.
lines_x.append([lon_orig, lon_dest])
lines_y.append([lat_orig, lat_dest])
# Two way connection points
df["MercatorX"] = lons
df["MercatorY"] = lats
# Loading Important Functions and Libraries
# Hence Connections needed to be represented so,
# selecting STAMEN_TONER
stamen_toner = get_provider(STAMEN_TONER)
# Selecting world coordinates
lon1, lat1 = transform(outProj, inProj, -150, -75)
lon2, lat2 = transform(outProj, inProj, 140, 75)
# Pass source-destination connections,
# tooltips to be displayed and title
m = figure(plot_width=800, plot_height=700,
x_range=(lon1, lon2), y_range=(lat1, lat2),
x_axis_type="mercator", y_axis_type="mercator",
tooltips=[("Origin_city", "@Origin_city"),
("Destination_city", "@Destination_city")],
title="Flight Travels in America")
# Add tile for stamen_toner
m.add_tile(stamen_toner)
# Drawing Multiple Lines.
m.multi_line(lines_x, lines_y, color="red")
# Circling the points and lines
m.circle(x="MercatorX", y="MercatorY", size=2,
alpha=0.8, color="red", source=df)
# Displaying the map.
show(m)