Skip to content

Instantly share code, notes, and snippets.

@tdewin
Created June 16, 2025 20:29
Show Gist options
  • Save tdewin/b4a0ef383232a459ba71b830992a5cfc to your computer and use it in GitHub Desktop.
Save tdewin/b4a0ef383232a459ba71b830992a5cfc to your computer and use it in GitHub Desktop.
Converst gpx to Inkscape sag
#!/usr/bin/env python3
# eg 50.84,4.35 is a gps coordinate in brussels . vw/vh is the amount of degrees. eg 0.06 => around 6km. It is the left lower corner as in the north space degrees go up if you move north (but in svg it is the other way around)
# ./loopgpx.py -vy 50.84 -vx 4.35 -vw 0.06 -vh 0.06 -width 1000 -height 1000 "file1.gpx" "file2.gpx" "file3.gpx" > map-of-multiple-loops.svg
# it uses print instead of propper xml tree building cause well hmm, i'm lazy and this should work as well if you don't use exotic file names
import sys
import argparse
import xml.etree.ElementTree as ET
import math
parser = argparse.ArgumentParser()
parser.add_argument("gpx",nargs='+')
parser.add_argument("-height",type=float)
parser.add_argument("-width",type=float)
parser.add_argument("-vx",type=float)
parser.add_argument("-vy",type=float)
parser.add_argument("-vw",type=float)
parser.add_argument("-vh",type=float)
args = parser.parse_args()
# i saw this formula on multiple websites, it seems to give a good approximation for the ratio
r = math.cos(args.vy*math.pi/180)
viewbox = "{} {} {} {}".format(0,0,args.vw,args.vh)
paths = []
for g in args.gpx:
tree = ET.parse(g)
root = tree.getroot()
xmlns = "http://www.topografix.com/GPX/1/1"
q = ".//{{{}}}{}".format(xmlns,"trkpt")
listp = []
pathtag = "M"
for ptx in root.findall(q):
#lon is the x axis
#lat is reverse y axis
lat = -(float(ptx.attrib["lat"]))+args.vy+args.vh
lon = (float(ptx.attrib["lon"])-args.vx)*r
listp.append("{} {},{}".format(pathtag,lon,lat))
pathtag = "L"
paths.append("<path inkscape:label=\"{}\" d=\"{}\"/>".format(g," ".join(listp)))
print("<svg viewBox=\"{}\" width=\"{}\" height=\"{}\"><g id=\"gpxmap\" inkscape:label=\"gpxmap\">{}</g><g id=\"info\"><text style=\"font: {}px\">{}</text></g></svg>".format(viewbox,args.width,args.height," ".join(paths),args.vh/5," ".join(sys.argv)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment