Planning to Travel Around the World, with Python?
By: Yin-Ta Pan
Tags: Networkx, Matplotlib, Python, Basemap, Data Analysis

Part 1

Introduction to Basemap

The matplotlib basemap toolkit is a library for plotting 2D data on map in Python. As everyone agrees that visualization is really important for people to understand data, map is the best visualization method when we are dealing with geographic locations (I believe no one is good at dealing with latitude and longitude data without a map.) Basemap is a powerful package in Python and we will pick some important features to explain in details. To have more understanding about the package, please check out the documentation.

Projection

As we all know, the earth is round. Therefore, to make it as a 2D map, we need some projection methods. In basemap package, it supports many projection methods and the list is here. Here are two examples of how to apply different projection methods in basemap .

Projection — Orthographic

Orthographic

Projection — Gall Stereographic

Gall Stereographic

Drawing points

Once we can plot a map, we would like to mark some places on it. In baemap package, we can use the basemap object we defined previously to transfer latitude and longitude to the coordinate system in the matplotlib . After that, we can apply scatter or annotate functions to put points on the map. In the following example, we marked the four major cities in the U.S., New York, Washington DC, Los Angels and San Francisco.

Marking the four major cities with basemap

Drawing routes

Besides putting points on a map, we also need to draw lines to visualize routes between two places. As we know, the shortest route between two points on the map is not a straight line, but the great circle route. In basemap package, we can plot routes with gcpoints function to make the great circle route. Here is the example for the route from New York to San Francisco with gcpoints function.

Route between New York and San Francisco

There are still a lot of interesting functions in basemap package to make your map more informative and attractive. For example, arcgisimage can create a map with satellite photos. However, we are good enough to move on making our plans for traveling around the world now.

Satellite map for Taiwan with arcgisimage function in basemap package

Playing with Real Data

In our plan, we would like to travel around the world with flights. In OpenFlights database, we are able to access Airports and Routes information around the world. With the information, we can make our plan to travel around the world. However, before that, let’s take a look at the data and use basemap tool to visualize it first.

Airport

In OpenFlights, it includes all 6,060 airports around the world with completed Name, City, Country, IATA code, Latitude, Longitude and all other information. Besides, since it has latitude and longitude information, we can easily lookup more details, such as the continent the airport belongs to, with Google Map api or other tools. We can also use basemap tool to put the airports on the map with different colors for airports in different continents: North America, South America, Asia, Europe, Africa and Oceania as following.

Airports around the world (Source: OpenFlights)

Route

Though we have all airports information around the world, we still need airline routes information when we think about how to travel around the world, since not all two airports are connected. In OpenFlights, it does have airline routes information. Unfortunately, it was not updating after June 2014. Though not perfect, we can still use the data and assume that the routes did not change a lot from 2014.

There are 67,663 different routes in OpenFlights database. It has Source airport and Destination airport information as well as Airline, Numbers of stops and Equipment details. If we only consider the source and destination airports, there are 33,971 distinct routes in the data. We can apply tools in basemap to visualize those routes as following.

Airline routes around the world (Source: OpenFlights)

Now we have all knowledge: skillsets to visualize on the map with Python and information about airports and airline routes. It seems like we are ready for our next step: optimizing our routes to travel around the world.