Remove "name"-Elements From GPX Files

7. March 2009

A hobby of us is tracking for OpenStreetMap. I don't want to upload my GPX files, because of the "name"-Elements. We describe in the "name"-Elements the number of the photo we took at this place. So before uploading I needed to clean the GPX files a bit.

The following python code uses lxml for xml handling and needs the gpx.xsd from http://www.topografix.com/gpx.asp

from lxml import etree
import sys

# 2 arguments?
if len(sys.argv) > 2:
    tree = etree.parse(sys.argv[1])

    # namespace of gpx files
    NS = dict(ns='http://www.topografix.com/GPX/1/1')
    # search for name-elements in trkpt-elements
    r = tree.xpath('//ns:trkpt/ns:name', namespaces=NS)

    # remove all found elements
    for i in r:
        a = i.getparent()
        a.remove(i)

    # test schema on cleaned xml-tree
    xmlschema = etree.XMLSchema(etree.parse('gpx.xsd'))
    xmlschema.validate(tree)

    # write new gpx file
    f = open(sys.argv[2], "w")
    tree.write (f, pretty_print=False )

else:
    print "usage: <gpx-file> <gpx-out-file>"

Tags: gpx, lxml, osm, python

Categories: Python

 
 

Comments

Steve M wrote ...
Thank you!!!!

I have been looking all over for an example of GPX namespace usage and XPath! Now my App actually works!

On the 2. October 2009 at 05:01 AM.

 
 

Write new Comment

captcha