Skip to content

Commit

Permalink
#198 add roi dropdown wip
Browse files Browse the repository at this point in the history
  • Loading branch information
2320sharon committed Oct 25, 2023
1 parent c424345 commit 973b243
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 29 deletions.
58 changes: 44 additions & 14 deletions src/coastseg/coastseg_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,18 @@ class ExtractShorelinesContainer(traitlets.HasTraits):
load_list = traitlets.List(trait=traitlets.Unicode())
# list of shorelines that will be thrown away
trash_list = traitlets.List(trait=traitlets.Unicode())
# list of roi ids that have extracted shorelines
roi_ids_list = traitlets.List(trait=traitlets.Unicode())

def __init__(self, load_list_widget=None, trash_list_widget=None):
def __init__(self, load_list_widget=None, trash_list_widget=None,roi_list_widget=None):
super().__init__()
if load_list_widget:
self.link_load_list(load_list_widget)
if trash_list_widget:
self.link_trash_list(trash_list_widget)
# Link the widgets and the traits
# traitlets.dlink((self, 'ROI_ids'), (ROI_ids_widget, 'options'))
if roi_list_widget:
self.link_roi_list(roi_list_widget)

def link_load_list(self, widget):
if hasattr(widget, "options"):
Expand All @@ -75,6 +78,10 @@ def link_trash_list(self, widget):
if hasattr(widget, "options"):
traitlets.dlink((self, "trash_list"), (widget, "options"))

def link_roi_list(self, widget):
if hasattr(widget, "options"):
traitlets.dlink((self, "ROI_ids"), (widget, "options"))


class CoastSeg_Map:
def __init__(self, **kwargs):
Expand Down Expand Up @@ -173,9 +180,8 @@ def get_selected_shorelines(gdf, selected_items) -> gpd.GeoDataFrame:
# Loop through each dictionary in dates_tuple
for criteria in list(selected_items):
satname, dates = criteria.split("_")
# print(f"satname: {satname} dates: {dates}")
filtered = gdf[
(gdf["date"] == datetime.strptime(dates, "%Y-%m-%d-%H-%M-%S"))
(gdf["date"] == datetime.strptime(dates, "%Y-%m-%d %H:%M:%S"))
& (gdf["satname"] == satname)
]
frames.append(filtered)
Expand Down Expand Up @@ -207,6 +213,20 @@ def get_selected_shorelines(gdf, selected_items) -> gpd.GeoDataFrame:
if not selected_gdf.empty:
self.load_extracted_shoreline_layer(selected_gdf, layer_name, style)

def on_ROI_change(
self,
new_roi_id: str,
) -> None:
# @todo pass in the ROI ID that the extracted shorelines are from
# @todo this code is temporary
# remove the old layer
self.remove_extracted_shorelines()

# -------------------------------------------
# load first the extracted shorelines for the selected ROI ID
extracted_shorelines = self.rois.get_extracted_shoreline(new_roi_id)
self.load_extracted_shorelines_on_map(extracted_shorelines, 1)

