]> www.average.org Git - loctrkd.git/blob - gps303/mkgpx.py
Add googlemaps lookaside backend
[loctrkd.git] / gps303 / mkgpx.py
1 from datetime import datetime, timezone
2 from sqlite3 import connect
3 import sys
4
5 from .gps303proto import *
6 from . import opencellid as ocid
7
8 ocid.init({"opencellid": {"dbfn": sys.argv[2]}})
9 db = connect(sys.argv[1])
10 c = db.cursor()
11 c.execute(
12     "select tstamp, packet from events where proto in ({})".format(
13         ", ".join(
14             [
15                 str(n)
16                 for n in (
17                     WIFI_POSITIONING.PROTO,
18                     WIFI_OFFLINE_POSITIONING.PROTO,
19                     GPS_POSITIONING.PROTO,
20                     GPS_OFFLINE_POSITIONING.PROTO,
21                 )
22             ]
23         )
24     )
25 )
26
27 print(
28     """<?xml version="1.0"?>
29 <gpx version="1.1"
30 creator="gps303"
31 xmlns="http://www.topografix.com/GPX/1/1">
32   <name>Location Data</name>
33   <trk>
34     <name>Location Data</name>
35     <trkseg>
36 """
37 )
38
39 for tstamp, packet in c:
40     msg = parse_message(packet)
41     if isinstance(msg, (WIFI_POSITIONING, WIFI_OFFLINE_POSITIONING)):
42         lat, lon = ocid.lookup(msg.mcc, msg.mnc, msg.gsm_cells, msg.wifi_aps)
43         if lat is None or lon is None:
44             continue
45     elif isinstance(msg, (GPS_POSITIONING, GPS_OFFLINE_POSITIONING)):
46         lat, lon = msg.latitude, msg.longitude
47     else:
48         continue
49     isotime = (
50         datetime.fromtimestamp(tstamp).astimezone(tz=timezone.utc).isoformat()
51     )
52     isotime = isotime[: isotime.rfind(".")] + "Z"
53     trkpt = """      <trkpt lat="{}" lon="{}">
54           <time>{}</time>
55       </trkpt>""".format(
56         lat, lon, isotime
57     )
58     print(trkpt)
59     if False:
60         print(
61             datetime.fromtimestamp(tstamp)
62             .astimezone(tz=timezone.utc)
63             .isoformat(),
64             msg,
65         )
66 print(
67     """    </trkseg>
68   </trk>
69 </gpx>"""
70 )