-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwidget.py
435 lines (345 loc) · 13.7 KB
/
widget.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
"""Map widget"""
import itertools
from typing import Iterator
import dearpygui.dearpygui as dpg
from dearpygui_map.geo import Coordinate
from dearpygui_map.io import TileHandler
from dearpygui_map.tile_source import OpenStreetMap, TileServer, TileSpec
class MapWidget:
"""Map widget for Dear PyGui"""
def __init__(
self,
width: int,
height: int,
center: tuple[float, float],
zoom_level: int,
tile_server: TileServer,
):
"""Initialize map widget
Args:
width (int): Widget width
height (int): Widget height
center (tuple[float, float]): Center point coordinates:
latitude, longitude
zoom_level (int): Tile map zoom level
tile_server (TileServer): Tile supplier, from
dearpygui_map.tile_source
"""
self.width = width
self.height = height
self.origin = Coordinate.from_latlon(*center).with_screen_offset(
-width / 2, -height / 2, zoom=zoom_level, resolution=tile_server.tile_size
)
self.zoom_level = zoom_level
self.tile_manager = TileManager(
origin=self.origin,
width=width,
height=height,
zoom_level=zoom_level,
tile_server=tile_server,
)
self.widget: int | str = None
self.global_handler: int | str = None
self._left_mouse_pressed: bool = False
self.last_drag: tuple[float, float] = (0.0, 0.0)
def insert_widget(self) -> int | str:
"""Draw widget, insert Dear PyGui code
Returns:
int | str: Dear PyGui drawlist, with map content
"""
with dpg.handler_registry() as self.global_handler:
dpg.add_mouse_click_handler(callback=self._mouse_click_cb)
dpg.add_mouse_drag_handler(callback=self._mouse_drag_cb)
dpg.add_mouse_release_handler(callback=self._mouse_release_cb)
dpg.add_mouse_wheel_handler(callback=self._mouse_wheel_cb)
with dpg.drawlist(width=self.width, height=self.height) as self.widget:
dpg.draw_rectangle((0, 0), (self.width, self.height))
self.tile_manager.add_tile_layer()
self.tile_manager.add_tile_license()
self.tile_manager.draw_layer()
return self.widget
def draw_layers(self):
"""Redraw tile layer, for example after dragging operation"""
self.tile_manager.draw_layer()
def zoom_on_point(self, canvas_position: tuple[float, float], zoom_level: int):
"""Zoom in/out, while keeping focus on given point
NOTE: it is necessary to call `self.refresh_layers` after this.
Args:
canvas_position (tuple[float, float]): X, y point on canvas,
which retains its screen position on zoom change.
zoom_level (int): New zoom level. If zoom level is less than
zero or greater than tile_server.max_zoom_level,
do nothing.
"""
if zoom_level < 0 or zoom_level > self.tile_manager.tile_server.max_zoom_level:
return
focus_position = self.origin.with_screen_offset(
*canvas_position,
zoom=self.zoom_level, # old zoom level
resolution=self.tile_manager.tile_server.tile_size,
)
self.origin = focus_position.with_screen_offset(
*[-i for i in canvas_position],
zoom=zoom_level, # new zoom level
resolution=self.tile_manager.tile_server.tile_size,
)
self.zoom_level = zoom_level
self.tile_manager.set_origin(self.origin, zoom_level)
def get_coordinate(
self,
canvas_x: float,
canvas_y: float,
) -> Coordinate:
"""Get lat, lon coordinates for a x, y point on canvas
Args:
canvas_x (float): Point x coordinate on canvas
canvas_y (float): Point y coordinate on canvas
Returns:
Coordinate: Location for the point on canvas
"""
return self.origin.with_screen_offset(
canvas_x,
canvas_y,
zoom=self.zoom_level,
resolution=self.tile_manager.tile_server.tile_size,
)
def _drag_canvas(self, delta_x: float, delta_y: float):
"""Move canvas by dragging
NOTE: it is necessary to call `self.refresh_layers` after this,
and `self._finish_drag`, when dragging is released.
Args:
delta_x (float): Change in x position
delta_y (float): Change in y position
"""
self.last_drag = (delta_x, delta_y)
self.tile_manager.drag_layer(delta_x, delta_y)
def _finish_drag(self):
"""Finish dragging; update tile positions"""
if self.last_drag == (0.0, 0.0):
return
self.origin = self.origin.with_screen_offset(
*[-i for i in self.last_drag],
zoom=self.zoom_level,
)
self.last_drag = (0.0, 0.0)
self.tile_manager.finish_drag()
def _mouse_click_cb(self, sender: int | str, app_data: int) -> None:
"""Callback on mouse click"""
if dpg.is_item_left_clicked(self.widget):
self._left_mouse_pressed = True
def _mouse_release_cb(self, sender: int | str, app_data: int) -> None:
"""Callback on mouse release"""
self._left_mouse_pressed = False
self._finish_drag()
def _mouse_drag_cb(self, sender: int | str, app_data: list[int | float]) -> None:
"""Callback on mouse drag"""
_, delta_x, delta_y = app_data
if self._left_mouse_pressed:
self._drag_canvas(delta_x, delta_y)
self.draw_layers()
def _mouse_wheel_cb(self, sender: int | str, app_data: int) -> None:
"""Callback on mouse wheel"""
del sender # unused
delta_zoom = app_data
canvas_pos = dpg.get_drawing_mouse_pos()
self.zoom_on_point(canvas_pos, self.zoom_level + delta_zoom)
self.draw_layers()
class TileManager:
"""Tile manager for MapWidget"""
def __init__(
self,
origin: Coordinate,
width: int,
height: int,
zoom_level: int,
tile_server: TileServer,
) -> None:
self.viewport_size = width, height
self.origin = origin
self.zoom_level = zoom_level
self.tile_server = tile_server
self.tile_draw_node_id: int | str = None
self.tiles: list[TileSpec] = []
self.last_drag: tuple[float, float] = (0.0, 0.0)
self.origin_offset = (0, 0)
self.set_origin(origin, zoom_level)
def set_origin(self, origin: Coordinate, zoom_level: int):
"""Set widget map origin position
Set self.zoom_level and self.origin_offset
Args:
origin (Coordinate): Origin point
zoom_level (int): Zoom level
"""
self.origin = origin
origin_xy = origin.tile_xy(zoom_level, floor_=False)
self.zoom_level = zoom_level
self.origin_offset: tuple[float, float] = tuple(
-origin_xy[i] * self.tile_server.tile_size[i] + self.last_drag[i]
for i in range(2)
)
def add_tile_layer(self):
"""Add Dear PyGui draw layer for map tiles"""
tile_layer = dpg.add_draw_layer(label="tiles")
self.tile_draw_node_id = dpg.add_draw_node(parent=tile_layer)
def add_tile_license(self):
"""Add tile lisence as a new layer"""
width_guess = len(self.tile_server.license_text) * 7 + 4
x_text = max(0, self.viewport_size[0] - width_guess)
y_text = max(0, self.viewport_size[1] - 13)
with dpg.draw_layer(label="license"):
dpg.draw_rectangle(
(max(0, x_text - 4), y_text),
self.viewport_size,
fill=(255, 255, 255, 100),
color=(255, 255, 255, 100),
)
dpg.draw_text(
(x_text, y_text),
color=(0, 0, 0, 255),
text=self.tile_server.license_text,
size=13,
)
def draw_layer(self):
"""Update tile layer, if there are too few tiles displayed"""
dpg.apply_transform(
self.tile_draw_node_id,
dpg.create_translation_matrix(
[sum(i) for i in zip(self.origin_offset, self.last_drag)]
),
)
missing_tiles = list(self._required_tiles_for_view())
if len(missing_tiles) == 0:
return
# TODO purge those tiles from draw layer that are too far away to display
# FIXME make tile handler persistent (do not spawn new handlers whenever scrolling)
downloader = TileHandler(
missing_tiles, self.draw_tile, thread_count=self.tile_server.thread_limit
)
downloader.start()
def _required_tiles_for_view(self) -> Iterator[TileSpec]:
"""Get tiles that need to be downloaded for a view
Check that which tiles are already in `self.tiles`, return specs for
the others that are not.
Yields:
TileSpec: Tile specification
"""
# TODO add some margin - download more tiles than should be absolutely required
tile_xyz_tuples = self._get_visible_tiles()
tile_specs = itertools.starmap(self.tile_server.to_tile_spec, tile_xyz_tuples)
tile_specs = filter(lambda ts: ts not in self.tiles, tile_specs)
yield from tile_specs
def _get_visible_tiles(self) -> Iterator[tuple[int, int, int]]:
"""Calculate which tiles should be visible
Compute given current origin offset, drag and zoom level
Yields:
tuple[int, int, int]: tile (x, y, z) values
"""
origin_offset = tuple(-i for i in self.last_drag)
min_point = self.origin.with_screen_offset(
*origin_offset,
zoom=self.zoom_level,
)
max_point = min_point.with_screen_offset(
*self.viewport_size, zoom=self.zoom_level
)
min_xy = min_point.tile_xy(self.zoom_level)
max_xy = max_point.tile_xy(self.zoom_level)
yield from itertools.product(
range(min_xy[0], max_xy[0] + 1),
range(min_xy[1], max_xy[1] + 1),
(self.zoom_level,),
)
return tuple(p.tile_xy(zoom=self.zoom_level) for p in [min_point, max_point])
def draw_tile(self, tile_spec: TileSpec):
"""Draw tile on canvas
If image loading fails, draw gray area. Otherwise, add tile
spec to self.tiles
"""
if tile_spec in self.tiles:
return
tile = MapTile(tile_spec)
if tile.draw_image(parent=self.tile_draw_node_id):
self.tiles.append(tile_spec)
def drag_layer(self, delta_x: float, delta_y: float):
"""Move layer to new position
NOTE: after calling this, one must redraw contents with
`TileManager.draw_layer`. Dragging action is finished with
`TileManager.finish_drag`, after mouse button is released.
Args:
delta_x (float): Change in x position
delta_y (float): Change in y position
"""
self.last_drag = (delta_x, delta_y)
def finish_drag(self):
"""Finish dragging; update tile positions"""
if self.last_drag == (0.0, 0.0):
return
self.origin_offset = tuple(
sum(i) for i in zip(self.origin_offset, self.last_drag)
)
origin_offset = tuple(-i for i in self.last_drag)
self.origin = self.origin.with_screen_offset(
*origin_offset,
zoom=self.zoom_level,
)
self.last_drag = (0.0, 0.0)
class MapTile:
"""Map tile"""
def __init__(self, tile_spec: TileSpec):
self.file = tile_spec.local_storage_path
self.x_canvas, self.y_canvas = tile_spec.canvas_coordinates()
self.width, self.height = tile_spec.tile_size
self.tile_spec = tile_spec
self.image_tag = None
def draw_image(self, parent: int | str) -> bool:
"""Draw tile
Place tile on canvas. If image loading fails, draw gray area.
Returns:
bool: True if image loading failed, False otherwise
"""
pmin = (self.x_canvas, self.y_canvas)
pmax = (self.x_canvas + self.width, self.y_canvas + self.height)
dpg_image = dpg.load_image(str(self.file))
if dpg_image is None:
dpg.draw_rectangle(pmin, pmax, fill=(128, 128, 128, 255), parent=parent)
return False
width, height, _, data = dpg_image
tile_registry = dpg.add_texture_registry()
texture = dpg.add_static_texture(width, height, data, parent=tile_registry)
if texture is None:
dpg.draw_rectangle(pmin, pmax, fill=(128, 128, 128, 255), parent=parent)
return False
self.image_tag = dpg.draw_image(
texture,
pmin,
pmax,
parent=parent,
)
return True
def add_map_widget(
width: int,
height: int,
center: tuple[float, float],
zoom_level: int,
tile_server: TileServer = OpenStreetMap,
) -> int | str:
"""Add map widget
Args:
width (int): Widget width
height (int): Widget height
center (tuple[float, float]): Center point coordinates:
latitude, longitude
zoom_level (int): Tile map zoom level
tile_server (TileServer): Tile supplier, from
dearpygui_map.tile_source. Default: OpenStreetMap
Returns:
int | str: Tag of Dear PyGui drawlist
"""
map_widget = MapWidget(
width=width,
height=height,
center=center,
zoom_level=zoom_level,
tile_server=tile_server,
)
return map_widget.insert_widget()