#!/usr/bin/env python # # This script imports waypoint entries from a GPX file to an # existing maemo-mapper POI sqlite database. # # (c) Sean McLean, 2007 # This software is licensed under the terms of the GPLv2 # import os, sys, optparse from pysqlite2 import dbapi2 as sqlite from xml.dom import minidom, Node # option parser parser = optparse.OptionParser(usage='%prog [options] gpx_input_file poi_output_file') parser.add_option('-c', action='store', dest='category', help='Category in which to import POIs') (options, args) = parser.parse_args() # default values if options.category: cat_id = options.category print "Using specified category id",cat_id else: cat_id = 11 print "Using default category id",cat_id # argument validation if len(args) != 2: parser.print_help() sys.exit() if not os.access(args[0],os.R_OK): print "Cannot open",args[0],"for reading." sys.exit() if not os.access(args[1],os.R_OK): print "File",args[1],"must be an existing sqlite database." sys.exit() # parse gpx try: print "Parsing gpx file at",args[0] doc = minidom.parse(args[0]) doc.normalize() except Exception, e: print "Failed parsing input file: ",e sys.exit() # connect to db try: print "Opening database at",args[1] con = sqlite.connect(database=args[1]) cur = con.cursor() except Exception, e: print "Cannot open database %s: %s", (args[1], e) sys.exit() # iterate through waypoints, push the data in to the poi db try: for node in doc.documentElement.getElementsByTagName('wpt'): lat = node.getAttribute('lat') lon = node.getAttribute('lon') name = node.getElementsByTagName('name')[0].firstChild.data desc = node.getElementsByTagName('desc')[0].firstChild.data print "Inserting: ",name,lat,lon cur.execute('insert into poi (lat,lon,label,desc,cat_id) values (?, ?, ?, ?, ?)', (lat,lon,name,desc,cat_id) ) except Exception, e: print "Error in conversion loop: %s", e # clean up print "Committing and closing POI database.." con.commit() cur.close() print "Done."