-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot1090.py
83 lines (72 loc) · 1.89 KB
/
plot1090.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import getopt
import json
import sys
ALT_RANGE = (0, 999999)
CENTER = (51.4768033, 0)
# Will work for file:/// URLs, if you want to throw stuff online you'll
# need to request your own key.
API_KEY = "AIzaSyBFHaZ3hPmn908XJVXRPbxshuzfzUkzwlg"
opts, r = getopt.getopt(sys.argv[1:], "c:a:A:", ["center=", "altitude-range=", "api-key="])
for k, v in opts:
if k in ("-c", "--center"):
CENTER = tuple(float(x) for x in v.split(",")[0:2])
elif k in ("-a", "--altitude-range"):
lo, hi = v.split("-")
ALT_RANGE = ((lo and int(lo) or ALT_RANGE[0]),
(hi and int(hi) or ALT_RANGE[1]))
elif k in ("-A", "--api-key"):
API_KEY = v
else:
raise RuntimeError("BUG flag: %s" % (k, v))
def poly2gmaps(poly):
"""GMaps API wants its polygons in a bulky dict, here we
generate one from more convenient tuple form."""
return [{"lat": lat, "lng": lon} for (lat, lon) in poly]
def write_html(vars, js, title="plot1090"):
"""Throws data (after json-encoding) and some JavaScript code
into a very simple Google Maps API client HTML page."""
jsvars = []
for v in sorted(vars):
jsvars.append("%s = %s;" % (v, json.JSONEncoder().encode(vars[v])))
print """\
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<title>%(title)s</title>
<style>
html, body {
height: 100%%;
margin: 0;
padding: 0;
}
#map {
height: 100%%;
}
</style>
</head>
<body>
<div id="map"></div>
<script>
%(vars)s
function initMap() {
// Create the map.
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 8,
center: d.center,
mapTypeId: google.maps.MapTypeId.TERRAIN
});
%(js)s
}
</script>
<script async defer
src="https://maps.googleapis.com/maps/api/js?key=%(api_key)s&signed_in=true&callback=initMap"></script>
</body>
</html>
""" % {
"title": title,
"vars": "\n".join(jsvars),
"js": js,
"api_key": API_KEY,
}