Plotting temporal trajectories on Google Earth with Python15 Feb 2016
In this post I’m going to show you how to display temporal trajectories on Google Earth using the python package simplekml. This package enables us to generate KML videos that we can then plot and record on Google Earth. The python script kmlMovie.py described in this post is available on my GitHub account.
This script can be used to make videos like the one below. The video quality is not very high but it gives you an idea of the kind of vizs that you can do with simplekml.
First, we need a temporal network divided into two csv files, Nodes.csv and Links.csv. Nodes.csv is composed of 3 columns providing geographical information about the nodes:
- Node ID
- Longitude of the node
- Latitude of the node
You can use the piece of code below to import the file into the dictionary nodes.
Links.csv is composed of 5 columns providing information about the “temporal” link between nodes defined in Nodes.csv. The links should be sorted by starting time.
- Link ID (numeric)
- Node of origin
- Node of destination
- Departure time of the link from the node of origin (date format ‘2014-01-01T08:00:00Z’)
- Arrival time of the link to the node of destination (date format ‘2014-01-01T08:00:00Z’)
You can use the piece of code below to import the file into the dictionary links. If you want to reproduce the example of movie given above, the temporal network is available here.
The script has also 7 parameters used to define the time window (start and end of the movie), the nodes’ and links’ remaining time on screen, the nodes’ and links’ styles and the name of the output file. Their values can be set directly into the Python script kmlMovie.py.
- begin: Starting time of the movie (date format ‘2014-01-01T08:00:00Z’)
- end: Ending time of the movie (date format ‘2014-01-01T08:00:00Z’)
- delay_node: Number of seconds the nodes remain visible after their creation
- delay_link: Number of seconds the links remain visible after their creation
- hrefnodestyle: Hyperlink leading to the node style image file
- rgbcolorlink: Define the links color with an rgb code
- output_file: Name of the output file
The parameters begin and end are in date format. They form a time interval containing all the departure and arrival times. The parameters delay_node and delay_link can be tuned to adjust the time during which the nodes and links remain visible before disappearing from the screen (50 seconds in the example). Finally, the parameter hrefnodestyle is a hyperlink leading to an image file (Cartman in this example). The color of the links is defined with the parameter rgbcolorline. The size of the nodes and the width of the link can also be tuned (see below). With little effort we could easily modify the code to set the nodes’ and links’ parameters individually in order to obtain different style and remaining times on screen.
Then, we need to define three auxiliary functions to lighten the code. TZtoUNIX and UNIXtoTZ are used to convert date to unix time stamp format and inversely. You will need to import the package datetime to use this two functions.
We then define the IntermediatePoint function in order to determine the coordinates of an intermediate point on a great circle path between two points. The location of the intermediate point on the arc can be tuned with the parameter fraction to obtain a point located at a distance fraction*(great circle distance between the nodes of origin and destination) from the origin. We need this function to build progressively the temporal link between the node of origin and the node of destination by increasing the value of the parameter fraction from 0 to 1 by step of 0.05 (see below).
We can now define the main function by creating a kml file called, with originality, kml in our case. The simplekml package allow us to add different types of object to this file (points and lines in our case) for which we can define a style and a temporal dimension. Any object in KML can have time data associated with it. This time data has the effect of restricting the visibility of the data set to a given time period or point in time. Although the complete data set is fetched when the KML file is loaded, the time slider in the Google Earth user interface controls which parts of the data are visible (see this webpage for more detail).
First, we define the nodestyle with the parameter hrefnodestyle, an hyperlink leading to the node style image file (Cartman in our case). We can also adjust the size of the nodes (2 in our example).
Then we define the time window (starting and ending time of the movie). By default, the time windows of the KML movie depends on the timespan of the KML objects. If we want to set it manually we need to create an hidden node with the dummynodestyle define below.
This hidden node will be “visible” from the beginning of the movie (set with the parameter begin) to the end of the movie (set with the parameter end). Since this node will not appear on screen we can choose its lon/lat coordinates randomly among the nodes (first line of the piece of code below).
We can now loop over the links sorted by IDs (i.e. starting time).
For each link, we extract the departure time, arrival time and the duration of the trip in second using the function TZtoUNIX.
We then create the nodes of origin and destination of this link which will be visible from the departure and arrival time of the link, respectively. Both nodes will remain visible during delay_node seconds.
We finally build the temporal link, progressively, piece by piece. The link is divided into 20 pieces. The first and last points of the link are the node of origin and destination, respectively. The 19 intermediate points are computed with the IntermediatePoint function defined above. We use the parameter rgbcolorlink to define the color of the link. The width of the link can also be adjusted (5 in this example). As it is the case for the nodes, the pieces of link will remain visible on screen during delay_link seconds.
The KML movie is finally saved into the output_file.