Skip to content

Instantly share code, notes, and snippets.

@juri
Created May 30, 2013 11:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save juri/5677168 to your computer and use it in GitHub Desktop.
Save juri/5677168 to your computer and use it in GitHub Desktop.
Parse a GPX file and add extra locations by interpolation. This allows you to slow down a simulated route in Xcode/iOS Simulator.
#!/usr/bin/python
# Interpolate GPX waypoints to slow a simulated route down.
import itertools
import sys
import xml.etree.ElementTree as ET
def pairwise(iterable):
"s -> (s0,s1), (s1,s2), (s2, s3), ..."
a, b = itertools.tee(iterable)
next(b, None)
return itertools.izip(a, b)
def add_points(tree, count):
root = tree.getroot()
newroot = ET.Element('gpx')
for c0, c1 in pairwise(root):
c0lat = float(c0.attrib["lat"])
c1lat = float(c1.attrib["lat"])
c0lon = float(c0.attrib["lon"])
c1lon = float(c1.attrib["lon"])
latdiff = c1lat - c0lat
londiff = c1lon - c0lon
latstep = latdiff / (count + 1)
lonstep = londiff / (count + 1)
ET.SubElement(newroot, "wpt", c0.attrib)
for i in range(1, count + 1):
sublat = c0lat + (i * latstep)
sublon = c0lon + (i * lonstep)
subelem = ET.SubElement(newroot, "wpt", { "lat": str(sublat), "lon": str(sublon) })
ET.SubElement(newroot, "wpt", root[-1].attrib)
newtree = ET.ElementTree(newroot)
newtree.write(sys.stdout)
if __name__ == '__main__':
# args: filename, additional point count
tree = ET.parse(sys.argv[1])
add_points(tree, int(sys.argv[2]))
@palto-blubek
Copy link

Very useful, thanks. However I think there's an indentation problem line 16?

@wizard1066
Copy link

Looks good, but how do you use it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment