Created
February 5, 2011 15:18
-
-
Save ptrv/812520 to your computer and use it in GitHub Desktop.
Change track names in GPX files (python)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# Script for assigning the date of a track in a GPX file as track name. | |
# | |
# Usage e.g $ python ChangeGpxTrackName.py source.gpx target.gpx" | |
# | |
# Peter Vasil | |
# Date: 2011-02-05 | |
from optparse import OptionParser | |
import sys | |
import os.path | |
from lxml import etree | |
def main(): | |
usage = "usage: %prog 'inputfile' 'outputfile'" | |
parser = OptionParser(usage, version="%prog 0.1") | |
(options, args) = parser.parse_args() | |
if len(args) != 2: | |
parser.error("\nplease enter the input and output GPX files") | |
infile, outfile = args | |
if not(os.path.isfile(infile)): | |
print "input file does not exist" | |
exit(1) | |
# parser = etree.XMLParser(ns_clean=True) | |
tree = etree.parse(infile) | |
namespace = "http://www.topografix.com/GPX/1/0" | |
trks = tree.findall("{%s}trk" % namespace) | |
trk_num = 0 | |
for trk in trks: | |
name = trk.find("{%s}name" % namespace) | |
time = trk.find(".//{%s}time" % namespace) | |
trk_num += 1 | |
print "-----------------------------------------" | |
print "Track number: %d" % trk_num | |
print "Old name: %s" % name.text | |
print "New name: %s" % time.text | |
name.text = time.text | |
print "-----------------------------------------" | |
treestring = etree.tostring(tree, encoding="utf-8", xml_declaration=True) | |
file = open(outfile, 'w') | |
file.write(treestring) | |
file.close() | |
if __name__ == '__main__': | |
sys.exit(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment