File: frame.py

package info (click to toggle)
grass 8.4.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 277,040 kB
  • sloc: ansic: 460,798; python: 227,732; cpp: 42,026; sh: 11,262; makefile: 7,007; xml: 3,637; sql: 968; lex: 520; javascript: 484; yacc: 450; asm: 387; perl: 157; sed: 25; objc: 6; ruby: 4
file content (440 lines) | stat: -rw-r--r-- 13,634 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
"""!
@package example.frame

@brief Example tool for displaying raster map and related information

Classes:
 - frame::ExampleMapPanel
 - frame::ExampleMapDisplay
 - frame::ExampleInfoTextManager

(C) 2011-2014 by the GRASS Development Team
This program is free software under the GNU General Public
License (>=v2). Read the file COPYING that comes with GRASS
for details.

@author Anna Petrasova <kratochanna gmail.com>
"""

import os
import sys
import wx

# this enables to run application standalone (> python example/frame.py )
if __name__ == "__main__":
    sys.path.append(os.path.join(os.environ["GISBASE"], "etc", "gui", "wxpython"))

# i18n is taken care of in the grass library code.
# So we need to import it before any of the GUI code.
from grass.script import core as gcore

from gui_core.mapdisp import SingleMapPanel, FrameMixin
from mapwin.buffered import BufferedMapWindow
from mapwin.base import MapWindowProperties
from mapdisp import statusbar as sb
from core.render import Map
from core.debug import Debug
from core.gcmd import RunCommand, GError
from core import globalvar

from example.toolbars import ExampleMapToolbar, ExampleMiscToolbar, ExampleMainToolbar
from example.dialogs import ExampleMapDialog

# It is possible to call grass library functions (in C) directly via ctypes
# however this is less stable. Example is available in doc/examples/python/,
# ctypes are used in nviz, vdigit, iclass gui modules.

# from ctypes import *
# try:
#     from grass.lib.raster import *
#     haveExample = True
#     errMsg = ''
# except ImportError as e:
#     haveExample = False
#     errMsg = _("Loading raster lib failed.\n%s") % e


class ExampleMapPanel(SingleMapPanel):
    """! Main panel of example tool.

    Inherits from SingleMapPanel, so map is displayed in one map widow.
    In case two map windows are needed, use DoubleMapPanel from (gui_core.mapdisp).

    @see IClassMapPanel in iclass.frame
    """

    def __init__(
        self,
        parent,
        giface,
        title=_("Example Tool"),
        toolbars=["MiscToolbar", "MapToolbar", "MainToolbar"],
        size=(800, 600),
        name="exampleWindow",
        **kwargs,
    ):
        """!Map Panel constructor

        @param parent (no parent is expected)
        @param title window title
        @param toolbars list of active toolbars (default value represents all toolbars)
        """
        SingleMapPanel.__init__(
            self, parent=parent, title=title, name=name, Map=Map(), **kwargs
        )

        # Place debug message where appropriate
        # and set debug level from 1 to 5 (higher to lower level functions).
        # To enable debug mode write:
        # > g.gisenv set=WX_DEBUG=5
        Debug.msg(1, "ExampleMapPanel.__init__()")

        #
        # Add toolbars to aui manager
        #
        toolbarsCopy = toolbars[:]
        # workaround to have the same toolbar order on all platforms
        if sys.platform == "win32":
            toolbarsCopy.reverse()

        for toolbar in toolbarsCopy:
            self.AddToolbar(toolbar)

        self.mapWindowProperties = MapWindowProperties()
        self.mapWindowProperties.setValuesFromUserSettings()
        self.mapWindowProperties.autoRenderChanged.connect(
            lambda value: self.OnRender(None) if value else None
        )
        #
        # Add statusbar
        #

        # choose items in statusbar choice, which makes sense for your application
        statusbarItems = [
            sb.SbCoordinates,
            sb.SbRegionExtent,
            sb.SbCompRegionExtent,
            sb.SbDisplayGeometry,
            sb.SbMapScale,
            sb.SbGoTo,
        ]
        self.statusbar = self.CreateStatusbar(statusbarItems)

        # create map window
        self.MapWindow = BufferedMapWindow(
            parent=self,
            Map=self.GetMap(),
            properties=self.mapWindowProperties,
            giface=self,
        )
        self._setUpMapWindow(self.MapWindow)
        self.MapWindow.InitZoomHistory()

        # create whatever you want, here it is a widget for displaying raster info
        self.info = ExampleInfoTextManager(self)

        # add map window (and other widgets) to aui manager
        self._addPanes()
        self._mgr.Update()

        # initialize variables related to your application functionality
        self.InitVariables()

        # default action
        self.GetMapToolbar().SelectDefault()

        self.Bind(wx.EVT_SIZE, self.OnSize)

        self.SetSize(size)

    def __del__(self):
        """!Destructor deletes temporary region"""
        gcore.del_temp_region()

    def OnCloseWindow(self, event):
        """!Destroy panel"""
        self._mgr.UnInit()
        self.Destroy()

    def InitVariables(self):
        """!Initialize any variables nneded by application"""
        self.currentRaster = None

        # use WIND_OVERRIDE region not to affect current region
        gcore.use_temp_region()

    def _addPanes(self):
        """!Add mapwindow (and other widgets) to aui manager"""
        window = self.GetWindow()
        name = "mainWindow"
        self._mgr.AddPane(
            window,
            wx.aui.AuiPaneInfo()
            .Name(name)
            .CentrePane()
            .Dockable(False)
            .CloseButton(False)
            .DestroyOnClose(True)
            .Layer(0),
        )

        window = self.info.GetControl()
        name = "infoText"
        self._mgr.AddPane(
            window,
            wx.aui.AuiPaneInfo()
            .Name(name)
            .Caption(_("Raster Info"))
            .MinSize((250, -1))
            .Dockable(True)
            .CloseButton(False)
            .Layer(0)
            .Left(),
        )

        # statusbar
        self.AddStatusbarPane()

    def AddToolbar(self, name):
        """!Add defined toolbar to the window

        Currently known toolbars are:
         - 'ExampleMapToolbar'        - basic map toolbar
         - 'ExampleMainToolbar'       - toolbar with application specific tools
         - 'ExampleMiscToolbar'       - toolbar with common tools (help, quit, ...)
        """
        # see wx.aui.AuiPaneInfo documentation for understanding all options
        if name == "MapToolbar":
            if "MapToolbar" not in self.toolbars:
                self.toolbars[name] = ExampleMapToolbar(self, self._toolSwitcher)

            self._mgr.AddPane(
                self.toolbars[name],
                wx.aui.AuiPaneInfo()
                .Name(name)
                .Caption(_("Map Toolbar"))
                .ToolbarPane()
                .Top()
                .LeftDockable(False)
                .RightDockable(False)
                .BottomDockable(False)
                .TopDockable(True)
                .CloseButton(False)
                .Layer(1)
                .Row(1)
                .BestSize(self.toolbars[name].GetBestSize()),
            )

        if name == "MiscToolbar":
            if "MiscToolbar" not in self.toolbars:
                self.toolbars[name] = ExampleMiscToolbar(self)

            self._mgr.AddPane(
                self.toolbars[name],
                wx.aui.AuiPaneInfo()
                .Name(name)
                .Caption(_("Misc Toolbar"))
                .ToolbarPane()
                .Top()
                .LeftDockable(False)
                .RightDockable(False)
                .BottomDockable(False)
                .TopDockable(True)
                .CloseButton(False)
                .Layer(1)
                .Row(1)
                .BestSize(self.toolbars[name].GetBestSize()),
            )

        if name == "MainToolbar":
            if "MainToolbar" not in self.toolbars:
                self.toolbars[name] = ExampleMainToolbar(self)

            self._mgr.AddPane(
                self.toolbars[name],
                wx.aui.AuiPaneInfo()
                .Name(name)
                .Caption(_("Main Toolbar"))
                .ToolbarPane()
                .Top()
                .LeftDockable(False)
                .RightDockable(False)
                .BottomDockable(False)
                .TopDockable(True)
                .CloseButton(False)
                .Layer(1)
                .Row(1)
                .BestSize(self.toolbars[name].GetBestSize()),
            )

    def GetMapToolbar(self):
        """!Returns toolbar with zooming tools"""
        return self.toolbars["MapToolbar"]

    def OnHelp(self, event):
        """!Show help page"""
        RunCommand("g.manual", entry="wxGUI.Example")

    def OnSelectRaster(self, event):
        """!Opens dialog to select raster map"""
        dlg = ExampleMapDialog(self)

        if dlg.ShowModal() == wx.ID_OK:
            raster = gcore.find_file(name=dlg.GetRasterMap(), element="cell")
            if raster["fullname"]:
                self.SetLayer(name=raster["fullname"])
            else:
                # show user that the map name is incorrect
                GError(
                    parent=self,
                    message=_("Raster map <{raster}> not found").format(
                        raster=dlg.GetRasterMap()
                    ),
                )

        dlg.Destroy()

    def SetLayer(self, name):
        """!Sets layer in Map and updates statistics.

        @param name layer (raster) name
        """
        Debug.msg(3, "ExampleMapPanel.SetLayer(): name=%s" % name)

        # this simple application enables to keep only one raster
        self.GetMap().DeleteAllLayers()
        cmdlist = ["d.rast", "map=%s" % name]
        # add layer to Map instance (core.render)
        newLayer = self.GetMap().AddLayer(
            ltype="raster",
            command=cmdlist,
            active=True,
            name=name,
            hidden=False,
            opacity=1.0,
            render=True,
        )
        self.GetWindow().ZoomToMap(
            layers=[
                newLayer,
            ],
            render=True,
        )
        self.currentRaster = name

        # change comp. region to match new raster, so that the statistics
        # are computed for the entire raster
        RunCommand("g.region", rast=self.currentRaster, parent=self)

        self.UpdateStatistics()

    def ComputeStatistics(self):
        """!Computes statistics for raster map using 'r.univar' module.

        @return statistic in form of dictionary
        """
        # RunCommand enables to run GRASS module
        res = RunCommand(
            "r.univar",  # module name
            flags="g",  # command flags
            map=self.currentRaster,  # module parameters
            read=True,
        )  # get command output

        return gcore.parse_key_val(res, val_type=float)

    def UpdateStatistics(self):
        """!Update statistic information.

        Called after changing raster map.
        """
        stats = self.ComputeStatistics()
        self.info.WriteStatistics(name=self.currentRaster, statDict=stats)


