1919"""
2020
2121from datetime import datetime
22- from pathlib import Path
22+ import json
2323
2424import wx
2525import wx .lib .scrolledpanel as SP
2929from icons .icon import MetaIcon
3030
3131import grass .script as gs
32+ from grass .tools import Tools
3233
3334from grass .grassdb import history
3435
5253 "rows" : _ ("Number of rows:" ),
5354 "cols" : _ ("Number of columns:" ),
5455 "cells" : _ ("Number of cells:" ),
56+ "t" : _ ("Top:" ),
57+ "b" : _ ("Bottom:" ),
58+ "tbres" : _ ("Top-bottom resolution:" ),
59+ "nsres3" : _ ("3D north-south resolution:" ),
60+ "ewres3" : _ ("3D east-west resolution:" ),
61+ "rows3" : _ ("Number of 3D rows:" ),
62+ "cols3" : _ ("Number of 3D cols:" ),
63+ "depths" : _ ("Number of depths:" ),
5564}
5665
5766
@@ -81,6 +90,7 @@ def __init__(self, parent, giface, title=("Command Info"), style=wx.TAB_TRAVERSA
8190 self .title = title
8291
8392 self .region_settings = None
93+ self .tools = Tools ()
8494
8595 self ._initImages ()
8696
@@ -141,7 +151,13 @@ def _createRegionSettingsBox(self):
141151 self .sizer_region_settings = wx .StaticBoxSizer (
142152 self .region_settings_box , wx .VERTICAL
143153 )
144-
154+ self .sizer_region_name = wx .BoxSizer (wx .HORIZONTAL )
155+ self .sizer_region_settings .Add (
156+ self .sizer_region_name ,
157+ proportion = 0 ,
158+ flag = wx .ALL | wx .EXPAND ,
159+ border = 5 ,
160+ )
145161 self .sizer_region_settings_match = wx .BoxSizer (wx .HORIZONTAL )
146162 self .sizer_region_settings .Add (
147163 self .sizer_region_settings_match ,
@@ -169,7 +185,7 @@ def _general_info_filter(self, key, value):
169185 return key in filter_keys or ((key in {"mask2d" , "mask3d" }) and value is True )
170186
171187 def _region_settings_filter (self , key ):
172- return key not in {"projection" , "zone" , "cells" }
188+ return key not in {"projection" , "zone" , "cells" , "cells3" }
173189
174190 def _updateGeneralInfoBox (self , command_info ):
175191 """Update a static box for displaying general info about the command.
@@ -247,25 +263,22 @@ def _updateRegionSettingsGrid(self, command_info):
247263 self .region_settings_box .Show ()
248264
249265 def _get_saved_region_name (self , history_region ):
250- """Find if history region matches any saved region in MAPSET/windows directory."""
251- env = gs .gisenv ()
252- windows_dir = (
253- Path (env ["GISDBASE" ]) / env ["LOCATION_NAME" ] / env ["MAPSET" ] / "windows"
254- )
255-
256- if not windows_dir .exists ():
266+ """Find if history region matches any saved region in the current mapset."""
267+ try :
268+ res = self .tools .g_list (type = "region" , mapset = "." , format = "json" )
269+ regions = json .loads (res .stdout )
270+ region_names = [r ["name" ] for r in regions ]
271+ except Exception :
257272 return None
258273
259- for region_file in windows_dir .iterdir ():
260- if not region_file .is_file ():
274+ for region_name in region_names :
275+ region_name = region_name .strip ()
276+ if not region_name :
261277 continue
262-
263- region_name = region_file .name
264-
265278 try :
266- saved_region = gs . parse_command (
267- "g. region" , region = region_name , flags = "g "
268- )
279+ saved_region = self . tools . g_region (
280+ region = region_name , flags = "u3" , format = "shell "
281+ ). keyval
269282
270283 if self ._compare_regions (history_region , saved_region ):
271284 return region_name
@@ -276,39 +289,39 @@ def _get_saved_region_name(self, history_region):
276289
277290 def _compare_regions (self , r1 , r2 ):
278291 """Compare two region dictionaries for equality."""
279- keys = [ "n " , "s " , "e " , "w" , "nsres" , "ewres" , "rows" , "cols" ]
292+ skip_keys = { "projection " , "zone " , "cells " , "cells3" }
280293
281- for k in keys :
294+ for k , v in r1 .items ():
295+ if k in skip_keys :
296+ continue
282297 try :
283- v1 = float (r1 .get (k , 0 ))
284- v2 = float (r2 .get (k , 0 ))
285- if abs (v1 - v2 ) > 1e-8 :
298+ if abs (float (v ) - float (r2 [k ])) > 1e-8 :
286299 return False
287- except (ValueError , TypeError ):
300+ except (KeyError , ValueError , TypeError ):
288301 return False
289302 return True
290303
291304 def _updateRegionSettingsMatch (self ):
292305 """Update text, icon and button dealing with region update"""
306+ self .sizer_region_name .Clear (True )
293307 self .sizer_region_settings_match .Clear (True )
294308
295309 # Region condition
296310 history_region = self .region_settings
297311 current_region = self ._get_current_region ()
298- region_matches = history_region == current_region
312+ region_matches = self . _compare_regions ( history_region , current_region )
299313 saved_name = self ._get_saved_region_name (history_region )
300314
301315 region_label = saved_name or _ ("Not set" )
302316
303- self .sizer_region_settings_match .Add (
317+ self .sizer_region_name .Add (
304318 StaticText (
305319 parent = self .region_settings_box ,
306320 id = wx .ID_ANY ,
307321 label = _ ("Region name: {}" ).format (region_label ),
308322 ),
309323 proportion = 0 ,
310- flag = wx .ALIGN_CENTER_VERTICAL | wx .RIGHT ,
311- border = 15 ,
324+ flag = wx .ALIGN_CENTER_VERTICAL ,
312325 )
313326
314327 # Icon and button according to the condition
@@ -379,14 +392,15 @@ def hideCommandInfo(self):
379392
380393 def _get_current_region (self ):
381394 """Get current computational region settings."""
382- return gs .region ()
395+ return gs .region (region3d = True )
383396
384397 def _get_history_region (self ):
385398 """Get computational region settings of executed command."""
386399 return {
387400 key : value
388401 for key , value in self .region_settings .items ()
389402 if self ._region_settings_filter (key )
403+ and key not in {"rows3" , "cols3" , "depths" }
390404 }
391405
392406 def OnUpdateRegion (self , event ):
0 commit comments