]> www.average.org Git - loctrkd.git/blob - gps303/opencellid.py
Work with cell location data; use opencellid
[loctrkd.git] / gps303 / opencellid.py
1 """
2 Download csv for your carrier and your area from https://opencellid.org/
3 $ sqlite3 <cell-database-file>
4 sqlite> create table if not exists cells (
5   "radio" text,
6   "mcc" int,
7   "net" int,
8   "area" int,
9   "cell" int,
10   "unit" int,
11   "lon" int,
12   "lat" int,
13   "range" int,
14   "samples" int,
15   "changeable" int,
16   "created" int,
17   "updated" int,
18   "averageSignal" int
19 );
20 sqlite> .mode csv
21 sqlite> .import <downloaded-file.csv> cells
22 sqlite> create index if not exists cell_idx on cells (mcc, area, cell);
23 """
24
25 from datetime import datetime, timezone
26 from pprint import pprint
27 from sqlite3 import connect
28 import sys
29
30 from .GT06mod import *
31
32 db = connect(sys.argv[1])
33 ldb = connect(sys.argv[2])
34 lc = ldb.cursor()
35 c = db.cursor()
36 c.execute(
37     """select timestamp, imei, clntaddr, length, proto, payload from events
38         where proto in (?, ?)""",
39     (WIFI_POSITIONING.PROTO, WIFI_OFFLINE_POSITIONING.PROTO),
40 )
41 for timestamp, imei, clntaddr, length, proto, payload in c:
42     obj = make_object(length, proto, payload)
43     qry = """select lat, lon from cells
44              where mcc = {} and (area, cell) in ({})""".format(
45         obj.mcc,
46         ", ".join(
47             [
48                 "({}, {})".format(locac, cellid)
49                 for locac, cellid, _ in obj.gsm_cells
50             ]
51         ),
52     )
53     print(qry)
54     lc.execute(qry)
55     for lat, lon in lc:
56         print(lat, lon)