Skip to content

Commit 24b80de

Browse files
committed
add dynamic redraw of leaflet if max points is changed
add dynamic redraw of ATL03 if classifier or ATL06-SR changes
1 parent 79f80bb commit 24b80de

File tree

1 file changed

+68
-56
lines changed

1 file changed

+68
-56
lines changed

demo/voila_demo.ipynb

Lines changed: 68 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,6 @@
129129
"update_button = widgets.Button(description=\"Update Map\")\n",
130130
"run_button = widgets.Button(description=\"Run SlideRule!\")\n",
131131
"run_output = widgets.Output()\n",
132-
"refresh_button = widgets.Button(description=\"Refresh Plot\")\n",
133-
"refresh_output = widgets.Output()\n",
134132
"download_output = widgets.Output()\n",
135133
"download_atl06_button = widgets.Button(description=\"Download File\")\n",
136134
"download_atl03_button = widgets.Button(description=\"Download File\")\n",
@@ -280,6 +278,7 @@
280278
"\n",
281279
" # clear existing geodataframe results\n",
282280
" elevations = [sliderule.emptyframe()]\n",
281+
" sliderule.logger.warning('No valid regions to run') if not m.regions else None\n",
283282
"\n",
284283
" # for each region of interest\n",
285284
" for poly in m.regions:\n",
@@ -295,7 +294,7 @@
295294
"\n",
296295
"# run sliderule action\n",
297296
"def on_run_clicked(b):\n",
298-
" global atl06_rsps, points_dropdown\n",
297+
" global atl06_rsps, points_dropdown, stride\n",
299298
" with run_output:\n",
300299
" print(f'SlideRule processing request... initiated\\r', end=\"\")\n",
301300
" perf_start = time.perf_counter()\n",
@@ -307,15 +306,16 @@
307306
" if points_dropdown.value == \"100K\":\n",
308307
" max_plot_points = 100000\n",
309308
" elif points_dropdown.value == \"all\":\n",
310-
" max_plot_points = 1000000000\n",
311-
" if max_plot_points > atl06_rsps.shape[0]:\n",
312-
" max_plot_points = atl06_rsps.shape[0]\n",
313-
" print(f'Plotting {max_plot_points} of {atl06_rsps.shape[0]} elevations. This may take 10-60+ seconds for larger point datasets.')\n",
309+
" max_plot_points = np.inf\n",
310+
" # limit number of points to plot\n",
311+
" plot_points = np.minimum(max_plot_points, atl06_rsps.shape[0])\n",
312+
" stride = int(atl06_rsps.shape[0]//plot_points)\n",
313+
" print(f'Plotting {plot_points} of {atl06_rsps.shape[0]} elevations. This may take 10-60+ seconds for larger point datasets.')\n",
314314
" fields = atl06_rsps.leaflet.default_atl06_fields()\n",
315315
" atl06_rsps.leaflet.GeoData(m.map,\n",
316316
" column_name=SRwidgets.variable.value,\n",
317317
" cmap=SRwidgets.colormap,\n",
318-
" max_plot_points=max_plot_points,\n",
318+
" stride=stride,\n",
319319
" tooltip=True,\n",
320320
" colorbar=True,\n",
321321
" fields=fields\n",
@@ -326,31 +326,29 @@
326326
" m.add_region_callback(atl06_rsps.leaflet.handle_region)\n",
327327
"\n",
328328
"# refresh action\n",
329-
"def on_refresh_clicked(b):\n",
330-
" global atl06_rsps\n",
331-
" with refresh_output:\n",
332-
" if atl06_rsps is not None and atl06_rsps.shape[0] > 0:\n",
329+
"def refresh_leaflet_draw(sender):\n",
330+
" global atl06_rsps, points_dropdown, stride\n",
331+
" if atl06_rsps is not None and atl06_rsps.shape[0] > 0:\n",
332+
" if isinstance(sender['new'], str) and (sender['new'] == \"10K\"):\n",
333333
" max_plot_points = 10000\n",
334-
" if points_dropdown.value == \"100K\":\n",
335-
" max_plot_points = 100000\n",
336-
" elif points_dropdown.value == \"all\":\n",
337-
" max_plot_points = 1000000000\n",
338-
" if max_plot_points > atl06_rsps.shape[0]:\n",
339-
" max_plot_points = atl06_rsps.shape[0]\n",
340-
" print(f'Plotting {max_plot_points} of {atl06_rsps.shape[0]} elevations. This may take 10-60+ seconds for larger point datasets.')\n",
341-
" fields = atl06_rsps.leaflet.default_atl06_fields()\n",
342-
" atl06_rsps.leaflet.GeoData(m.map,\n",
343-
" column_name=SRwidgets.variable.value,\n",
344-
" cmap=SRwidgets.colormap,\n",
345-
" max_plot_points=max_plot_points,\n",
346-
" tooltip=True,\n",
347-
" colorbar=True,\n",
348-
" fields=fields\n",
349-
" )\n",
350-
" # install handlers and callbacks\n",
351-
" atl06_rsps.leaflet.set_observables(SRwidgets)\n",
352-
" atl06_rsps.leaflet.add_selected_callback(SRwidgets.atl06_click_handler)\n",
353-
" m.add_region_callback(atl06_rsps.leaflet.handle_region)\n",
334+
" elif isinstance(sender['new'], str) and (sender['new'] == \"100K\"):\n",
335+
" max_plot_points = 100000\n",
336+
" elif isinstance(sender['new'], str) and (sender['new'] == \"all\"):\n",
337+
" max_plot_points = np.inf\n",
338+
" else:\n",
339+
" return\n",
340+
" # limit number of points to plot\n",
341+
" plot_points = np.minimum(max_plot_points, atl06_rsps.shape[0])\n",
342+
" stride_new = int(atl06_rsps.shape[0]//plot_points)\n",
343+
" if (stride_new == stride):\n",
344+
" return\n",
345+
" # update stride\n",
346+
" stride = stride_new\n",
347+
" print(f'Plotting {plot_points} of {atl06_rsps.shape[0]} elevations. This may take 10-60+ seconds for larger point datasets.')\n",
348+
" # sliced geodataframe for plotting\n",
349+
" atl06_rsps.leaflet._gdf_selected = atl06_rsps.leaflet._gdf[slice(None,None,stride)]\n",
350+
" atl06_rsps.leaflet._gdf_selected['data'] = atl06_rsps.leaflet._gdf_selected[atl06_rsps.leaflet.column_name]\n",
351+
" atl06_rsps.leaflet.redraw()\n",
354352
"\n",
355353
"# show code action\n",
356354
"def on_show_code06_clicked(b):\n",
@@ -398,7 +396,6 @@
398396
"\n",
399397
"# link buttons\n",
400398
"run_button.on_click(on_run_clicked)\n",
401-
"refresh_button.on_click(on_refresh_clicked)\n",
402399
"show_code06_button.on_click(on_show_code06_clicked)\n",
403400
"download_atl06_button.on_click(on_atl06_download_clicked)"
404401
]
@@ -430,6 +427,7 @@
430427
" description = \"Pts to Draw\",\n",
431428
" disabled = False,\n",
432429
")\n",
430+
"points_dropdown.observe(refresh_leaflet_draw)\n",
433431
"\n",
434432
"# display widgets for setting SlideRule parameters\n",
435433
"display.display(widgets.VBox([\n",
@@ -449,9 +447,9 @@
449447
"]))\n",
450448
"\n",
451449
"# display buttons\n",
452-
"display.display(SRwidgets.HBox([run_button, refresh_button, refresh_output]))\n",
450+
"display.display(run_button)\n",
453451
"display.display(SRwidgets.HBox([download_atl06_button, SRwidgets.file_format]))\n",
454-
"display.display(SRwidgets.HBox([show_code06_button, show_code06_output]))\n"
452+
"display.display(SRwidgets.HBox([show_code06_button, show_code06_output]))"
455453
]
456454
},
457455
{
@@ -564,7 +562,7 @@
564562
"\n",
565563
"# photon_cloud action\n",
566564
"def on_pc_clicked(b):\n",
567-
" global atl03_rsps, atl06_rsps, elev_dropdown\n",
565+
" global atl03_rsps, atl06_rsps, elev_dropdown, fig\n",
568566
" with pc_output:\n",
569567
" pc_output.clear_output(True)\n",
570568
"\n",
@@ -597,8 +595,35 @@
597595
" **SRwidgets.plot_kwargs)\n",
598596
" # draw and show plot\n",
599597
" plt.show()\n",
600-
" plt.draw()\n",
601-
"\n",
598+
" fig.canvas.draw()\n",
599+
"\n",
600+
"def refresh_ATL03_plot(sender):\n",
601+
" global atl03_rsps, atl06_rsps, elev_dropdown, fig\n",
602+
" if isinstance(sender['new'], str):\n",
603+
" # reset figure and axes\n",
604+
" fig.clear()\n",
605+
" fig.set_facecolor('white')\n",
606+
" fig.canvas.header_visible = False\n",
607+
" ax = fig.add_subplot(111)\n",
608+
" # reset title and labels\n",
609+
" ax.set_title(\"Photon Cloud\")\n",
610+
" ax.set_ylabel('height (m)')\n",
611+
" # start at the first segment\n",
612+
" x_offset = atl03_rsps['segment_dist'].min()\n",
613+
" # plot ATL03 and ATL06 data\n",
614+
" atl03_rsps.icesat2.plot(ax=ax, kind='scatter',\n",
615+
" data_type='atl03', cmap=SRwidgets.colormap,\n",
616+
" classification=SRwidgets.plot_classification.value,\n",
617+
" x_offset=x_offset, legend=True, legend_frameon=True,\n",
618+
" **SRwidgets.plot_kwargs)\n",
619+
" if (elev_dropdown.value == 'enabled'):\n",
620+
" atl06_rsps.icesat2.plot(ax=ax, kind='scatter',\n",
621+
" data_type='atl06', x_offset=x_offset,\n",
622+
" legend=True, legend_frameon=True,\n",
623+
" **SRwidgets.plot_kwargs)\n",
624+
" # draw and show plot\n",
625+
" fig.canvas.draw()\n",
626+
" \n",
602627
"# create button to display geodataframe\n",
603628
"pc_button.on_click(on_pc_clicked)\n",
604629
"\n",
@@ -638,22 +663,7 @@
638663
{
639664
"cell_type": "code",
640665
"execution_count": null,
641-
"metadata": {
642-
"extensions": {
643-
"jupyter_dashboards": {
644-
"version": 1,
645-
"views": {
646-
"default_view": {
647-
"col": 0,
648-
"height": 15,
649-
"row": 32,
650-
"width": 3
651-
}
652-
}
653-
}
654-
},
655-
"tags": []
656-
},
666+
"metadata": {},
657667
"outputs": [],
658668
"source": [
659669
"# elevation plot drop down\n",
@@ -672,7 +682,9 @@
672682
"display.display(pc_button)\n",
673683
"display.display(pc_output)\n",
674684
"display.display(SRwidgets.HBox([download_atl03_button, SRwidgets.file_format]))\n",
675-
"display.display(show_code03_button, show_code03_output)"
685+
"display.display(show_code03_button, show_code03_output)\n",
686+
"SRwidgets.plot_classification.observe(refresh_ATL03_plot)\n",
687+
"elev_dropdown.observe(refresh_ATL03_plot)"
676688
]
677689
},
678690
{
@@ -717,7 +729,7 @@
717729
"name": "python",
718730
"nbconvert_exporter": "python",
719731
"pygments_lexer": "ipython3",
720-
"version": "3.12.0"
732+
"version": "3.10.13"
721733
},
722734
"toc-showtags": false
723735
},

0 commit comments

Comments
 (0)