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