1 import googlemaps as gmaps
2 from sqlite3 import connect
9 with open(conf["googlemaps"]["accesstoken"], encoding="ascii") as fl:
10 token = fl.read().rstrip()
11 gclient = gmaps.Client(key=token)
14 def lookup(mcc, mnc, gsm_cells, wifi_aps):
16 "home_mobile_country_code": mcc,
17 "home_mobile_network_code": mnc,
23 "locationAreaCode": loc,
25 "signalStrength": sig,
27 for loc, cellid, sig in gsm_cells
29 "wifi_access_points": [
30 {"macAddress": mac, "signalStrength": sig} for mac, sig in wifi_aps
33 result = gclient.geolocate(**kwargs)
34 if "location" in result:
35 return result["location"]["lat"], result["location"]["lng"]
37 raise ValueError("google geolocation: " + str(result))
40 if __name__.endswith("__main__"):
41 from datetime import datetime, timezone
43 from .gps303proto import *
45 db = connect(sys.argv[1])
48 """select tstamp, packet from events
49 where proto in (?, ?)""",
50 (WIFI_POSITIONING.PROTO, WIFI_OFFLINE_POSITIONING.PROTO),
52 init({"googlemaps": {"accesstoken": sys.argv[2]}})
54 for timestamp, packet in c:
55 obj = parse_message(packet)
57 avlat, avlon = lookup(obj.mcc, obj.mnc, obj.gsm_cells, obj.wifi_aps)
59 "{} {:+#010.8g},{:+#010.8g}".format(
60 datetime.fromtimestamp(timestamp), avlat, avlon