Planning to Travel Around the World, with Python? | by Yin-Ta Pan | Towards Data Science

Traveling Around the world is always the dream for everyone, but seldom of us are prepared to do so. To be honest, we even do not know what traveling around the world means. Of course, I cannot provide anything you need when you traveling, such as money and vacation, which are what I am lack of too. However, you can start to plan, or to daydream, with the skills you learned from the articles.
There are two parts in this article:
[Part 1] — this article is about how to draw a map with Basemap package in Python. We need map before and during the trip, and we can use the package in Python to draw what we need. [source code in Jupyter notebook]
[Part 2] — this article is about how to optimize the route for us to travel around the world with Networkx package in Python. Networkx is a powerful package in Python to analyze complex network, and it will be helpful for us to make the plan of traveling around the world, with our goals. [source code in Jupyter notebook]
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.
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
Projection — Gall Stereographic
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.
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.
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.
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.
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.
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.
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.
A good definition to a problem is always the most important thing. Before we start our analysis, we need to define what is “Travel Around the World” for us: you traveled around the world if you have visited at least one site in each continent: North America, South America, Asia, Europe, Africa and Oceania.
Networkx is a Python package for the creation, manipulation, and study of the structure, dynamics, and functions of complex networks. We can take the flight routes between airports as a complex networks, and Networkx package is therefore very helpful for us to analyze them. Before we start, there are some terms we need to get familiar first:
Nodes: the points in the network; in our example, airports are nodes in the airline network.
Edges: the connections between two points; in our example, flight routes are edges. Since not all two airports are connected, we need to use route data from OpenFlights to define them.
Directed/ Non-Directed graph: in directed graph, edge from A to B does not equal to edge from B to A; in contrast, in non-directed graph, edge A to B is the same as B to A. In our example, we should use directed graph as a route from A to B is not indicating that there is a corresponding route from B to A.
Simple/ Multigraphs: in simple graph, there is at most one edge between each point; in multigraphs, it allows more than one edge between two nodes, and each edge can have different attributes. To simplify our analysis, we choose to use simple graph model, which means we do not consider the differences of airlines.
Here are some useful functions for us to analyze the air flight network:
dijkstra_path: the shortest path from A to B by Dijkstra’s algorithm. We can put the “weight” in the function to indicate how to calculate the “distance”. For example, we want to find the shortest path from Atlanta to Taipei is to layover in Incheon Airport (ICN) with the following code.
nx.dijkstra_path(G, source='ATL', target='TPE', weight='Distance')>> [Output]: ['ATL', 'ICN', 'TPE']
single_source_dijkstra: when we do not have a specific destination, we can use the function to calculate the distance to all possible destinations, considering the layover options.
Without Networkx package, we can still do some analysis on route data from OpenFlights. For example, we can calculated distance by geodesic function in geopy package from airports’ latitudes and longitudes. We can take a look at the shortest route travel to another continents in each continent with pandas package with the processed dataset, which contains calculated mileage information, as following.
With the table, we can find the minimum distance to another continents and the corresponding airports. Taking North America as an example, the shortest route to South America is from Queen Beatrix International Airport (AUA) to Josefa Camejo International Airport (LSP). To Oceania, the shortest route is from Daniel K. Inouye International Airport (HNL) to Cassidy International Airport (CXI). The shortest routes for each continent to travel between can be visualized on the map as following.
To be noticed, there is no direct route between Europe and Oceania. However, it does not mean people in Europe cannot travel to Oceania, since we can always transfer in other airports. In this complicate situation, we can use Networkx package to handle it efficiently.
For Networkx 2.3 version, we can import edges from pandas with from_pandas_edgelist function and putting other columns as attributes for edges. We can put latitude, longitude and continent information for all airports by set_node_attributes function in Networkx and make our analysis easier in the future through the following codes.
We can define our goal as to travel the 6 continents with shortest distance. Definitely, we can calculate all distances between two airports and optimize the solution. However, since there are 6000+ airports around the world, it would be millions of possible pairs of airports, and the calculation could take forever. Therefore, instead of calculate the distance between all two airports, we can achieve the goal by finding the next closest airport to another continents given the location we currently in. With this logic, we define a customized function as following.
In the function, there are some input parameters we can change:
G: the graph for global airline network. It should be a Graph object in Networkx.
Start_Airport: the airport we start with.
Must_go: a list of airports we must visit during the trip.
Dont_go: a list of airports we do not want to visit during the trip.
With the tool, we can start the plan to travel around the world. Assuming that we start our trip from Atlanta (ATL,) where I am stay in currently. Without putting any Must_go and Dont_go sites, we can visualization the result on the map. The red triangles represent the destination airport we plan to visit and the black dots are where we layover. The trip starts with Atlanta and the first stop is Ernesto Cortissoz International Airport (BAQ) in South America. After that we will fly to TFN in Tenerife, Spain (Europe,) EUN in Western Sahara (Africa,) ADB in Turkey (Asia,) ROR in Palau (Oceania) and come back to Atlanta. As what we want, we have traveled all 6 continents with total distance of 39,905 km.
As shown on previous plan, there are many airports are actually far away from the continents “we think” it should belong to. For example, TFN airport, even though it is under Spain and is part of Europe, it actually closer to Africa. Besides, I would like to be back home, Taipei, during the trip. We can accomplish both goals with Must_go and Dont_go parameters in Globetrotting function. Here is the final plan for me to travel around the world.
In this article, we applied Networkx package to analyze global airline network and using basemap package in matplotlib to visualize the results. As mentioned, Networkx is a powerful analytical package for complex network and provides useful information in difficult scenarios. With the tools, we can plan our trip to travel around the world, which means visiting all 6 continents, under some customized requirements. Also, we can visualize our plan on the map as shown.
However, though we are proficient at Networkx and basemap package now, we are still in shortage of the most important resource, time and money, to actually travel around the world. Therefore, hope all of us have a nice working day tomorrow!
Welcome to discuss anything about Python, R, data analysis, or AI through LinkedIn. Just don’t show off your trip pictures with me!