diff --git a/src/coastseg/coastseg_map.py b/src/coastseg/coastseg_map.py index 95079105..89d20d26 100644 --- a/src/coastseg/coastseg_map.py +++ b/src/coastseg/coastseg_map.py @@ -1540,12 +1540,13 @@ def remove_extracted_shorelines(self): # remove extracted shoreline vectors from the map self.remove_extracted_shoreline_layers() self.id_container.ids = [] - self.extract_shorelines_container.max_shorelines = 0 + self.extract_shorelines_container.load_list = [] + self.extract_shorelines_container.trash_list = [] def remove_extracted_shoreline_layers(self): - if self.extract_shorelines_container.geo_data.data != {}: - self.remove_layer_by_name(self.extract_shorelines_container.geo_data.name) - self.extract_shorelines_container.geo_data = GeoJSON(data={}) + # if self.extract_shorelines_container.geo_data.data != {}: + # self.remove_layer_by_name(self.extract_shorelines_container.geo_data.name) + # self.extract_shorelines_container.geo_data = GeoJSON(data={}) def remove_bbox(self): """Remove all the bounding boxes from the map""" @@ -1724,6 +1725,13 @@ def load_extracted_shorelines_to_map(self, row_number: int = 0) -> None: Returns: 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.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() @@ -1732,9 +1740,6 @@ def load_extracted_shorelines_to_map(self, row_number: int = 0) -> None: logger.warning("No ROIs found. Please load ROIs.") raise Exception("No ROIs found. Please load ROIs.") - # Get the extracted shorelines for all ROIs - ids_with_extracted_shorelines = self.rois.get_ids_with_extracted_shorelines() - # Get the available ROI IDs available_ids = self.get_all_roi_ids() @@ -1751,19 +1756,23 @@ def load_extracted_shorelines_to_map(self, row_number: int = 0) -> None: logger.warning("No ROIs found with extracted shorelines.") return - if ids_with_extracted_shorelines is None: - self.id_container.ids = [] - elif not isinstance(ids_with_extracted_shorelines, list): - self.id_container.ids = list(ids_with_extracted_shorelines) - else: - self.id_container.ids = ids_with_extracted_shorelines + # if ids_with_extracted_shorelines is None: + # self.id_container.ids = [] + # elif not isinstance(ids_with_extracted_shorelines, list): + # self.id_container.ids = list(ids_with_extracted_shorelines) + # else: + # self.id_container.ids = ids_with_extracted_shorelines + # load the extracted shorelines for the selected ROI ID + # Load extracted shorelines for the first ROI ID with extracted shorelines - for selected_id in roi_ids_with_extracted_shorelines: - extracted_shorelines = self.rois.get_extracted_shoreline(selected_id) - if extracted_shorelines is not None: - logger.info(f"Extracted shorelines found for ROI {selected_id}") - self.load_extracted_shorelines_on_map(extracted_shorelines, row_number) - break + # select the first ROI ID with extracted shorelines + selected_id = ids_with_extracted_shorelines[0] + print(f"selected_id {selected_id}") + + extracted_shorelines = self.update_loadable_shorelines(selected_id) + self.extract_shorelines_container.trash_list = [] + if extracted_shorelines: + self.load_extracted_shorelines_on_map(extracted_shorelines, row_number) def load_extracted_shorelines_on_map( self, @@ -2143,3 +2152,64 @@ def convert_selected_set_to_geojson( for feature in selected_features ] return selected_shapes + + def on_roi_change( + self, + selected_id: str, + ) -> None: + # remove the old layers + self.remove_extracted_shoreline_layers() + # update the load_list and trash_list + extracted_shorelines = self.update_loadable_shorelines(selected_id) + self.extract_shorelines_container.trash_list = [] + # load the new extracted shorelines onto the map + self.load_extracted_shorelines_on_map(extracted_shorelines, 1) + + 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.roi_ids_list = [] + self.extract_shorelines_container.load_list = [] + logger.warning("No ROIs found with extracted shorelines.") + return [] + + self.id_container.ids = [] + self.extract_shorelines_container.roi_ids_list = [] + self.id_container.ids = list(ids_with_extracted_shorelines) + self.extract_shorelines_container.roi_ids_list = list( + ids_with_extracted_shorelines + ) + return ids_with_extracted_shorelines + + def update_loadable_shorelines(self, selected_id: str): + extracted_shorelines = self.rois.get_extracted_shoreline(selected_id) + logger.info(f"ROI ID {selected_id} extracted shorelines {extracted_shorelines}") + # if extracted shorelines exist, load them onto map, if none exist nothing loads + if hasattr(extracted_shorelines, "gdf"): + if not extracted_shorelines.gdf.empty: + if extracted_shorelines.gdf["date"].dtype == "object": + # If the "date" column is already of string type, concatenate directly + formatted_dates = extracted_shorelines.gdf["date"] + else: + # If the "date" column is not of string type, convert to string with the required format + formatted_dates = extracted_shorelines.gdf["date"].apply( + lambda x: x.strftime("%Y-%m-%d %H:%M:%S") + ) + + self.extract_shorelines_container.load_list = ( + extracted_shorelines.gdf["satname"] + "_" + formatted_dates + ).tolist() + # if not extracted_shorelines.gdf.empty: + # self.extract_shorelines_container.load_list = ( + # extracted_shorelines.gdf["satname"] + # + "_" + # + extracted_shorelines.gdf["date"].apply( + # lambda x: x.strftime("%Y-%m-%d %H:%M:%S") + # ) + # ).tolist() + else: + return None + return extracted_shorelines