Skip to content

Instantly share code, notes, and snippets.

@gaute
Created June 25, 2010 10:02
Show Gist options
  • Save gaute/452664 to your computer and use it in GitHub Desktop.
Save gaute/452664 to your computer and use it in GitHub Desktop.
Extracts tracklogs from the RMaps database and converts them to GPX files.
# rmapstogpx.py
#
# Extracts tracklogs from the RMaps database (/rmaps/data/geodata.db)
# and converts them to GPX files.
#
# Workaround for http://code.google.com/p/robertprojects/issues/detail?id=88
#
# Usage: python rmapstogpx.py geodata.db
# Copyright (c) 2010, Gaute Hvoslef Kvalnes <gaute@verdsveven.com>
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following
# disclaimer in the documentation and/or other materials
# provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
import sys
import sqlite3
from xml.dom.minidom import Document
conn = sqlite3.connect(sys.argv[1])
curs = conn.cursor()
curs.execute("select trackid, name from tracks")
for trackid, name in curs.fetchall():
gpxdoc = Document()
gpx = gpxdoc.createElement("gpx")
gpx.setAttribute("version", "1.0")
gpx.setAttribute("creator", sys.argv[0])
gpx.setAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance")
gpx.setAttribute("xmlns", "http://www.topografix.com/GPX/1/0")
gpx.setAttribute("xsi:schemaLocation", "http://www.topografix.com/GPX/1/0 http://www.topografix.com/GPX/1/0/gpx.xsd")
gpxdoc.appendChild(gpx)
gpxname = gpxdoc.createElement("name")
gpx.appendChild(gpxname)
gpxname.appendChild(gpxdoc.createTextNode(name))
trk = gpxdoc.createElement("trk")
gpx.appendChild(trk)
trkseg = gpxdoc.createElement("trkseg")
trk.appendChild(trkseg)
curs.execute("select lat, lon from trackpoints where trackid=?",
(trackid,))
for lat, lon in curs.fetchall():
trkpt = gpxdoc.createElement("trkpt")
trkpt.setAttribute("lat", str(lat))
trkpt.setAttribute("lon", str(lon))
trkseg.appendChild(trkpt)
gpxfilename = str(trackid) + "-" + name + ".gpx"
gpxfile = open(gpxfilename, "w")
print "Writing " + gpxfilename
gpxfile.write(gpxdoc.toprettyxml(indent=" "))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment