#!/usr/bin/env python # Handle GPX files in various ways. # Copyright 2009 by Akkana Peck. Share and enjoy under the GPL v2 or later. import sys, xml.dom.minidom class TrackFile : """Parsing and handling of GPS track files. Currently only GPX format is supported. """ def __init__(self) : pass def findBounds(self, trkpts) : minlon = 361 maxlon = -361 minlat = 91 maxlat = -91 for pt in trkpts : lat = float(pt.getAttribute("lat")) lon = float(pt.getAttribute("lon")) if lat > maxlat : maxlat = lat if lat < minlat : minlat = lat if lon > maxlon : maxlon = lon if lon < minlon : minlon = lon return ( minlon, minlat, maxlon, maxlat ) def readTrackFile(self, _filename, opts) : self.filename = _filename self.points = [] if (opts.debug) : print "Reading track file", self.filename dom = xml.dom.minidom.parse(self.filename) # Find the track segments timenode = dom.getElementsByTagName("time") tracks = dom.getElementsByTagName("trk") print "Found", len(tracks), "tracks" for trk in tracks : name = self.getVal(trk, "name") trkpts = trk.getElementsByTagName("trkpt") trksegs = trk.getElementsByTagName("trkseg") if opts.list or opts.write : print "\n", name, "has", len(trkpts), "track points", if len(trksegs) > 1 : print "in", len(trksegs), "segments", if not opts.write : print '' # to print a newline continue if (len(trkpts) <= 0) : print "-- not writing" continue outfilename = name + ".gpx" if opts.prompt : while True : # Python has no do ... while loop outfilename = raw_input("\nFilename (" + outfilename + "): ") if outfilename != "" : break if outfilename[-4:] != ".gpx" : outfilename = outfilename + ".gpx" ( minlon, minlat, maxlon, maxlat ) = self.findBounds(trkpts) fp = open(outfilename, "w") fp.write('\n') fp.write('\n') if len(timenode) > 0 : timenode[0].writexml(fp) fp.write('\n') # write bounds fp.write('') trk.writexml(fp) fp.write('\n\n') fp.close() print "-- wrote", outfilename # trkpts = dom.getElementsByTagName("trkpt") # for i in range (0, len(trkpts), 1) : # self.handleTrackPoint(trkpts[i]) @staticmethod def getVal(parent, name) : nodeList = parent.getElementsByTagName(name) if len(nodeList) < 1 : return "No " + name + " element" children = nodeList[0].childNodes if len(children) < 1 : return "No " + name + " children" node = children[0] if node.nodeType != node.TEXT_NODE: return name + " isn't a text node" return node.data if __name__ == "__main__" : from optparse import OptionParser usage = "Usage: %prog [options] gpx_file(s)" version = "%prog 0.1: analyze or split GPX files with multiple tracks.\n\ Copyright 2009 by Akkana Peck; share and enjoy under the GPL v2 or later." parser = OptionParser(usage=usage, version=version) parser.add_option("-l", "--list", action="store_true", dest="list", help="list the tracks available -- don't write anything") parser.add_option("-w", "--write", action="store_true", dest="write", help="split into multiple track files") parser.add_option("-p", "--prompt", action="store_true", dest="prompt", help="prompt for each filename before writing") parser.add_option("-d", "--debug", action="store_true", dest="debug") (options, args) = parser.parse_args() for arg in args : trkfile = TrackFile() trkfile.readTrackFile(arg, options)