Skip to content

Instantly share code, notes, and snippets.

@mjsqu
Last active February 12, 2020 00:55
Show Gist options
  • Save mjsqu/96128c2e274b7d096505f17586e25701 to your computer and use it in GitHub Desktop.
Save mjsqu/96128c2e274b7d096505f17586e25701 to your computer and use it in GitHub Desktop.
Tour Aotearoa File Splitter
#!/usr/bin/python
# TA file reducer, splitter for Garmin Edge 200
# Takes the big file from the website and creates new smaller files with ONLY trkpt tags
# Simple version of code - assumes program, file and output files are all in the same directory
import re
# Assumes the input file is in the current directory
infile = 'Tour-Aotearoa-route-20200123-Kennett-Bros.gpx'
# Regular expression to capture only the first part of the trkpt tag, and encased attributes (usu. lat= long=)
simplify = r'(<trkpt .*?)>.*?</trkpt>'
# Read the data
with open(infile,'r') as f:
tadata = f.read()
# Use a regex substitution in DOTALL (or single-line mode) - where dot matches newline
# Replace the complete trkpt tag with a single <trkpt lat= long= /> version
cleandata = re.sub(simplify,r'\1/>',tadata,flags=re.DOTALL)
# Set up header and trailer strings
head = """<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<gpx version="1.1" creator="GPS Visualizer https://www.gpsvisualizer.com/" xmlns="http://www.topografix.com/GPX/1/1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd">"""
tail = """</gpx>"""
# Use this regex to capture the name tag from the trk along with the trk itself
# Capture groups:
# 0 - Entire trk tag, all point data
# 1 - name tag value
splitrx = r'(<trk>.*?<name>(.*?)</name>.*?</trk>)'
# Findall creates a list of matches split into tuples trkdata,name tag value
bits = re.findall(splitrx,cleandata,flags=re.DOTALL)
# For each regex match use capture group 0 to place the data in the file
# Get the new file name from capture group 1
for data,filename in bits:
with open(filename+'_clean.gpx','w') as f:
f.write(head)
f.write(data)
f.write(tail)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment