Skip to content

Commit cd6e7ab

Browse files
committed
Update mapping example (clear map before drawing tiles)
Signed-off-by: martinRenou <[email protected]>
1 parent f39686a commit cd6e7ab

File tree

1 file changed

+27
-57
lines changed

1 file changed

+27
-57
lines changed

examples/mapping.ipynb

Lines changed: 27 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@
77
"outputs": [],
88
"source": [
99
"from math import pow, cos, pi, radians, degrees, atan, tan, sinh, log\n",
10-
"from ipywidgets import Image, Output, IntSlider\n",
11-
"from ipycanvas import Canvas, MultiCanvas\n",
10+
"\n",
11+
"from ipywidgets import Image, IntSlider\n",
12+
"\n",
1213
"from ipyevents import Event\n",
13-
"import asyncio"
14+
"\n",
15+
"from ipycanvas import Canvas, MultiCanvas, hold_canvas"
1416
]
1517
},
1618
{
@@ -19,6 +21,8 @@
1921
"metadata": {},
2022
"outputs": [],
2123
"source": [
24+
"import asyncio\n",
25+
"\n",
2226
"class Timer:\n",
2327
" def __init__(self, timeout, callback):\n",
2428
" self._timeout = timeout\n",
@@ -34,17 +38,17 @@
3438
"\n",
3539
"def debounce(wait):\n",
3640
" \"\"\" Decorator that will postpone a function's\n",
37-
" execution until after wait seconds\n",
41+
" execution until after `wait` seconds\n",
3842
" have elapsed since the last time it was invoked. \"\"\"\n",
3943
" def decorator(fn):\n",
44+
" timer = None\n",
4045
" def debounced(*args, **kwargs):\n",
46+
" nonlocal timer\n",
4147
" def call_it():\n",
4248
" fn(*args, **kwargs)\n",
43-
" try:\n",
44-
" debounced.t.cancel()\n",
45-
" except(AttributeError):\n",
46-
" pass\n",
47-
" debounced.t = Timer(wait, call_it)\n",
49+
" if timer is not None:\n",
50+
" timer.cancel()\n",
51+
" timer = Timer(wait, call_it)\n",
4852
" return debounced\n",
4953
" return decorator"
5054
]
@@ -150,15 +154,6 @@
150154
" return {'pix': xys, 'tile': xyn}"
151155
]
152156
},
153-
{
154-
"cell_type": "code",
155-
"execution_count": null,
156-
"metadata": {},
157-
"outputs": [],
158-
"source": [
159-
"out = Output(layout={'border': '1px solid black'})"
160-
]
161-
},
162157
{
163158
"cell_type": "code",
164159
"execution_count": null,
@@ -184,22 +179,27 @@
184179
" self[1].on_mouse_move(self.mouse_move_handler)\n",
185180
" self[1].on_mouse_up(self.mouse_up_handler)\n",
186181
" self[1].on_mouse_out(self.mouse_out_handler)\n",
187-
" \n",
188-
" #@out.capture()\n",
182+
"\n",
189183
" def show(self, lat=None, lon=None):\n",
190184
" if lat is None:\n",
191185
" lat = self.lat\n",
192186
" if lon is None:\n",
193187
" lon = self.lon\n",
194188
" x, y = latlon2xy(lat, lon, self.z)\n",
195189
" grid = get_tile_grid(x, y, self.width, self.height, self.tile_nb)\n",
196-
" for i, row in enumerate(grid['pix']):\n",
197-
" for j, col in enumerate(row):\n",
198-
" dx, dy = col\n",
199-
" x, y = grid['tile'][i][j]\n",
200-
" url = f'https://tile.openstreetmap.org/{self.z}/{x}/{y}.png'\n",
201-
" tile = Image.from_url(url)\n",
202-
" self[0].draw_image(tile, dx, dy)\n",
190+
" \n",
191+
" with hold_canvas(self):\n",
192+
" self[0].fill_style = '#aad3df'\n",
193+
" self[0].fill_rect(0, 0, self.size[0], self.size[1])\n",
194+
"\n",
195+
" for i, row in enumerate(grid['pix']):\n",
196+
" for j, col in enumerate(row):\n",
197+
" dx, dy = col\n",
198+
" x, y = grid['tile'][i][j]\n",
199+
" url = f'https://tile.openstreetmap.org/{self.z}/{x}/{y}.png'\n",
200+
" tile = Image.from_url(url)\n",
201+
" self[0].draw_image(tile, dx, dy)\n",
202+
"\n",
203203
" def mouse_down_handler(self, pixel_x, pixel_y):\n",
204204
" self.dragging = True\n",
205205
" self.x_mouse = pixel_x\n",
@@ -237,36 +237,6 @@
237237
"m = Map(788, 788, 7, 49, 2)\n",
238238
"m"
239239
]
240-
},
241-
{
242-
"cell_type": "code",
243-
"execution_count": null,
244-
"metadata": {},
245-
"outputs": [],
246-
"source": [
247-
"d = Event(source=m, watched_events=['wheel'])\n",
248-
"\n",
249-
"@out.capture()\n",
250-
"def zoom(event):\n",
251-
" m.z -= event['deltaY'] // 3\n",
252-
" m.z = max(m.z, 1)\n",
253-
" m.z = min(m.z, 19)\n",
254-
" print(m.z)\n",
255-
" m.tile_nb = numTiles(m.z)\n",
256-
" m.x, m.y = latlon2xy(m.lat, m.lon, m.z)\n",
257-
" m.show()\n",
258-
"\n",
259-
"d.on_dom_event(zoom)"
260-
]
261-
},
262-
{
263-
"cell_type": "code",
264-
"execution_count": null,
265-
"metadata": {},
266-
"outputs": [],
267-
"source": [
268-
"out"
269-
]
270240
}
271241
],
272242
"metadata": {

0 commit comments

Comments
 (0)