GRASS Programmer's Manual  6.4.2(2012)
mcalc_builder.py
Go to the documentation of this file.
00001 """!
00002 @package mcalc_builder.py
00003 
00004 @brief Map calculator, wrapper for r.mapcalc
00005 
00006 Classes:
00007  - MapCalcFrame
00008 
00009 (C) 2008, 2011 by the GRASS Development Team
00010 
00011 This program is free software under the GNU General Public License
00012 (>=v2). Read the file COPYING that comes with GRASS for details.
00013 
00014 @author Michael Barton, Arizona State University
00015 @author Martin Landa <landa.martin gmail.com>
00016 @author Tim Michelsen (load/save expression)
00017 """
00018 
00019 import os
00020 import sys
00021 import time
00022 
00023 import globalvar
00024 import wx
00025 
00026 import grass.script as grass
00027 
00028 import gcmd
00029 import gselect
00030 try:
00031     import subprocess
00032 except:
00033     sys.path.append(os.path.join(globalvar.ETCWXDIR, "compat"))
00034     import subprocess
00035 from preferences import globalSettings as UserSettings
00036 
00037 class MapCalcFrame(wx.Frame):
00038     """!Mapcalc Frame class. Calculator-style window to create and run
00039     r(3).mapcalc statements.
00040     """
00041     def __init__(self, parent, cmd, id = wx.ID_ANY,
00042                  style = wx.DEFAULT_FRAME_STYLE | wx.RESIZE_BORDER, **kwargs):
00043         self.parent = parent
00044         if self.parent:
00045             self.log = self.parent.GetLogWindow()
00046         else:
00047             self.log = None
00048         
00049         # grass command
00050         self.cmd = cmd
00051 
00052         if self.cmd == 'r.mapcalc':
00053             self.rast3d = False
00054             title = _('GRASS GIS Raster Map Calculator')
00055         if self.cmd == 'r3.mapcalc':
00056             self.rast3d = True
00057             title = _('GRASS GIS 3D Raster Map Calculator')
00058             
00059         wx.Frame.__init__(self, parent, id = id, title = title, **kwargs)
00060         self.SetIcon(wx.Icon(os.path.join(globalvar.ETCICONDIR, 'grass.ico'), wx.BITMAP_TYPE_ICO))
00061         
00062         self.panel = wx.Panel(parent = self, id = wx.ID_ANY)
00063         self.CreateStatusBar()
00064         
00065         #
00066         # variables
00067         #
00068         self.heading = _('mapcalc statement')
00069         self.funct_dict = {
00070             'abs(x)':'abs()',
00071             'acos(x)':'acos()',
00072             'asin(x)':'asin()',
00073             'atan(x)':'atan()',
00074             'atan(x,y)':'atan( , )',
00075             'cos(x)':'cos()',
00076             'double(x)':'double()',
00077             'eval([x,y,...,]z)':'eval()',
00078             'exp(x)':'exp()',
00079             'exp(x,y)':'exp( , )',
00080             'float(x)':'float()',
00081             'graph(x,x1,y1[x2,y2..])':'graph( , , )',
00082             'if(x)':'if()',
00083             'if(x,a)':'if( , )',
00084             'if(x,a,b)':'if( , , )',
00085             'if(x,a,b,c)':'if( , , , )',
00086             'int(x)':'if()',
00087             'isnull(x)':'isnull()',
00088             'log(x)':'log(',
00089             'log(x,b)':'log( , )',
00090             'max(x,y[,z...])':'max( , )',
00091             'median(x,y[,z...])':'median( , )',
00092             'min(x,y[,z...])':'min( , )',
00093             'mode(x,y[,z...])':'mode( , )',
00094             'not(x)':'not()',
00095             'pow(x,y)':'pow( , )',
00096             'rand(a,b)':'rand( , )',
00097             'round(x)':'round()',
00098             'sin(x)':'sin()',
00099             'sqrt(x)':'sqrt()',
00100             'tan(x)':'tan()',
00101             'xor(x,y)':'xor( , )',
00102             'row()':'row()',
00103             'col()':'col()',
00104             'x()':'x()',
00105             'y()':'y()',
00106             'ewres()':'ewres()',
00107             'nsres()':'nsres()',
00108             'null()':'null()'
00109             }
00110         
00111         if self.rast3d:
00112             self.funct_dict['z()'] = 'z()'
00113             self.funct_dict['tbres()'] = 'tbres()'
00114             element = 'rast3d'
00115         else:
00116             element = 'cell'
00117         
00118         self.operatorBox = wx.StaticBox(parent = self.panel, id = wx.ID_ANY,
00119                                         label=" %s " % _('Operators'))
00120         self.operandBox = wx.StaticBox(parent = self.panel, id = wx.ID_ANY,
00121                                        label=" %s " % _('Operands'))
00122         self.expressBox = wx.StaticBox(parent = self.panel, id = wx.ID_ANY,
00123                                        label=" %s " % _('Expression'))
00124         
00125         #
00126         # Buttons
00127         #
00128         self.btn_clear = wx.Button(parent = self.panel, id = wx.ID_CLEAR, label = _("Cl&ear"))
00129         self.btn_help = wx.Button(parent = self.panel, id = wx.ID_HELP)
00130         self.btn_run = wx.Button(parent = self.panel, id = wx.ID_ANY, label = _("&Run"))
00131         self.btn_run.SetDefault()
00132         self.btn_close = wx.Button(parent = self.panel, id = wx.ID_CLOSE)
00133         self.btn_save = wx.Button(parent = self.panel, id = wx.ID_SAVE)
00134         self.btn_save.SetToolTipString(_('Save expression to file'))
00135         self.btn_load = wx.Button(parent = self.panel, id = wx.ID_ANY,
00136                                   label = _("&Load"))
00137         self.btn_load.SetToolTipString(_('Load expression from file'))
00138         
00139         self.btn = dict()        
00140         self.btn['pow'] = wx.Button(parent = self.panel, id = wx.ID_ANY, label = "^")
00141         self.btn['pow'].SetToolTipString(_('exponent'))
00142         self.btn['div'] = wx.Button(parent = self.panel, id = wx.ID_ANY, label = "/")
00143         self.btn['div'].SetToolTipString(_('divide'))
00144         self.btn['add'] = wx.Button(parent = self.panel, id = wx.ID_ANY, label = "+")
00145         self.btn['add'].SetToolTipString(_('add'))
00146         self.btn['minus'] = wx.Button(parent = self.panel, id = wx.ID_ANY, label = "-")
00147         self.btn['minus'].SetToolTipString(_('subtract'))
00148         self.btn['mod'] = wx.Button(parent = self.panel, id = wx.ID_ANY, label = "%")
00149         self.btn['mod'].SetToolTipString(_('modulus'))
00150         self.btn['mult'] = wx.Button(parent = self.panel, id = wx.ID_ANY, label = "*")
00151         self.btn['mult'].SetToolTipString(_('multiply'))
00152 
00153         self.btn['parenl'] = wx.Button(parent = self.panel, id = wx.ID_ANY, label = "(") 
00154         self.btn['parenr'] = wx.Button(parent = self.panel, id = wx.ID_ANY, label = ")")
00155         self.btn['lshift'] = wx.Button(parent = self.panel, id = wx.ID_ANY, label = "<<")
00156         self.btn['lshift'].SetToolTipString(_('left shift'))
00157         self.btn['rshift'] = wx.Button(parent = self.panel, id = wx.ID_ANY, label = ">>")
00158         self.btn['rshift'].SetToolTipString(_('right shift'))
00159         self.btn['rshiftu'] = wx.Button(parent = self.panel, id = wx.ID_ANY, label = ">>>")
00160         self.btn['rshiftu'].SetToolTipString(_('right shift (unsigned)'))
00161         self.btn['gt'] = wx.Button(parent = self.panel, id = wx.ID_ANY, label = ">")
00162         self.btn['gt'].SetToolTipString(_('greater than'))
00163         self.btn['gteq'] = wx.Button(parent = self.panel, id = wx.ID_ANY, label = ">=")
00164         self.btn['gteq'].SetToolTipString(_('greater than or equal to'))
00165         self.btn['lt'] = wx.Button(parent = self.panel, id = wx.ID_ANY, label = "<")
00166         self.btn['lt'].SetToolTipString(_('less than'))
00167         self.btn['lteq'] = wx.Button(parent = self.panel, id = wx.ID_ANY, label = "<=")
00168         self.btn['lteq'].SetToolTipString(_('less than or equal to'))
00169         self.btn['eq'] = wx.Button(parent = self.panel, id = wx.ID_ANY, label = "==")
00170         self.btn['eq'].SetToolTipString(_('equal to'))
00171         self.btn['noteq'] = wx.Button(parent = self.panel, id = wx.ID_ANY, label = "!=")
00172         self.btn['noteq'].SetToolTipString(_('not equal to'))
00173 
00174         self.btn['compl'] = wx.Button(parent = self.panel, id = wx.ID_ANY, label = "~")
00175         self.btn['compl'].SetToolTipString(_('one\'s complement'))
00176         self.btn['not'] = wx.Button(parent = self.panel, id = wx.ID_ANY, label = "!")
00177         self.btn['not'].SetToolTipString(_('NOT'))
00178         self.btn['andbit'] = wx.Button(parent = self.panel, id = wx.ID_ANY, label = '&&')
00179         self.btn['andbit'].SetToolTipString(_('bitwise AND'))
00180         self.btn['orbit'] = wx.Button(parent = self.panel, id = wx.ID_ANY, label = "|")
00181         self.btn['orbit'].SetToolTipString(_('bitwise OR'))
00182         self.btn['and'] = wx.Button(parent = self.panel, id = wx.ID_ANY, label = "&&&&")
00183         self.btn['and'].SetToolTipString(_('logical AND'))
00184         self.btn['andnull'] = wx.Button(parent = self.panel, id = wx.ID_ANY, label = "&&&&&&")
00185         self.btn['andnull'].SetToolTipString(_('logical AND (ignores NULLs)'))
00186         self.btn['or'] = wx.Button(parent = self.panel, id = wx.ID_ANY, label = "||")
00187         self.btn['or'].SetToolTipString(_('logical OR'))
00188         self.btn['ornull'] = wx.Button(parent = self.panel, id = wx.ID_ANY, label = "|||")
00189         self.btn['ornull'].SetToolTipString(_('logical OR (ignores NULLs)'))
00190         self.btn['cond'] = wx.Button(parent = self.panel, id = wx.ID_ANY, label = "a ? b : c") 
00191         self.btn['cond'].SetToolTipString(_('conditional'))
00192         
00193         #
00194         # Text area
00195         #
00196         self.text_mcalc = wx.TextCtrl(parent = self.panel, id = wx.ID_ANY, size = (-1, 75),
00197                                       style = wx.TE_MULTILINE)
00198         wx.CallAfter(self.text_mcalc.SetFocus)
00199         
00200         #
00201         # Map and function insertion text and ComboBoxes
00202         self.newmaplabel = wx.StaticText(parent = self.panel, id = wx.ID_ANY)
00203         if self.rast3d:
00204             self.newmaplabel.SetLabel(_('Name for new 3D raster map to create'))
00205         else:
00206             self.newmaplabel.SetLabel(_('Name for new raster map to create'))
00207         self.newmaptxt = wx.TextCtrl(parent = self.panel, id = wx.ID_ANY, size=(250, -1))
00208         self.mapsellabel = wx.StaticText(parent = self.panel, id = wx.ID_ANY)
00209         if self.rast3d:
00210             self.mapsellabel.SetLabel(_('Insert existing 3D raster map'))
00211         else:
00212             self.mapsellabel.SetLabel(_('Insert existing raster map'))
00213         self.mapselect = gselect.Select(parent = self.panel, id = wx.ID_ANY, size = (250, -1),
00214                                         type = element, multiple = False)
00215         self.functlabel = wx.StaticText(parent = self.panel, id = wx.ID_ANY,
00216                                         label = _('Insert mapcalc function'))
00217         self.function = wx.ComboBox(parent = self.panel, id = wx.ID_ANY, 
00218                                     size = (250, -1), choices = sorted(self.funct_dict.keys()),
00219                                     style = wx.CB_DROPDOWN |
00220                                     wx.CB_READONLY | wx.TE_PROCESS_ENTER)
00221         
00222         self.addbox = wx.CheckBox(parent=self.panel,
00223                                   label=_('Add created raster map into layer tree'), style = wx.NO_BORDER)
00224         self.addbox.SetValue(UserSettings.Get(group='cmd', key='addNewLayer', subkey='enabled'))
00225         if not self.parent or self.parent.GetName() != 'LayerManager':
00226             self.addbox.Hide()
00227         
00228         #
00229         # Bindings
00230         #
00231         for btn in self.btn.keys():
00232             self.btn[btn].Bind(wx.EVT_BUTTON, self.AddMark)
00233         
00234         self.btn_close.Bind(wx.EVT_BUTTON, self.OnClose)
00235         self.btn_clear.Bind(wx.EVT_BUTTON, self.OnClear)
00236         self.btn_run.Bind(wx.EVT_BUTTON, self.OnMCalcRun)
00237         self.btn_help.Bind(wx.EVT_BUTTON, self.OnHelp)
00238         self.btn_save.Bind(wx.EVT_BUTTON, self.OnSaveExpression)
00239         self.btn_load.Bind(wx.EVT_BUTTON, self.OnLoadExpression)
00240         
00241         self.mapselect.Bind(wx.EVT_TEXT, self.OnSelect)
00242         self.function.Bind(wx.EVT_COMBOBOX, self._return_funct)
00243         self.function.Bind(wx.EVT_TEXT_ENTER, self.OnSelect)
00244         self.newmaptxt.Bind(wx.EVT_TEXT, self.OnUpdateStatusBar)
00245         self.text_mcalc.Bind(wx.EVT_TEXT, self.OnUpdateStatusBar)
00246 
00247         self._layout()
00248 
00249         self.SetMinSize(self.GetBestSize())
00250     
00251     def _return_funct(self,event):
00252         i = event.GetString()
00253         self._addSomething(self.funct_dict[i])
00254     
00255     def _layout(self):
00256         sizer = wx.BoxSizer(wx.VERTICAL)
00257         
00258         controlSizer = wx.BoxSizer(wx.HORIZONTAL)
00259         operatorSizer = wx.StaticBoxSizer(self.operatorBox, wx.HORIZONTAL)
00260 
00261         buttonSizer1 = wx.GridBagSizer(5, 1)
00262         buttonSizer1.Add(item = self.btn['add'], pos = (0,0))
00263         buttonSizer1.Add(item = self.btn['minus'], pos = (0,1))
00264         buttonSizer1.Add(item = self.btn['mod'], pos = (5,0))
00265         buttonSizer1.Add(item = self.btn['mult'], pos = (1,0))
00266         buttonSizer1.Add(item = self.btn['div'], pos = (1,1))
00267         buttonSizer1.Add(item = self.btn['pow'], pos = (5,1))
00268         buttonSizer1.Add(item = self.btn['gt'], pos = (2,0))
00269         buttonSizer1.Add(item = self.btn['gteq'], pos = (2,1))
00270         buttonSizer1.Add(item = self.btn['eq'], pos = (4,0))
00271         buttonSizer1.Add(item = self.btn['lt'], pos = (3,0))
00272         buttonSizer1.Add(item = self.btn['lteq'], pos = (3,1))
00273         buttonSizer1.Add(item = self.btn['noteq'], pos = (4,1))
00274 
00275         buttonSizer2 = wx.GridBagSizer(5, 1)
00276         buttonSizer2.Add(item = self.btn['and'], pos = (0,0))
00277         buttonSizer2.Add(item = self.btn['andbit'], pos = (1,0))
00278         buttonSizer2.Add(item = self.btn['andnull'], pos = (2,0))
00279         buttonSizer2.Add(item = self.btn['or'], pos = (0,1))
00280         buttonSizer2.Add(item = self.btn['orbit'], pos = (1,1))
00281         buttonSizer2.Add(item = self.btn['ornull'], pos = (2,1))
00282         buttonSizer2.Add(item = self.btn['lshift'], pos = (3,0))
00283         buttonSizer2.Add(item = self.btn['rshift'], pos = (3,1))
00284         buttonSizer2.Add(item = self.btn['rshiftu'], pos = (4,0))
00285         buttonSizer2.Add(item = self.btn['cond'], pos = (5,0))
00286         buttonSizer2.Add(item = self.btn['compl'], pos = (5,1))
00287         buttonSizer2.Add(item = self.btn['not'], pos = (4,1))
00288 
00289         operandSizer = wx.StaticBoxSizer(self.operandBox, wx.HORIZONTAL)
00290         buttonSizer3 = wx.GridBagSizer(7, 1)
00291         buttonSizer3.Add(item = self.newmaplabel, pos = (0,0),
00292                          span = (1, 2), flag = wx.ALIGN_CENTER)
00293         buttonSizer3.Add(item = self.newmaptxt, pos = (1,0),
00294                          span = (1, 2))
00295         buttonSizer3.Add(item = self.functlabel, pos = (2,0),
00296                          span = (1,2), flag = wx.ALIGN_CENTER)
00297         buttonSizer3.Add(item = self.function, pos = (3,0),
00298                          span = (1,2))                         
00299         buttonSizer3.Add(item = self.mapsellabel, pos = (4,0),
00300                          span = (1,2), flag = wx.ALIGN_CENTER)
00301         buttonSizer3.Add(item = self.mapselect, pos = (5,0),
00302                          span = (1,2))
00303         threebutton = wx.GridBagSizer(1, 2)
00304         threebutton.Add(item = self.btn['parenl'], pos = (0,0),
00305                          span = (1,1), flag = wx.ALIGN_LEFT)
00306         threebutton.Add(item = self.btn['parenr'], pos = (0,1),
00307                          span = (1,1), flag = wx.ALIGN_CENTER)
00308         threebutton.Add(item = self.btn_clear, pos = (0,2),
00309                          span = (1,1), flag = wx.ALIGN_RIGHT)
00310         buttonSizer3.Add(item = threebutton, pos = (6,0),
00311                          span = (1,1), flag = wx.ALIGN_CENTER)
00312 
00313         buttonSizer4 = wx.BoxSizer(wx.HORIZONTAL)
00314         buttonSizer4.AddSpacer(10)
00315         buttonSizer4.Add(item = self.btn_load,
00316                          flag = wx.ALL, border = 5)
00317         buttonSizer4.Add(item = self.btn_save,
00318                          flag = wx.ALL, border = 5)                         
00319         buttonSizer4.AddSpacer(30)
00320         buttonSizer4.Add(item = self.btn_help,
00321                          flag = wx.ALL, border = 5)
00322         buttonSizer4.Add(item = self.btn_run,
00323                          flag = wx.ALL, border = 5)
00324         buttonSizer4.Add(item = self.btn_close,
00325                          flag = wx.ALL, border = 5)
00326         
00327         operatorSizer.Add(item = buttonSizer1, proportion = 0,
00328                           flag = wx.ALL | wx.EXPAND, border = 5)
00329         operatorSizer.Add(item = buttonSizer2, proportion = 0,
00330                           flag = wx.TOP | wx.BOTTOM | wx.RIGHT | wx.EXPAND, border = 5)
00331         
00332         operandSizer.Add(item = buttonSizer3, proportion = 0,
00333                          flag = wx.TOP | wx.BOTTOM | wx.RIGHT, border = 5)
00334         
00335         controlSizer.Add(item = operatorSizer, proportion = 1,
00336                          flag = wx.RIGHT | wx.EXPAND, border = 5)
00337         controlSizer.Add(item = operandSizer, proportion = 0,
00338                          flag = wx.EXPAND)
00339 
00340         expressSizer = wx.StaticBoxSizer(self.expressBox, wx.HORIZONTAL)
00341         expressSizer.Add(item = self.text_mcalc, proportion = 1,
00342                          flag = wx.EXPAND)
00343 
00344         sizer.Add(item = controlSizer, proportion = 0,
00345                   flag = wx.EXPAND | wx.ALL,
00346                   border = 5)        
00347         sizer.Add(item = expressSizer, proportion = 1,
00348                   flag = wx.EXPAND | wx.LEFT | wx.RIGHT,
00349                   border = 5)
00350         sizer.Add(item = buttonSizer4, proportion = 0,
00351                   flag = wx.ALIGN_RIGHT | wx.ALL, border = 3)
00352         if self.addbox.IsShown():
00353             sizer.Add(item = self.addbox, proportion = 0,
00354                       flag = wx.LEFT | wx.RIGHT,
00355                       border = 5)
00356         
00357         self.panel.SetAutoLayout(True)
00358         self.panel.SetSizer(sizer)
00359         sizer.Fit(self.panel)
00360         
00361         self.Layout()
00362         
00363     def AddMark(self,event):
00364         """!Sends operators to insertion method
00365         """
00366         if event.GetId() == self.btn['compl'].GetId(): mark = "~"
00367         elif event.GetId() == self.btn['not'].GetId(): mark = "!"
00368         elif event.GetId() == self.btn['pow'].GetId(): mark = "^"
00369         elif event.GetId() == self.btn['div'].GetId(): mark = "/"
00370         elif event.GetId() == self.btn['add'].GetId(): mark = "+"
00371         elif event.GetId() == self.btn['minus'].GetId(): mark = "-"
00372         elif event.GetId() == self.btn['mod'].GetId(): mark = "%"
00373         elif event.GetId() == self.btn['mult'].GetId(): mark = "*"
00374         elif event.GetId() == self.btn['lshift'].GetId(): mark = "<<"
00375         elif event.GetId() == self.btn['rshift'].GetId(): mark = ">>"
00376         elif event.GetId() == self.btn['rshiftu'].GetId(): mark = ">>>"
00377         elif event.GetId() == self.btn['gt'].GetId(): mark = ">"
00378         elif event.GetId() == self.btn['gteq'].GetId(): mark = ">="
00379         elif event.GetId() == self.btn['lt'].GetId(): mark = "<"
00380         elif event.GetId() == self.btn['lteq'].GetId(): mark = "<="
00381         elif event.GetId() == self.btn['eq'].GetId(): mark = "=="
00382         elif event.GetId() == self.btn['noteq'].GetId(): mark = "!="
00383         elif event.GetId() == self.btn['andbit'].GetId(): mark = "&"
00384         elif event.GetId() == self.btn['orbit'].GetId(): mark = "|"
00385         elif event.GetId() == self.btn['or'].GetId(): mark =  "||"
00386         elif event.GetId() == self.btn['ornull'].GetId(): mark = "|||"
00387         elif event.GetId() == self.btn['and'].GetId(): mark = "&&"
00388         elif event.GetId() == self.btn['andnull'].GetId(): mark = "&&&"
00389         elif event.GetId() == self.btn['cond'].GetId(): mark = " ? : "
00390         elif event.GetId() == self.btn['parenl'].GetId(): mark = "("
00391         elif event.GetId() == self.btn['parenr'].GetId(): mark = ")"
00392         self._addSomething(mark)
00393         
00394     def OnSelect(self, event):
00395         """!Gets raster map or function selection and send it to
00396         insertion method
00397         """
00398         item = event.GetString()
00399         self._addSomething(item)
00400 
00401     def OnUpdateStatusBar(self, event):
00402         """!Update statusbar text"""
00403         self.SetStatusText("r.mapcalc '%s = %s'" % (self.newmaptxt.GetValue(),
00404                                                     self.text_mcalc.GetValue()))
00405         event.Skip()
00406         
00407     def _addSomething(self, what):
00408         """!Inserts operators, map names, and functions into text area
00409         """
00410         self.text_mcalc.SetFocus()
00411         mcalcstr  = self.text_mcalc.GetValue()
00412         position  = self.text_mcalc.GetInsertionPoint()
00413         
00414         newmcalcstr = mcalcstr[:position]
00415         
00416         position_offset = 0
00417         try:
00418             if newmcalcstr[-1] != ' ':
00419                 newmcalcstr += ' '
00420                 position_offset += 1
00421         except:
00422             pass
00423         
00424         newmcalcstr += what
00425         position_offset += len(what)
00426         newmcalcstr += ' ' + mcalcstr[position:]
00427         
00428         self.text_mcalc.SetValue(newmcalcstr)
00429         if what == '()':
00430             position_offset -= 1
00431         self.text_mcalc.SetInsertionPoint(position + position_offset)
00432         self.text_mcalc.Update()
00433         
00434     def OnMCalcRun(self,event):
00435         """!Builds and runs r.mapcalc statement
00436         """
00437         name = self.newmaptxt.GetValue().strip()
00438         if not name:
00439             gcmd.GError(parent = self,
00440                         message = _("You must enter the name of a new map to create"))
00441             return
00442         
00443         if not self.text_mcalc.GetValue().strip():
00444             gcmd.GError(parent = self,
00445                         message = _("You must enter a mapcalc statement to create a new map"))
00446             return
00447         
00448         mctxt = self.text_mcalc.GetValue().strip().replace("\n"," ")
00449         mctxt = mctxt.replace(" " , "")
00450         
00451         if self.log:
00452             cmd = [self.cmd, str('%s = %s' % (name, mctxt))]
00453             self.log.RunCmd(cmd, onDone = self.OnDone)
00454             self.parent.Raise()
00455         else:
00456             gcmd.RunCommand(self.cmd,
00457                             "%s=%s" % (name, mctxt))
00458         
00459     def OnDone(self, cmd, returncode):
00460         """!Add create map to the layer tree"""
00461         if not self.addbox.IsChecked():
00462             return
00463         name = self.newmaptxt.GetValue().strip() + '@' + grass.gisenv()['MAPSET']
00464         mapTree = self.parent.GetLayerTree()
00465         if not mapTree.GetMap().GetListOfLayers(l_name = name):
00466             mapTree.AddLayer(ltype = 'raster',
00467                              lname = name,
00468                              lcmd = ['d.rast', 'map=%s' % name],
00469                              multiple = False)
00470         
00471         display = self.parent.GetLayerTree().GetMapDisplay()
00472         if display and display.IsAutoRendered():
00473             display.GetWindow().UpdateMap(render = True)
00474         
00475     def OnSaveExpression(self, event):
00476         """!Saves expression to file
00477         """
00478         mctxt = self.newmaptxt.GetValue() + ' = ' + self.text_mcalc.GetValue() + os.linesep
00479         
00480         #dialog
00481         dlg = wx.FileDialog(parent = self,
00482                             message = _("Choose a file name to save the expression"),
00483                             wildcard = _("Expression file (*)|*"),
00484                             style = wx.SAVE | wx.FD_OVERWRITE_PROMPT)
00485         if dlg.ShowModal() == wx.ID_OK:
00486             path = dlg.GetPath()
00487             if not path:
00488                 dlg.Destroy()
00489                 return
00490             
00491             try:
00492                 fobj = open(path, 'w')
00493                 fobj.write(mctxt)
00494             finally:
00495                 fobj.close()
00496         
00497         dlg.Destroy()
00498 
00499     def OnLoadExpression(self, event):
00500         """!Load expression from file
00501         """
00502         dlg = wx.FileDialog(parent = self,
00503                             message = _("Choose a file name to load the expression"),
00504                             wildcard = _("Expression file (*)|*"),
00505                             style = wx.OPEN)
00506         if dlg.ShowModal() == wx.ID_OK:
00507             path = dlg.GetPath()
00508             if not path:
00509                 dlg.Destroy()
00510                 return
00511 
00512             try:
00513                 fobj = open(path,'r')
00514                 mctxt = fobj.read()
00515             finally:
00516                 fobj.close()
00517             
00518             try:
00519                 result, exp = mctxt.split('=', 1)
00520             except ValueError:
00521                 result = ''
00522                 exp = mctxt
00523             
00524             self.newmaptxt.SetValue(result.strip())
00525             self.text_mcalc.SetValue(exp.strip())
00526             self.text_mcalc.SetFocus()
00527             self.text_mcalc.SetInsertionPointEnd()
00528         
00529         dlg.Destroy()
00530                 
00531     def OnClear(self, event):
00532         """!Clears text area
00533         """
00534         self.text_mcalc.SetValue('')
00535         
00536     def OnHelp(self, event):
00537         """!Launches r.mapcalc help
00538         """
00539         gcmd.RunCommand('g.manual', parent = self, entry = self.cmd)
00540         
00541     def OnClose(self,event):
00542         """!Close window"""
00543         self.Destroy()
00544 
00545 if __name__ == "__main__":
00546     app = wx.App(0)
00547     frame = MapCalcFrame(parent = None, cmd = 'r.mapcalc')
00548     frame.Show()
00549     app.MainLoop()
00550 
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines