How to Create a Rotating Globe Using Python and the Basemap Toolkit
In previous tutorials I demonstrated multiple data visualization methods using: animated GIFs, live-plotting, geographic mapping, and various tools related to the matplotlib library in Python. For information and further exploration, click any of the links below:
Using the code above, we can introduce a loop and rotation scheme that simulates a rotating globe. In the next section, I introduce rotation by varying the perspective angle.
To vary the perspective and begin the illusion of a rotating earth, we only need to change one variable: the longitudinal viewing angle. We can, of course, vary both latitude and longitude, but to create a realistic rotation of earth, we need to fix the latitudinal viewing angle and rotate the longitudinal viewing angle. This can be done by altering the perspective angle in the code above:
In this case, I chose Milan, Italy as the central viewing coordinate. The respective orthographic map looks like the following:
With the ability to alter the perspective angle, we can now loop through an array of chosen longitudinal view points to simulate a rotating globe.
In a previous tutorial, I introduced a function that saves a series of .png files and loops through them to create animated GIFs [review the function here]. In this section, I will be using that function (gifly) to create the illusion of a rotating earth. By varying the longitudinal perspective angle, the simple rotating earth GIF is created using the code outlined below.
The animation takes roughly 2 minutes and 40 seconds to run on a Macbook with 8GB RAM and a 2GHz i7 processor. The function is not necessarily a real-time animation, however, the GIF runs smoothly and occupies roughly 10MB of space. The animation runs on all platforms and is a great addition to presentations and lectures without needing to worry about file formats.
Additionally, it is important to note the import of the GIF function:
This function should be in the same folder as the script used to create the animated GIF. I have uploaded the 'gifly' function on this project's GitHub page [download it here].
At this point in the tutorial, the user should be capable of producing the rotating globe shown above. Many of the parameters are tunable: latitude viewing angle, frame rate, land color, ocean color, resolution of latitudinal frame (rotation steps), etc. This gives the user the ability to create a wide array of applications relating to earth animations with Python's Basemap toolkit. From here, I will demonstrate how to insert the rotating globe into a space backdrop, and also include NASA's high resolution Blue Marble image to create a realistic depiction of a rotating earth from an outer space perspective angle.
We can take the animation a step further by placing a background behind the globe to simulate a space-like environment. To do so, I downloaded a .png image of space [this one] and placed it on a parallel axis behind the Basemap image. Additionally, I used NASA's Blue Marble image which provides realistic contours and colors of earth. These two improvements create the animation shown here.
The full code takes between 8-10 minutes to run with a Blue Marble resolution is 50% (on my particular machine, referenced above). As far as I know, the routine above is the most efficient way to create the .gif of the animated Basemap plot. A preferred method for this type of animation would be rotation of the map, however, I do not believe the ability to do that exists under the Basemap toolkit.
In this tutorial, I outlined the creation of a rotating globe using Python's Basemap toolkit in conjunction with NASA's Blue Marble image. I introduced orthographic projection in Python and the idea of perspective viewpoints, which allowed us to view the globe from an extraterrestrial vantage point. By varying the longitudinal viewing angle, we were able to create the illusion of a rotating globe. Many factors affect the performance and visuals of the rotating globe: the revolution step, frame rate, figure size, Blue Marble resolution - all of which are alterable depending on the desired outcome for the .gif image. A few applications using this project: a rotating globe for a presentation on geography; a rotating globe for visualization of an array of multi-continental destinations; an animation for satellite view angles; an animation for solar radiation purposes...the possibilities are numerous! I plan to use this project to map airport destinations in real-time based on flight departure times - so stay tuned for that application.
Resources:
See More in Programming and Python: