Skip to content

Commit

Permalink
refine clear layer logic
Browse files Browse the repository at this point in the history
  • Loading branch information
coolzhao committed Jun 26, 2023
1 parent e92ed05 commit c9548bf
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 23 deletions.
7 changes: 4 additions & 3 deletions __init__.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
#-----------------------------------------------------------
# -----------------------------------------------------------
# Copyright (C) 2023 CryoLab CUHK
#-----------------------------------------------------------
# -----------------------------------------------------------
# from PyQt5.QtWidgets import QAction, QMessageBox
import os
import inspect
from .geo_sam_tool import Geo_SAM

cmd_folder = os.path.split(inspect.getfile(inspect.currentframe()))[0]


def classFactory(iface):
return Geo_SAM(iface, cmd_folder)
return Geo_SAM(iface, cmd_folder)
20 changes: 11 additions & 9 deletions geo_sam_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,16 +129,18 @@ def create_widget_selector(self):
self.reset_prompt_type()
self.dockFirstOpen = False
else:
self.clear_layers()
if self.wdg_sel.radioButton_enable.isChecked():
self.reset_prompt_type()
self.clear_layers(clear_extent=True)
self.enable_disable_edit_mode()
self.show_hide_sam_feature_extent()
# if self.wdg_sel.radioButton_enable.isChecked():
# self.reset_prompt_type()

# add widget to QGIS
self.iface.addDockWidget(Qt.TopDockWidgetArea, self.wdg_sel)

def destruct(self):
'''Destruct actions when closed widget'''
self.clear_layers()
self.clear_layers(clear_extent=True)

def unload(self):
'''Unload actions when plugin is closed'''
Expand All @@ -148,7 +150,7 @@ def unload(self):
# self.wdg_sel.setVisible(False)
self.iface.removeToolBarIcon(self.action)
self.iface.removePluginMenu('&Geo-SAM', self.action)
self._clear_layers()
self.clear_layers(clear_extent=True)

if hasattr(self, "shortcut_tab"):
self.shortcut_tab.disconnect()
Expand Down Expand Up @@ -380,7 +382,7 @@ def load_feature(self):
'''load feature'''
self.feature_dir = self.wdg_sel.QgsFile_feature.filePath()
if self.feature_dir is not None and os.path.exists(self.feature_dir):
self.clear_layers()
self.clear_layers(clear_extent=True)
self._init_feature_related()
# self.load_shp_file()
# self.draw_foreground_point()
Expand All @@ -399,9 +401,9 @@ def _clear_layers(self):
self.polygon.rollback_changes()
self.prompt_history.clear()

def clear_layers(self):
def clear_layers(self, clear_extent: bool = False):
'''Clear all temporary layers (canvas and new sam result) and reset prompt'''
self.clear_canvas_layers_safely(clear_extent=False)
self.clear_canvas_layers_safely(clear_extent=clear_extent)
if hasattr(self, "polygon"):
self.polygon.rollback_changes()
# self.reset_prompt_type()
Expand Down Expand Up @@ -438,7 +440,7 @@ def undo_sam_polygon(self):
return None
last_ids = self.sam_feature_history.pop(-1)
if len(last_ids) == 1:
self.clear_layers()
self.clear_layers(clear_extent=False)
return None
rm_ids = list(range(last_ids[0], last_ids[1]+1))
self.polygon.layer.dataProvider().deleteFeatures(rm_ids)
Expand Down
10 changes: 5 additions & 5 deletions tools/canvasTool.py
Original file line number Diff line number Diff line change
Expand Up @@ -431,11 +431,11 @@ def _init_layer(self,):
self.layer.commitChanges()
else:
iface.messageBar().pushMessage(
"Warring",
"Output Shapefile is not specified."
" Create a new one on memory (polygon_sam)."
" Remember to save it to disk.",
level=Qgis.Warning,
"Note:",
"Output Shapefile is not specified. "
"A temporal layer 'Polygon_sam' is created, "
"remember to save it before quit.",
level=Qgis.Info,
duration=30)
self.layer = QgsVectorLayer('Polygon', 'polygon_sam', 'memory')
# self.layer.setCrs(self.qgis_project.crs())
Expand Down
12 changes: 9 additions & 3 deletions tools/torchgeo_sam.py
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,8 @@ def __getitem__(self, query: Dict[str, Any]) -> Dict[str, Any]:
# bbox may be useful to form the final mask results (geo-info)
sample = {"crs": self.crs, "bbox": bbox, "path": filepath}
if 'img_shape' in tags.keys():
sample['img_shape'] = eval(img_shape) # convert string to python data structure
# convert string to python data structure
sample['img_shape'] = eval(img_shape)
sample['input_shape'] = eval(input_shape)

if self.is_image:
Expand Down Expand Up @@ -564,7 +565,8 @@ def __init__(
"""

self.res = dataset.res
self.dist_roi = ((feature_size*dataset.res/2)**2)*2
# self.dist_roi = ((feature_size*dataset.res/2)**2)*2
self.dist_roi = None
self.q_bbox = None
self.q_path = None
if roi is None:
Expand All @@ -590,7 +592,11 @@ def __init__(
dist_roi_tmp = (center_x_bbox - center_x_roi)**2 + \
(center_y_bbox - center_y_roi)**2
# print(dist_roi_tmp)
if dist_roi_tmp < self.dist_roi:
if idx == 1:
self.dist_roi = dist_roi_tmp
self.q_bbox = bbox
self.q_path = hit.object
elif dist_roi_tmp < self.dist_roi:
self.dist_roi = dist_roi_tmp
self.q_bbox = bbox
self.q_path = hit.object
Expand Down
5 changes: 3 additions & 2 deletions ui/UI.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
from PyQt5 import uic
from qgis.gui import QgsDockWidget
try:
from qgis.PyQt.QtGui import QDockWidget, QWidget
except:
Expand All @@ -9,5 +10,5 @@
selector_path = os.path.join(cwd, "Selector.ui")
encoder_path = os.path.join(cwd, "Encoder.ui")

UI_Selector = uic.loadUi(selector_path)
UI_Encoder = uic.loadUi(encoder_path)
UI_Selector: QgsDockWidget = uic.loadUi(selector_path)
UI_Encoder: QgsDockWidget = uic.loadUi(encoder_path)
2 changes: 1 addition & 1 deletion ui/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from .UI import UI_Selector, UI_Encoder
from .UI import UI_Selector, UI_Encoder

0 comments on commit c9548bf

Please sign in to comment.