Skip to content

Commit

Permalink
#198 add delete_extracted_shorelines_files, delete_selected_shorelines
Browse files Browse the repository at this point in the history
  • Loading branch information
2320sharon committed Dec 15, 2023
1 parent acf8393 commit 4385994
Showing 1 changed file with 106 additions and 1 deletion.
107 changes: 106 additions & 1 deletion src/coastseg/coastseg_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,91 @@
__all__ = ["IDContainer", "ExtractShorelinesContainer", "CoastSeg_Map"]



def delete_extracted_shorelines_files(session_path: str, selected_items: List):
"""Delete the extracted shorelines from the session directory for all the relevant files
Removes the extracted shorelines from the following files:
- extracted_shorelines_lines.geojson
- extracted_shorelines_points.geojson
- extracted_shorelines_dict.json
- transect_time_series.csv
- transect_time_series_tidally_corrected.csv
As well as all the csv files matching the following patterns
- _timeseries_raw.csv
- _timeseries_tidally_corrected.csv
"""
# delete the extracted shorelines from both geojson files
geodata_processing.edit_geojson_files(
session_path,
[
"extracted_shorelines_lines.geojson",
"extracted_shorelines_points.geojson",
],
selected_items,
common.remove_rows,
)
# delete the extracted shorelines from the extracted_shorelines_dict.json file
filename = "extracted_shorelines_dict.json"
# Extract dates and satellite names from the selected items
dates_list, sat_list = common.extract_dates_and_sats(selected_items)
# update extracted_shorelines_dict.json and transects_cross_distances.json
common.update_extracted_shorelines_dict_transects_dict(
session_path, filename, dates_list, sat_list
)
# delete the extracted shorelines from the transect_time_series.csv files
filenames = [
"transect_time_series.csv",
"transect_time_series_tidally_corrected.csv",
]
filepaths = [
os.path.join(session_path, filename)
for filename in filenames
if os.path.isfile(os.path.join(session_path, filename))
]
dates_list, sat_list = common.extract_dates_and_sats(selected_items)
common.update_transect_time_series(filepaths, dates_list)
# delete the selected shorelines from all the individual csv files
file_patterns = ["_timeseries_tidally_corrected", "_timeseries_raw.csv"]
for file_pattern in file_patterns:
common.drop_rows_from_csv(file_pattern, session_path, dates_list)
# delete the extracted shorelines from the jpg detection files
jpg_path = os.path.join(session_path, "jpg_files", "detection")
if os.path.exists(jpg_path) and os.path.isdir(jpg_path):
common.delete_jpg_files(dates_list, sat_list, jpg_path)

def find_shorelines_directory(path, roi_id):
# List the contents of the specified path
contents = os.listdir(path)

# Check for extracted shorelines geojson file in the specified path
extracted_shorelines_file = [
file
for file in contents
if "extracted_shorelines" in file and file.endswith(".geojson")
]
if extracted_shorelines_file:
return path

# If the file is not found, check for a directory with the ROI ID
roi_directory = [
directory
for directory in contents
if os.path.isdir(os.path.join(path, directory)) and roi_id in directory
]
if roi_directory:
roi_path = os.path.join(path, roi_directory[0])
roi_contents = os.listdir(roi_path)
extracted_shorelines_file = [
file
for file in roi_contents
if "extracted_shorelines" in file and file.endswith(".geojson")
]
if extracted_shorelines_file:
return roi_path

return None


class IDContainer(traitlets.HasTraits):
ids = traitlets.List(trait=traitlets.Unicode())

Expand Down Expand Up @@ -2267,4 +2352,24 @@ def get_selected_shorelines(gdf, selected_items) -> gpd.GeoDataFrame:
f"load_selected_shorelines_on_map: selected_gdf.head() {selected_gdf.head()}"
)
if not selected_gdf.empty:
self.load_extracted_shoreline_layer(selected_gdf, layer_name, colormap)
self.load_extracted_shoreline_layer(selected_gdf, layer_name, colormap)


def delete_selected_shorelines(
self, layer_name: str, selected_id: str, selected_shorelines: List = None
) -> None:
if selected_shorelines and selected_id:
self.map.default_style = {"cursor": "wait"}
session_name = self.get_session_name()
session_path = file_utilities.get_session_location(
session_name=session_name, raise_error=True
)
# get the path to the session directory that contains the extracted shoreline files
session_path = find_shorelines_directory(session_path, selected_id)
if os.path.exists(session_path) and os.path.isdir(session_path):
delete_extracted_shorelines_files(
session_path, list(selected_shorelines)
)
# this will remove the selected shorelines from the files
self.remove_layer_by_name(layer_name)
self.map.default_style = {"cursor": "default"}

0 comments on commit 4385994

Please sign in to comment.