def create_map(self):
"""create an interactive map object using the map_settings
Returns:
Expand Down Expand Up @@ -2093,6 +2113,21 @@ def get_roi_ids(
matching_ids = self.get_all_roi_ids()
return matching_ids

def update_roi_ids_with_shorelines(self):
# Get the list of the ROI IDs that have extracted shorelines
ids_with_extracted_shorelines = self.get_roi_ids(has_extracted_shorelines=True)

# if no ROIs have extracted shorelines, return otherwise load extracted shorelines for the first ROI ID with extracted shorelines
if not ids_with_extracted_shorelines:
self.id_container.ids = []
self.extract_shorelines_container.load_list = []
logger.warning("No ROIs found with extracted shorelines.")
return []

self.id_container.ids = list(ids_with_extracted_shorelines)
self.extract_shorelines_container.load_list = list(ids_with_extracted_shorelines)
return ids_with_extracted_shorelines

def load_extracted_shorelines_to_map(self, row_number: int = 0) -> None:
"""Loads stylized extracted shorelines onto the map for a single selected region of interest (ROI).
Expand All @@ -2106,19 +2141,14 @@ def load_extracted_shorelines_to_map(self, row_number: int = 0) -> None:
None: This function does not return anything, but rather loads the extracted shorelines onto the map.
"""
# Get the list of the ROI IDs that have extracted shorelines
ids_with_extracted_shorelines = self.get_roi_ids(has_extracted_shorelines=True)
ids_with_extracted_shorelines = self.update_roi_ids_with_shorelines()
# if no ROIs have extracted shorelines, return
if not ids_with_extracted_shorelines:
return
# Remove any existing extracted shorelines
self.remove_extracted_shoreline_layers()

# if no ROIs have extracted shorelines, return otherwise load extracted shorelines for the first ROI ID with extracted shorelines
if not ids_with_extracted_shorelines:
self.id_container.ids = []
logger.warning("No ROIs found with extracted shorelines.")
return
else:
self.id_container.ids = list(ids_with_extracted_shorelines)
# Load extracted shorelines for the first ROI ID with extracted shorelines

# select the first ROI ID with extracted shorelines
selected_id = ids_with_extracted_shorelines[0]
# load the extracted shorelines for the selected ROI ID
Expand All @@ -2131,7 +2161,7 @@ def load_extracted_shorelines_to_map(self, row_number: int = 0) -> None:
extracted_shorelines.gdf["satname"]
+ "_"
+ extracted_shorelines.gdf["date"].apply(
lambda x: x.strftime("%Y-%m-%d-%H-%M-%S")
lambda x: x.strftime("%Y-%m-%d %H:%M:%S")
)
).tolist()

Expand Down
38 changes: 23 additions & 15 deletions src/coastseg/extract_shorelines_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,10 @@ def __init__(self, extracted_shoreline_traitlet):
options=[],
layout=ipywidgets.Layout(padding="0px", margin="0px"),
)
self.ROI_list_widget = ipywidgets.dropdown(
self.ROI_list_widget = ipywidgets.Dropdown(
description="Available ROIs",
options=[],
layout=ipywidgets.Layout(padding="0px", margin="0px"),
layout=ipywidgets.Layout(width="90%", padding="0px", margin="0px"),
)

# Buttons
Expand Down Expand Up @@ -128,6 +128,8 @@ def __init__(self, extracted_shoreline_traitlet):
self.undo_button.on_click(self.undo_button_clicked)
self.empty_trash_button.on_click(self.delete_all_button_clicked)

# callback function for when a roi is selected
self.ROI_list_widget.observe(self.on_roi_selected, names="value")
# callback function for when a load shoreline item is selected
self.load_list_widget.observe(self.on_load_selected, names="value")
# callback function for when a trash item is selected
Expand Down Expand Up @@ -162,6 +164,7 @@ def __init__(self, extracted_shoreline_traitlet):
total_VBOX = ipywidgets.VBox(
[
title_html,
self.ROI_list_widget,
load_instruction_box,
load_list_vbox,
trash_instruction_box,
Expand All @@ -174,16 +177,16 @@ def __init__(self, extracted_shoreline_traitlet):

super().__init__([total_VBOX])

# def add_ROI_callback(self, callback: Callable[[List[str]], None]):
# """
# Add a callback function to be called when a ROI ID is selected.
def add_ROI_callback(self, callback: Callable[[List[str]], None]):
"""
Add a callback function to be called when a ROI ID is selected.
# Parameters
# ----------
# callback : function
# The function to be called when a ROI ID is selected.
# """
# self.roi_selected_callback = callback
Parameters
----------
callback : function
The function to be called when a ROI ID is selected.
"""
self.roi_selected_callback = callback

def add_load_callback(self, callback: Callable[[List[str]], None]):
"""
Expand Down Expand Up @@ -218,10 +221,15 @@ def add_remove_callback(self, callback: Callable[[List[str]], None]):
"""
self.remove_callback = callback

# def on_roi_selected(self, change: dict):
# """Callback function for when an ROI ID is selected"""
# # when the content sof the load list changes update the layer
# # self.roi_selected_callback(change["new"])
def on_roi_selected(self, change: dict):
"""Callback function for when an ROI ID is selected"""
# when the content sof the load list changes update the layer
# clear the load and trash lists
self.load_list_widget.options = []
self.trash_list_widget.options = []
print(change["new"])
# call the callback function
self.roi_selected_callback(change["new"])

def on_load_selected(self, change: dict):
"""Callback function for when a load shoreline item is selected"""
Expand Down
6 changes: 6 additions & 0 deletions src/coastseg/map_UI.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ def __init__(self, coastseg_map, **kwargs):
self.extract_shorelines_widget.add_load_callback(
coastseg_map.load_selected_shorelines_on_map
)
self.extract_shorelines_widget.add_ROI_callback(
coastseg_map.load_selected_shorelines_on_map
)
self.extract_shorelines_widget.add_remove_all_callback(
coastseg_map.delete_selected_shorelines
)
Expand All @@ -98,6 +101,9 @@ def __init__(self, coastseg_map, **kwargs):
coastseg_map.extract_shorelines_container.link_trash_list(
self.extract_shorelines_widget.trash_list_widget
)
coastseg_map.extract_shorelines_container.link_roi_list(
self.extract_shorelines_widget.roi_list_widget
)

# create button styles
self.create_styles()
Expand Down

0 comments on commit 973b243

Please sign in to comment.