QGIS Python Tutorial: Animate GPS Track

In this QGIS python tutorial series, I will explain about how to animate a GPS track on QGIS map. Animation is a really cool way to visualize spatial data, because our eyes and brain are more connected with a series of moving object in the screen. It made us to watch it until finish or at a point when our brain says enough.

For this tutorial, I will use a GPX file which is called ride.gpx. I will parse  the file using xml dom to extract the coordinate of track points. Then animate the points with markers with a defined time interval. The result of track animation can be seen in figure 1.

Tracking animation Python QGIS
Figure 1. Tracking animation
Let's start this tutorial with importing some python libraries such as xml dom and time.

import time
from xml.dom import minidom

Using the minidom library, then we read the gpx file and get the track element. It will return a track list for each point.

#READ GPX FILE
data=open('F:/ride.gpx')
xmldoc = minidom.parse(data)
track = xmldoc.getElementsByTagName('trkpt')
n_track=len(track)

Next, the list is parsed to get each point coordinate (longitude and latitude). Based on the coordinates, then a marker is created for each point and stored in a list(marker_list). The marker symbol also customized in this step by setting up the color, icon type, fill color, size, and line width. Finally the markers are displayed on QGIS map canvas with interval 1 second.  

#PARSING GPX ELEMENT AND CREATING MARKER
marker_list=[]
canvas=iface.mapCanvas()
for s in range(n_track):
    lon,lat=track[s].attributes['lon'].value,track[s].attributes['lat'].value
    x=float(lon)
    y=float(lat)
    m = QgsVertexMarker(canvas)
    m.setCenter(QgsPointXY(x,y))
    m.setColor(QColor(255,0,0))
    m.setFillColor(QColor(255,255,0))
    m.setIconSize(10)
    m.setIconType(QgsVertexMarker.ICON_CIRCLE)
    m.setPenWidth(3)
    marker_list.append(m)
    time.sleep(1)
    print(m)

Next, a function which is called hide_track is created to hide the GPS track. And also a play_track function is created to play the track animation. These functions will take each marker in marker list and hide or show it with 1 second time interval. Both functions can be used only after the code execution. To hide the track, just type in the python console hide_track() and play_track() to start the GPS track animation again.

#FUNCTION TO HIDE AND PLAY TRACK ANIMATION    
def hide_track():
    for i in range(n_track):
        marker_list[i].hide()

def play_track():
    hide_track()
    for j in range(n_track):
        marker_list[j].show()
        time.sleep(1) #Time interval. You can change it here
        print(marker_list[j])

The complete code  can be seen below. In figure 2 shows the code action in QGIS.

import time
from xml.dom import minidom

#READ GPX FILE
data=open('F:/ride.gpx')
xmldoc = minidom.parse(data)
track = xmldoc.getElementsByTagName('trkpt')
n_track=len(track)

#PARSING GPX ELEMENT AND CREATING MARKER
marker_list=[]
canvas=iface.mapCanvas()
for s in range(n_track):
    lon,lat=track[s].attributes['lon'].value,track[s].attributes['lat'].value
    x=float(lon)
    y=float(lat)
    m = QgsVertexMarker(canvas)
    m.setCenter(QgsPointXY(x,y))
    m.setColor(QColor(255,0,0))
    m.setFillColor(QColor(255,255,0))
    m.setIconSize(10)
    m.setIconType(QgsVertexMarker.ICON_CIRCLE)
    m.setPenWidth(3)
    marker_list.append(m)
    time.sleep(1)
    print(m)
   
#FUCNTION TO HIDE AND PLAY TRACK ANIMATION    
def hide_track():
    for i in range(n_track):
        marker_list[i].hide()

def play_track():
    hide_track()
    for j in range(n_track):
        marker_list[j].show()
        time.sleep(1)
        print(marker_list[j])

GPS track animation in QGIS
Figure 2. GPS track animation in QGIS
That's all the tutorial how to anime GPS track in QGIS using python. If you want to learn more about QGIS Python Programming, please see the QGIS Python Tutorial series which covers some other topics. 

Related Posts
Disqus Comments