class ExampleMapDisplay(FrameMixin, ExampleMapPanel):
    """Map display for wrapping map panel with frame methods"""

    def __init__(self, parent, giface, **kwargs):
        # init map panel
        ExampleMapPanel.__init__(
            self,
            parent=parent,
            giface=giface,
            **kwargs,
        )
        # set system icon
        parent.SetIcon(
            wx.Icon(
                os.path.join(globalvar.ICONDIR, "grass_map.ico"), wx.BITMAP_TYPE_ICO
            )
        )

        # bindings
        parent.Bind(wx.EVT_CLOSE, self.OnCloseWindow)

        # extend shortcuts and create frame accelerator table
        self.shortcuts_table.append((self.OnFullScreen, wx.ACCEL_NORMAL, wx.WXK_F11))
        self._initShortcuts()

        # add Map Display panel to Map Display frame
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self, proportion=1, flag=wx.EXPAND)
        parent.SetSizer(sizer)
        parent.Layout()


class ExampleInfoTextManager:
    """!Class for displaying information.

    Wrraper for wx.TextCtrl.
    """

    def __init__(self, parent):
        """!Creates wx.TextCtrl for displaying information."""
        self.textCtrl = wx.TextCtrl(
            parent, id=wx.ID_ANY, style=wx.TE_MULTILINE | wx.TE_RICH2 | wx.TE_READONLY
        )
        self.textCtrl.SetInsertionPoint(0)
        self.font = self.textCtrl.GetFont()

    def GetControl(self):
        """!Returns control itself."""
        return self.textCtrl

    def _setStyle(self, style):
        """!Sets default style of textCtrl.

        @param style "title"/"value"
        """
        if style == "title":
            self.font.SetWeight(wx.FONTWEIGHT_BOLD)
        elif style == "value":
            self.font.SetWeight(wx.FONTWEIGHT_NORMAL)
        else:
            return

        self.textCtrl.SetFont(self.font)

    def _writeLine(self, title, value):
        """!Formats text (key, value pair) with styles."""
        self._setStyle("title")
        self.textCtrl.AppendText("%s: " % title)
        self._setStyle("value")
        self.textCtrl.AppendText("%.2f\n" % value)

    def _writeRasterTitle(self, name):
        """!Writes title."""
        self._setStyle("title")
        self.textCtrl.AppendText("%s\n\n" % name)

    def WriteStatistics(self, name, statDict):
        """!Write and format information about raster map

        @param name raster map name
        @param statDict dictionary containing information
        """
        self.GetControl().Clear()
        self._writeRasterTitle(name=name)
        for key, value in statDict.items():
            self._writeLine(title=key, value=value)