Skip to content

Instantly share code, notes, and snippets.

@sookoll
Created December 10, 2019 23:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sookoll/dc2f292246c775d8b554368cd16d53af to your computer and use it in GitHub Desktop.
Save sookoll/dc2f292246c775d8b554368cd16d53af to your computer and use it in GitHub Desktop.
Merge GPX files into one
#!/usr/bin/env python
#
# gpx-merge -- merge GPX-files from directory into single GPX-file. GPX
#
# usage: python3 gpx-merge.py -d input_dir -o output.gpx
#
# Copyright (c) 2019 Mihkel Oviir
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
from argparse import ArgumentParser
import glob
import xml.dom.minidom
parser = ArgumentParser()
parser.add_argument("-d", "--directory", dest="directory",
required=True, help="path to directory containing gpx files")
parser.add_argument("-o", "--output", dest="output",
required=True, help="path to output")
args = parser.parse_args()
directoy = args.directory
output = args.output
files = glob.glob(directoy + "/*.gpx")
parent = xml.dom.minidom.parse(files[0]).firstChild # <gpx>
iterables = iter(files)
next(iterables) # skip first as it is already in parent
for file in iterables:
dom = xml.dom.minidom.parse(file)
for wpt in dom.getElementsByTagName("wpt"): # all <wpt> elements
parent.appendChild(wpt)
for rte in dom.getElementsByTagName("rte"): # all <rte> elements
parent.appendChild(rte)
for trk in dom.getElementsByTagName("trk"): # all <trk> elements
parent.appendChild(trk)
file = open(output, 'w')
file.write('<?xml version="1.0" encoding="utf-8"?>')
file.write(parent.toxml())
file.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment