-
Notifications
You must be signed in to change notification settings - Fork 88
/
NavSatFixViewer.py
180 lines (151 loc) · 6.26 KB
/
NavSatFixViewer.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#!/usr/bin/env python3
import functools
from io import BytesIO, StringIO
import math
import numpy as np
import requests
import time
import librosshow.termgraphics as termgraphics
# <rant>
# had issues with accessing the openstreetmap tile server over IPv6.
# force IPv4 for now. the world isn't ready for IPv6 yet. :-/
import socket
import requests.packages.urllib3.util.connection as urllib3_cn
def allowed_gai_family():
family = socket.AF_INET # force IPv4
return family
urllib3_cn.allowed_gai_family = allowed_gai_family
# </rant>
def memoize(f):
""" Memoization decorator for functions taking one or more arguments. """
class memodict(dict):
def __init__(self, f):
self.f = f
def __call__(self, *args):
return self[args]
def __missing__(self, key):
ret = self[key] = self.f(*key)
return ret
return memodict(f)
try:
from PIL import Image, ImageOps
except ImportError:
print("This message type requires an additional Python package. Please run:")
print(" $ sudo pip3 install pillow")
print("and try again.")
exit()
@memoize
def get_tile(xtile, ytile, zoom):
try:
url = 'https://a.tile.openstreetmap.org/%s/%s/%s.png' % (zoom, xtile, ytile)
response = requests.get(url, headers = {
"User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36",
"Accept-Language": "en-US,en;q=0.9",
"Http-Upgrade-Insecure-Requests": "1",
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
"Dnt": "1",
})
img = Image.open(BytesIO(response.content))
except IOError:
return None
except requests.exceptions.ConnectionError:
return None
return img.convert('RGB')
def deg2num(lat_deg, lon_deg, zoom):
lat_rad = math.radians(lat_deg)
n = 2.0 ** zoom
xtile = int((lon_deg + 180.0) / 360.0 * n)
ytile = int((1.0 - math.log(math.tan(lat_rad) + (1 / math.cos(lat_rad))) / math.pi) / 2.0 * n)
return (xtile, ytile)
def num2deg(xtile, ytile, zoom):
n = 2.0 ** zoom
lon_deg = xtile / n * 360.0 - 180.0
lat_rad = math.atan(math.sinh(math.pi * (1 - 2 * ytile / n)))
lat_deg = math.degrees(lat_rad)
return (lat_deg, lon_deg)
class NavSatFixViewer(object):
def __init__(self, canvas, title = ""):
self.g = canvas
self.title = title
self.xmin = 0
self.xmax = 1
self.ymin = 0
self.ymax = 1
self.zoom = 17
self.data = [ (0,0) ] * 128
self.pointer = 0
self.last_update_shape_time = 0
def keypress(self, c):
if c == "+" or c == "=":
self.zoom += 1
if self.zoom > 19:
self.zoom = 19
elif c == "-":
self.zoom -= 1
if self.zoom < 5:
self.zoom = 5
def update(self, msg):
self.pointer = (self.pointer + 1) % len(self.data)
self.data[self.pointer] = (msg.latitude, msg.longitude)
def draw(self):
t = time.time()
# capture changes in terminal shape at least every 0.25s
if t - self.last_update_shape_time > 0.25:
self.g.update_shape()
self.last_update_shape_time = t
lat_point = self.data[self.pointer][0]
lon_point = self.data[self.pointer][1]
width = self.g.shape[0]
height = self.g.shape[1]
xtile, ytile = deg2num(lat_point, lon_point, self.zoom)
lat_min, lon_min = num2deg(xtile, ytile, self.zoom)
lat_max, lon_max = num2deg(xtile + 1, ytile + 1, self.zoom)
img = get_tile(xtile, ytile, self.zoom)
self.g.clear()
# background map image
self.g.set_color(termgraphics.COLOR_BLUE)
if img is not None:
img = img.resize((width, height), Image.NEAREST)
img_data = np.fromstring(img.tobytes(), dtype = np.uint8).reshape((width, height, 3))
# "night mode" version of the image: just flip all the bits
img_data_night = (~img_data >> 1)
# extract the text only to re-display in higher contrast and without 2x4 block effects
img_data_text = (np.mean(img_data, axis = 2) < 163).astype(np.uint8) * 255
# draw night mode image
self.g.image(img_data_night, width, height, (0, 0), image_type = termgraphics.IMAGE_RGB)
# draw text in pure white, clearing blocks of background
self.g.set_color((255, 255, 255))
self.g.image(img_data_text, width, height, (0, 0), image_type = termgraphics.IMAGE_MONOCHROME, clear_block = True)
else:
self.g.set_color((127, 127, 127))
self.g.text("[Unable to retrieve map image; is this machine online?]", (0, 0))
# trail of last few positions
self.g.set_color(termgraphics.COLOR_YELLOW)
points = []
for k in range(len(self.data)):
for i in range(-1, 2):
for j in range(-1, 2):
points.append((
width * (self.data[k][1] - lon_min) / (lon_max - lon_min) + i,
height * (self.data[k][0] - lat_min) / (lat_max - lat_min) + j
))
self.g.points(points, clear_block = True)
# current position
self.g.set_color(termgraphics.COLOR_RED)
for i in range(-3, 4):
for j in range(-3, 4):
self.g.point((
int(width * (self.data[self.pointer][1] - lon_min) / (lon_max - lon_min)) + i,
int(height * (self.data[self.pointer][0] - lat_min) / (lat_max - lat_min)) + j
), clear_block = True)
for i in range(-3, 4):
for j in range(-3, 4):
self.g.point((
int(width * (self.data[self.pointer][1] - lon_min) / (lon_max - lon_min)) + i,
int(height * (self.data[self.pointer][0] - lat_min) / (lat_max - lat_min)) + j
), clear_block = False)
self.g.set_color((0, 127, 255))
self.g.text(self.title, (0, self.g.shape[1] - 4))
self.g.set_color((127, 127, 127))
self.g.text("+/-: zoom", (int(self.g.shape[0]/3), self.g.shape[1] - 4))
self.g.draw()