Skip to content

Instantly share code, notes, and snippets.

@mattharrison
Created August 31, 2012 05:16
Show Gist options
  • Save mattharrison/3549290 to your computer and use it in GitHub Desktop.
Save mattharrison/3549290 to your computer and use it in GitHub Desktop.
python code to concat/shrink gpx files
# attempt to shrink gpx files and concat them to get on android device
# got zip file with gpx from http://www.phillowry.com/wasatchwatch.htm
# converted to kml with gpx2kml.com
# gaia gps bogged down with files...
# my tracks does ok with them
import sys
import xml.etree.ElementTree as et
def concat(fins, fout, stride):
tree = et.ElementTree()
tree.parse(fins[0])
parent = tree._root.getchildren()[1].getchildren()[-1] #trkseg
i = 0
to_remove = []
to_add = []
for child in parent.getchildren():
# I hate ns handling in etree!!!
if child.tag.endswith('trkpt'):
#import pdb;pdb.set_trace()
if i % stride != 0:
to_remove.append(child)
i += 1
for rem in to_remove:
parent.remove(rem)
for other in fins[1:]:
t2 = et.ElementTree()
t2.parse(other)
p2 = t2._root.getchildren()[1].getchildren()[-1] #trkseg
for child in p2.getchildren():
# I hate ns handling in etree!!!
if child.tag.endswith('trkpt'):
#import pdb;pdb.set_trace()
if i % stride == 0:
to_add.append(child)
i += 1
for node in to_add:
parent.append(node)
tree.write(fout)
def shrink(fin, fout, stride=2):
tree = et.ElementTree()
tree.parse(fin)
# don't work silly ns...
#parent = tree.find('trkseg')
parent = tree._root.getchildren()[1].getchildren()[-1] #trkseg
i = 0
to_remove = []
for child in parent.getchildren():
# I hate ns handling in etree!!!
if child.tag.endswith('trkpt'):
if i % stride != 0:
to_remove.append(child)
i += 1
for rem in to_remove:
parent.remove(rem)
tree.write(fout)
#shrink(open(sys.argv[1]), open(sys.argv[2], 'w'), stride=40)
concat([open('/tmp/w1.gpx'), open('/tmp/w2.gpx')], open('/tmp/w100-40.gpx', 'w'), stride=40)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment