GPS track analysis

GPS data has become extremely available in the last time and analyzing it always yields interesting results.The purpose of this article is to show a simple example where GPS track data is compared with a set of waypoints in order to determine whether a specific tracks was followed. This scenario is custom to various competitions ranging from randonee, cycling (cross country, orientation), 4×4 and the examples can go on and on. A more advanced version of this simple script was used for the TAT competition this year.

From a technical point of view this is a python script which does the following steps:

  • parse the kml file for the checkpoints
  • parse the kml file for the track data
  • as the track data is analyzed each point is compared (distance is calculated) to the checkpoints

Parsing kml data

Parsing xml data in python is quite easy once you solve the namespace problem:

def parse_cp(cp_file, day):
    global cps
    doc = libxml2.parseDoc(clean_data(cp_file))
    ctxt = doc.xpathNewContext()
    ctxt.xpathRegisterNs('kml', "")
    res = ctxt.xpathEval("//Placemark")
    for e in res:
        n = e.xpathEval('name')[0]
        name = n.content.strip()
        if name.startswith('cp') and name.endswith(day):
            coord = e.xpathEval('Point/coordinates')[0].content.split(',')
            cps.append([name, coord])
    cps = sorted(cps, key = lambda cp : cp[0])
    print cps


In order to calculate distance between 2 points there is already an example which does just that. Many thanks to the author.

def parse_track_file(track_file, precision, cp_stats):
    doc = libxml2.parseDoc(clean_data(track_file))
    ctxt = doc.xpathNewContext()
    res = ctxt.xpathEval("//Placemark/Point/coordinates")
    for e in res:
        pos = e.content.strip().split(',')
        for c in cp_stats:
            if not c[1]:
                dst = distance(c[0][1], pos)
                if dst < precision:
                    print 'Near cp', c[0][0], 'by', dst
                    c[1] = True

Next step is just a matter of competition rules in order to determine whether it’s important to validate all checkpoints, what is the max deviation allowed and so on. And you can build a hole application on top in order to present this data to the users and import kml data from devices such as tracker in an organized way. Download the example above here.

Find more info related to GPS on tuxmobil

One Response

  1. There is a typo in your last sentence, I think it’s ‘whole’ not ‘hole’.

Leave a Reply