Skip to content

Commit

Permalink
wxGUI: Use r.mask.status to get info about mask (#4519)
Browse files Browse the repository at this point in the history
This replaces low level checks of MASK raster in the current mapset with calls of r.mask.status.

GConsole and SbMask require r.mask.status to output a potential name of the mask regerdless of mask being set or not, so this functionality still needs to be implemented first before moving this PR forward. History changes seem to be complete.
  • Loading branch information
wenzeslaus authored Jan 29, 2025
1 parent 3650646 commit 6a32875
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 17 deletions.
6 changes: 4 additions & 2 deletions gui/wxpython/core/gconsole.py
Original file line number Diff line number Diff line change
Expand Up @@ -856,13 +856,15 @@ def OnCmdDone(self, event):
for p in task.get_options()["flags"]:
if p.get("name") == "r" and p.get("value"):
action = "delete"
mask_full_name = gs.parse_command("r.mask.status", format="json")["name"]
mask_name, mask_mapset = mask_full_name.split("@", maxsplit=1)
gisenv = gs.gisenv()
self._giface.grassdbChanged.emit(
grassdb=gisenv["GISDBASE"],
location=gisenv["LOCATION_NAME"],
mapset=gisenv["MAPSET"],
mapset=mask_mapset,
action=action,
map="MASK",
map=mask_name,
element="raster",
)

Expand Down
26 changes: 15 additions & 11 deletions gui/wxpython/lmgr/statusbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,14 @@ class SbMask:

def __init__(self, parent, giface):
self.name = "mask"
self.mask_layer = "MASK"
self.parent = parent
self.giface = giface
self.widget = Button(
parent=parent, id=wx.ID_ANY, label=_(self.mask_layer), style=wx.NO_BORDER
parent=parent, id=wx.ID_ANY, label=_("Mask"), style=wx.NO_BORDER
)
self.widget.Bind(wx.EVT_BUTTON, self.OnRemoveMask)
self.widget.SetForegroundColour(wx.Colour(255, 0, 0))
self.widget.SetToolTip(tip=_("Left mouse click to remove the MASK"))
self.widget.SetToolTip(tip=_("Left mouse click to remove the raster mask"))
self.giface.currentMapsetChanged.connect(self.Refresh)
if not watchdog_used:
self.giface.grassdbChanged.connect(self.dbChanged)
Expand All @@ -94,7 +93,12 @@ def dbChanged(self, map=None, newname=None):
:param str map: map that is changed
:param str newname: new map
"""
if self.mask_layer in {map, newname}:
mask_layer = gs.parse_command("r.mask.status", format="json")["name"].split(
"@", maxsplit=1
)[0]
# This assumes mask is always in the current mapset (or the event is triggered
# only for the current mapset).
if mask_layer in {map, newname}:
self.Refresh()
self.giface.updateMap.emit()

Expand Down Expand Up @@ -124,30 +128,30 @@ def GetWidget(self):

def Refresh(self):
"""Show mask in the statusbar if mask file found"""
if gs.find_file(
name=self.mask_layer, element="cell", mapset=gs.gisenv()["MAPSET"]
)["name"]:
if gs.parse_command("r.mask.status", format="json")["present"]:
self.Show()
else:
self.Hide()

def OnRemoveMask(self, event):
dlg = wx.MessageDialog(
self.parent,
message=_("Are you sure that you want to remove the MASK?"),
caption=_("Remove MASK"),
message=_("Are you sure that you want to remove the current raster mask?"),
caption=_("Remove raster mask"),
style=wx.YES_NO | wx.YES_DEFAULT | wx.ICON_QUESTION,
)
if dlg.ShowModal() != wx.ID_YES:
dlg.Destroy()
return
RunCommand("r.mask", flags="r")
mask_full_name = gs.parse_command("r.mask.status", format="json")["name"]
mask_name, mask_mapset = mask_full_name.split("@", maxsplit=1)
gisenv = gs.gisenv()
self.giface.grassdbChanged.emit(
grassdb=gisenv["GISDBASE"],
location=gisenv["LOCATION_NAME"],
mapset=gisenv["MAPSET"],
map=self.mask_layer,
mapset=mask_mapset,
map=mask_name,
action="delete",
element="raster",
)
11 changes: 7 additions & 4 deletions python/grass/grassdb/history.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,21 +258,24 @@ def get_initial_command_info(env_run):
exec_time = datetime.now().isoformat()

# 2D raster MASK presence
env = gs.gisenv(env_run)
mapset_path = Path(env["GISDBASE"]) / env["LOCATION_NAME"] / env["MAPSET"]
mask2d_present = (mapset_path / "cell" / "MASK").exists()
mask2d_status = gs.parse_command("r.mask.status", format="json", env=env_run)

# 3D raster MASK presence
env = gs.gisenv(env_run)
mapset_path = Path(env["GISDBASE"]) / env["LOCATION_NAME"] / env["MAPSET"]
mask3d_present = (mapset_path / "grid3" / "RASTER3D_MASK").exists()
mask3d_name = f"RASTER3D_MASK@{env['MAPSET']}"

# Computational region settings
region_settings = gs.region(env=env_run)

# Finalize the command info dictionary
return {
"timestamp": exec_time,
"mask2d": mask2d_present,
"mask2d": mask2d_status["present"],
"mask2d_name": mask2d_status["name"],
"mask3d": mask3d_present,
"mask3d_name": mask3d_name,
"region": region_settings,
"status": Status.RUNNING.value,
}
Expand Down

0 comments on commit 6a32875

Please sign in to comment.