1#!/usr/local/bin/python3.8
2
3from xml.etree.ElementTree import parse
4import sys, re, hashlib
5try:
6  import sqlite3 as sqlite
7except ImportError:
8  from pysqlite2 import dbapi2 as sqlite
9
10if len(sys.argv) <3:
11  print("usage:")
12  print("./osb2foxtrot gpxfile poidbfile")
13  print("  gpxfile: a file in the OSB GPX file format, containing bug information")
14  print("  poidbfile: usually poi.db, or any other wellformed foxtrotgps poi sqlite database file")
15  sys.exit(1)
16
17def escapeQuote(str):
18  """escapes single quotes by double single quote, sqlite style"""
19  return str.replace('\'', '\'\'');
20
21gpx = open(sys.argv[1])
22xml = parse(gpx).getroot()
23
24con = sqlite.connect(sys.argv[2])
25cur = con.cursor()
26
27for wpt in xml:
28  if wpt.tag=="{http://www.topografix.com/GPX/1/1}wpt":
29    data = {} # dictionary holding all relevant data to be written into database
30    # transfer lat, lon into dictionary directly
31    for item in list(wpt.items()):
32      data[item[0]]=item[1]
33    # transfer information from the sublevel tags into dictionary
34    for sub in wpt:
35      if sub.tag=="{http://www.topografix.com/GPX/1/1}desc":
36        data['desc']=sub.text
37    # build the query
38    idmd5 = hashlib.md5(data['lon']+data['lat']).hexdigest()[0:18] #not the format foxtrotgps uses
39    query = "insert into poi (idmd5, lon, lat, keywords, visibility, cat, subcat, price_range, extended_open, desc) values ('"+idmd5+"', '"+data['lon']+"', '"+data['lat']+"', 'OpenStreetBug', 0.0, 14.0, 2.0, 3.0, 0.0, '"+escapeQuote(data['desc'])+"')" #foxtrotgps crashes if some of these remain NULL.
40    cur.execute(query)
41
42
43con.commit()
44
45