From 1fa20086ab022cdd573087170a31d1bc8a7753f8 Mon Sep 17 00:00:00 2001 From: Emma Paz Date: Tue, 6 Jun 2023 14:59:03 +0000 Subject: [PATCH 1/3] add opacity slider to control stac layer --- stac_ipyleaflet/stac_discovery/stac_widget.py | 25 ++++++++++++++++--- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/stac_ipyleaflet/stac_discovery/stac_widget.py b/stac_ipyleaflet/stac_discovery/stac_widget.py index e140b6f..b95875f 100644 --- a/stac_ipyleaflet/stac_discovery/stac_widget.py +++ b/stac_ipyleaflet/stac_discovery/stac_widget.py @@ -1,11 +1,12 @@ from datetime import datetime -from ipywidgets import Box, Combobox, DatePicker, Dropdown, HBox, HTML -from ipywidgets import Layout, Output, RadioButtons, Tab, ToggleButtons, VBox +from ipywidgets import Box, DatePicker, Dropdown, HBox, HTML +from ipywidgets import Layout, Output, RadioButtons, SelectionSlider, Tab, ToggleButtons, VBox from pystac_client import Client from stac_ipyleaflet.stac_discovery.stac import Stac class StacDiscoveryWidget(): def template(self) -> Box( style={"max_height: 200px"}): + opacity_values = [i*10 for i in range(10+1)] # [0.001, 0.002, ...] titiler_stac_endpoint = "https://titiler.maap-project.org" standard_width = "440px" styles = { @@ -15,7 +16,7 @@ def template(self) -> Box( style={"max_height: 200px"}): layouts = { "default": Layout(width=standard_width, padding="2px 6px"), "header": Layout(width=standard_width, padding="2px 6px", margin="2px 2px -6px 2px"), - "buttons": Layout(display="flex", flex_flow="row", justify_content="flex-end", margin="0.5rem 1.5rem"), + "buttons": Layout(display="flex", flex_flow="row", align_items="flex-end", justify_content="flex-end", margin="0.5rem 1.5rem"), "radio": Layout(display="flex", width="max-content", padding="2px 6px"), } @@ -304,7 +305,16 @@ def list_palette_categories(): disabled=True, tooltips=["Display selected Item on the Map"], ) - buttons_box = Box([stac_buttons], layout=layouts["buttons"]) + stac_opacity_slider = SelectionSlider( + value=1, + options=[("%g"%i, i/100) for i in opacity_values], + description="STAC Layer", + continuous_update=False, + orientation='horizontal', + layout=Layout(margin="-12px 0 4px 0") + ) + + buttons_box = Box([ stac_opacity_slider, stac_buttons], layout=layouts["buttons"]) stac_tab_labels = ['Catalog', 'Visualization'] tab_widget_children = [] stac_tab_widget = Tab() @@ -341,6 +351,12 @@ def list_palette_categories(): output ] + def handle_stac_layer_opacity(change): + selected_layer = change.owner.description + print(selected_layer) + if self.stac_data["layer_added"] == True: + l = self.layers[-1] + l.opacity = change["new"] def prep_data_display_settings(): is_displayable = False @@ -571,6 +587,7 @@ def button_clicked(change): self.layers = self.layers[:len(self.layers)-1] self.stac_data["layer_added"] = False self.add_tile_layer(url=tile_url, name=items_dropdown.value, attribution=items_dropdown.value) + stac_opacity_slider.observe(handle_stac_layer_opacity, names="value") self.stac_data["layer_added"] = True if len(bounds) > 0: self.fit_bounds([[bounds[1], bounds[0]], [bounds[3], bounds[2]]]) From 3786aa7c10862bd928b3cc707e4d005dc302a12d Mon Sep 17 00:00:00 2001 From: Emma Paz Date: Tue, 6 Jun 2023 18:09:32 +0000 Subject: [PATCH 2/3] use selected item dropdown value for layer opacity --- stac_ipyleaflet/stac_discovery/stac_widget.py | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/stac_ipyleaflet/stac_discovery/stac_widget.py b/stac_ipyleaflet/stac_discovery/stac_widget.py index b95875f..c0840e6 100644 --- a/stac_ipyleaflet/stac_discovery/stac_widget.py +++ b/stac_ipyleaflet/stac_discovery/stac_widget.py @@ -352,15 +352,14 @@ def list_palette_categories(): ] def handle_stac_layer_opacity(change): - selected_layer = change.owner.description - print(selected_layer) if self.stac_data["layer_added"] == True: - l = self.layers[-1] - l.opacity = change["new"] + l = self.find_layer(items_dropdown.value) + if l.name: + l.opacity = change["new"] def prep_data_display_settings(): is_displayable = False - + stac_opacity_slider.disabled = True assets = [i for i in self.stac_data["items"] if i["id"] == items_dropdown.value][0]["assets"] item_href = [i for i in self.stac_data["items"] if i["id"] == items_dropdown.value][0]["href"] metadata = Stac.get_item_info(url=item_href) @@ -378,7 +377,7 @@ def prep_data_display_settings(): data_types = data_asset.media_type # print(f"{asset} data type:", data_types) if "application=geotiff" in data_types and "profile=cloud-optimized" in data_types: - is_displayable = True + is_displayable = True # if "statistics" in metadata: # minv, maxv = metadata["statistics"]["1"]["min"], metadata["statistics"]["1"]["max"] # print("MIN/MAX", minv, maxv) @@ -407,6 +406,8 @@ def prep_data_display_settings(): output.clear_output() print("Item is ready for display.") else: + stac_buttons.disabled = True + stac_opacity_slider.disabled = True with output: output.clear_output() print("This item cannot displayed. Only Cloud-Optimized GeoTIFFs are supported at this time.") @@ -531,6 +532,9 @@ def palette_category_changed(change): # palette.value = None # raster_options.children = [] """ + def reset_stac_opacity_slider(): + stac_opacity_slider.value = 1 + stac_opacity_slider.disabled = False def button_clicked(change): if change["new"] == "Display ": @@ -589,6 +593,7 @@ def button_clicked(change): self.add_tile_layer(url=tile_url, name=items_dropdown.value, attribution=items_dropdown.value) stac_opacity_slider.observe(handle_stac_layer_opacity, names="value") self.stac_data["layer_added"] = True + reset_stac_opacity_slider() if len(bounds) > 0: self.fit_bounds([[bounds[1], bounds[0]], [bounds[3], bounds[2]]]) output.clear_output() From a10aa97410eb5b53bc118da51308719f3b889536 Mon Sep 17 00:00:00 2001 From: Emma Paz Date: Wed, 7 Jun 2023 21:49:41 +0000 Subject: [PATCH 3/3] slider label update to match basemap --- stac_ipyleaflet/stac_discovery/stac_widget.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stac_ipyleaflet/stac_discovery/stac_widget.py b/stac_ipyleaflet/stac_discovery/stac_widget.py index c0840e6..46e8bd6 100644 --- a/stac_ipyleaflet/stac_discovery/stac_widget.py +++ b/stac_ipyleaflet/stac_discovery/stac_widget.py @@ -308,7 +308,7 @@ def list_palette_categories(): stac_opacity_slider = SelectionSlider( value=1, options=[("%g"%i, i/100) for i in opacity_values], - description="STAC Layer", + description="% Opacity:", continuous_update=False, orientation='horizontal', layout=Layout(margin="-12px 0 4px 0")