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', "http://www.opengis.net/kml/2.2") res = ctxt.xpathEval("//Placemark") for e in res: n = e.xpathEval('name') name = n.content.strip() if name.startswith('cp') and name.endswith(day): coord = e.xpathEval('Point/coordinates').content.split(',') cps.append([name, coord]) doc.freeDoc() ctxt.xpathFreeContext() cps = sorted(cps, key = lambda cp : cp) 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: dst = distance(c, pos) if dst < precision: print 'Near cp', c, 'by', dst c = True doc.freeDoc() ctxt.xpathFreeContext()
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