Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add stac opacity control #62

Merged
merged 3 commits into from
Jun 8, 2023
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 28 additions & 6 deletions stac_ipyleaflet/stac_discovery/stac_widget.py
Original file line number Diff line number Diff line change
@@ -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 = {
Expand All @@ -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"),
}

Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -341,10 +351,15 @@ def list_palette_categories():
output
]

def handle_stac_layer_opacity(change):
if self.stac_data["layer_added"] == True:
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)
Expand All @@ -362,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)
Expand Down Expand Up @@ -391,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.")
Expand Down Expand Up @@ -515,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 ":
Expand Down Expand Up @@ -571,7 +591,9 @@ 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
reset_stac_opacity_slider()
if len(bounds) > 0:
self.fit_bounds([[bounds[1], bounds[0]], [bounds[3], bounds[2]]])
output.clear_output()
Expand Down