2 Lookaside backend to query local opencellid database
5 from configparser import ConfigParser
6 from sqlite3 import connect
7 from typing import Any, Dict, List, Tuple
9 __all__ = "init", "lookup"
14 def init(conf: ConfigParser) -> None:
16 ldb = connect(conf["opencellid"]["dbfn"])
17 ldb.execute("create temp table seen (locac int, cellid int, signal int)")
26 mcc: int, mnc: int, gsm_cells: List[Tuple[int, int, int]], __: Any
27 ) -> Tuple[float, float, float]:
28 assert ldb is not None
31 "insert into seen (locac, cellid, signal) values (?, ?, ?)",
36 """select c.lat, c.lon, s.signal
41 and c.cell = s.cellid""",
44 data = list(lc.fetchall())
45 # This should be faster than dropping and recreating the temp table
46 # https://www.sqlite.org/lang_delete.html#the_truncate_optimization
47 lc.execute("delete from seen")
50 raise ValueError("No location data found in opencellid")
51 sumsig = sum([1 / sig for _, _, sig in data])
52 nsigs = [1 / sig / sumsig for _, _, sig in data]
53 avlat = sum([lat * nsig for (lat, _, _), nsig in zip(data, nsigs)])
54 avlon = sum([lon * nsig for (_, lon, _), nsig in zip(data, nsigs)])
55 return avlat, avlon, 99.9 # TODO estimate accuracy for real