[GRASS-SVN] r62931 - grass/trunk/gui/wxpython/rlisetup

svn_grass at osgeo.org svn_grass at osgeo.org
Tue Nov 25 03:34:46 PST 2014


Author: lucadelu
Date: 2014-11-25 03:34:46 -0800 (Tue, 25 Nov 2014)
New Revision: 62931

Modified:
   grass/trunk/gui/wxpython/rlisetup/functions.py
   grass/trunk/gui/wxpython/rlisetup/wizard.py
Log:
g.gui.rlisetup: improved tool: check temporal files already exist, add capability to choose the layer number; some PEP8 cleanup

Modified: grass/trunk/gui/wxpython/rlisetup/functions.py
===================================================================
--- grass/trunk/gui/wxpython/rlisetup/functions.py	2014-11-25 11:24:38 UTC (rev 62930)
+++ grass/trunk/gui/wxpython/rlisetup/functions.py	2014-11-25 11:34:46 UTC (rev 62931)
@@ -10,6 +10,7 @@
 from grass.script import core as grass
 from core.gcmd import GError
 
+
 class SamplingType:
     """"
     KMVWINC = samplingtype=moving, regionbox=keyboard, shape=circle
@@ -80,21 +81,21 @@
 def convertFeature(vect, outrast, cat, origrast, layer='1', overwrite=False):
     """Convert a single feature to a raster"""
     tmp_vect = "tmp_{rast}".format(rast=outrast)
-    grass.run_command('v.extract', input=vect, cats=cat, type='area', 
-                      layer=layer, output=tmp_vect, flags='d', 
-		      overwrite=overwrite, quiet=True)
+    grass.run_command('v.extract', input=vect, cats=cat, type='area',
+                      layer=layer, output=tmp_vect, flags='d',
+                      overwrite=overwrite, quiet=True)
     grass.run_command('g.region', rast=origrast)
     grass.run_command('g.region', vect=tmp_vect)
     grass.run_command('g.region', align=origrast)
-    grass.run_command('v.to.rast', input=tmp_vect, type='area', 
-                      layer=layer, use='val', value=cat, output=outrast, 
-		      overwrite=overwrite, quiet=True)
+    grass.run_command('v.to.rast', input=tmp_vect, type='area',
+                      layer=layer, use='val', value=cat, output=outrast,
+                      overwrite=overwrite, quiet=True)
     grass.run_command('g.remove', flags='f', type='vect',
                       name=tmp_vect, quiet=True)
 
 
 def obtainCategories(vector, layer='1'):
-    """This function returns a list of categories for all areas in 
+    """This function returns a list of categories for all areas in
     the given layer"""
     vect_cats = []
     vc = grass.read_command('v.category', input=vector, layer=layer,
@@ -110,13 +111,14 @@
     """Create the string for configuration file"""
     reg = grass.region()
     return "MASKEDOVERLAYAREA {name}|{n}|{s}|{e}|{w}\n".format(name=outrast,
-                                                             n=reg['n'],
-                                                             s=reg['s'],
-                                                             e=reg['e'],
-                                                             w=reg['w'])
+                                                               n=reg['n'],
+                                                               s=reg['s'],
+                                                               e=reg['e'],
+                                                               w=reg['w'])
 
 
-def sampleAreaVector(vect, rast, vect_cats, layer='1', progDialog=None):
+def sampleAreaVector(vect, rast, vect_cats, layer='1', overwrite=False,
+                     progDialog=None):
     """Create the strings to add to the configuration file using vector"""
     areanum = len(vect_cats)
     output = []
@@ -126,8 +128,20 @@
         return None
     for n in range(areanum):
         cat = str(vect_cats[n])
-        rast_name = "{name}_{cat}".format(name=vect.split('@')[0], cat=cat)
-        convertFeature(vect, rast_name, cat, rast, layer=layer)
+        outpref = "{rast}_{vect}_".format(vect=vect.split('@')[0],
+                                          rast=rast.split('@')[0])
+        rast_name = "{pref}{cat}".format(pref=outpref, cat=cat)
+        # check if raster already axist
+
+        if len(grass.list_strings('rast', pattern=rast_name, mapset='.')) == 1 \
+           and not overwrite:
+            GError(message=_("The raster map <%s> already exists."
+                             " Please remove or rename the maps "
+                             "with the prefix '%s' or select the "
+                             "option to overwrite existing maps"
+                             % (rast_name, outpref)))
+            return None
+        convertFeature(vect, rast_name, cat, rast, layer, overwrite)
         output.append(obtainAreaVector(rast_name))
         if progDialog:
             progDialog.Update(n)

Modified: grass/trunk/gui/wxpython/rlisetup/wizard.py
===================================================================
--- grass/trunk/gui/wxpython/rlisetup/wizard.py	2014-11-25 11:24:38 UTC (rev 62930)
+++ grass/trunk/gui/wxpython/rlisetup/wizard.py	2014-11-25 11:34:46 UTC (rev 62931)
@@ -802,6 +802,7 @@
         TitledPage.__init__(self, wizard, _("Insert sampling areas"))
         self.samplingtype = 'whole'
         self.parent = parent
+        self.overwriteTemp = False
         # toggles
         self.radioBox = wx.RadioBox(parent=self, id=wx.ID_ANY,
                                     label="",
@@ -847,8 +848,12 @@
         self.regionNumPanel.SetSizer(self.regionPanelSizer)
         self.sizer.Add(self.regionNumPanel, flag=wx.ALIGN_CENTER, pos=(2, 0))
 
-        self.areaPanelSizer = wx.GridBagSizer(1, 3)
+        self.areaPanelSizer = wx.GridBagSizer(2, 3)
         self.areaPanel = wx.Panel(parent=self, id=wx.ID_ANY)
+        self.overwriteText = wx.StaticText(parent=self.areaPanel, id=wx.ID_ANY,
+                                      label=_('Do you want to overwrite existing'
+                                              ' temporal maps if they exist?'))
+        self.overwriteCheck = wx.CheckBox(parent=self.areaPanel, id=wx.ID_ANY)
         self.areaText = wx.StaticText(parent=self.areaPanel, id=wx.ID_ANY,
                                       label=_('Do you want to check vector areas?'))
         self.areaOK = wx.Button(self.areaPanel, wx.ID_ANY, 'Yes', (50, 80))
@@ -857,10 +862,15 @@
         self.areaNO.SetToolTip(wx.ToolTip(_("All the features will be used")))
         self.areaOK.Bind(wx.EVT_BUTTON, self.OnVectYes)
         self.areaNO.Bind(wx.EVT_BUTTON, self.OnVectNo)
+        self.overwriteCheck.Bind(wx.EVT_CHECKBOX, self.OnOverwrite)
+        self.areaPanelSizer.Add(self.overwriteText, flag=wx.ALIGN_CENTER,
+                                pos=(0, 0))
+        self.areaPanelSizer.Add(self.overwriteCheck, flag=wx.ALIGN_CENTER,
+                                pos=(0, 1))
         self.areaPanelSizer.Add(self.areaText, flag=wx.ALIGN_CENTER,
-                                pos=(0, 0))
-        self.areaPanelSizer.Add(self.areaOK, flag=wx.ALIGN_CENTER, pos=(0, 1))
-        self.areaPanelSizer.Add(self.areaNO, flag=wx.ALIGN_CENTER, pos=(0, 2))
+                                pos=(1, 0))
+        self.areaPanelSizer.Add(self.areaOK, flag=wx.ALIGN_CENTER, pos=(1, 1))
+        self.areaPanelSizer.Add(self.areaNO, flag=wx.ALIGN_CENTER, pos=(1, 2))
         self.areaPanel.SetSizer(self.areaPanelSizer)
         self.sizer.Add(self.areaPanel, flag=wx.ALIGN_CENTER, pos=(3, 0))
 
@@ -939,6 +949,10 @@
         self.RegionDraw(event.GetInt())
         return
 
+    def OnOverwrite(self, event):
+        self.overwriteTemp = self.overwriteCheck.GetValue()
+        return
+
     def OnVectYes(self, event):
         """The user choose to select the vector areas, this function set the
         next page to VectorAreasPage"""
@@ -955,7 +969,8 @@
         self.sizer.Hide(self.areaPanel)
         self.SetNext(self.parent.summarypage)
 
-        vect_cats = obtainCategories(self.parent.startpage.vect, self.vectorlayer)
+        vect_cats = obtainCategories(self.parent.startpage.vect,
+                                     self.parent.startpage.vectorlayer)
 
         self._progressDlg = wx.ProgressDialog(title=_("Analysing vector"),
                                               message="Analysing vector",
@@ -969,6 +984,7 @@
                                                   self.parent.startpage.rast,
                                                   vect_cats,
                                                   self.parent.startpage.vectorlayer,
+                                                  self.overwriteTemp,
                                                   self._progressDlg)
         grass.del_temp_region()
         if self.parent.msAreaList:
@@ -1528,6 +1544,8 @@
         self.areascount = self.areascount + 1
         if self.areascount == self.areanum:
             wx.FindWindowById(wx.ID_FORWARD).Enable(True)
+            self.areaOK.Enable(False)
+            self.areaNO.Enable(False)
             return True
         else:
             self.title.SetLabel(_('Select sample area ' + str(self.areascount + 1) \
@@ -1550,10 +1568,23 @@
     def newCat(self):
         """Convert to raster and draw the new feature"""
         cat = self.vect_cats[self.areascount]
-        self.outname = "{name}_{cat}".format(name=self.vect.split('@')[0],
-                                             cat=cat)
+        self.outpref = "{rast}_{vect}_".format(vect=self.vect.split('@')[0],
+                                               rast=self.rast.split('@')[0])
+        self.outname = "{pref}{cat}".format(pref=self.outpref, cat=cat)
+        # check if raster already axist
+
+        if len(grass.list_strings('rast', pattern=self.outname, mapset='.')) == 1 \
+           and not self.parent.samplingareapage.overwriteTemp:
+            GError(parent=self, message=_("The raster map <%s> already exists."
+                                          " Please remove or rename the maps "
+                                          "with the prefix '%s' or select the "
+                                          "option to overwrite existing maps"
+                                          % (self.outname, self.outpref)))
+            self.parent.wizard.ShowPage(self.parent.samplingareapage)
+            return
         convertFeature(self.vect, self.outname, cat, self.rast,
-                       layer=self.vectlayer)
+                       self.parent.startpage.vectorlayer,
+                       self.parent.samplingareapage.overwriteTemp)
         cmdlistcat = ['d.rast', 'map=%s' % self.outname]
         self.map_.AddLayer(ltype='raster', command=cmdlistcat, active=True,
                            name=self.outname, hidden=False, opacity=1.0,
@@ -1569,6 +1600,7 @@
     def OnEnterPage(self, event):
         """Function during entering: draw the raster map and the first vector
         feature"""
+        print self.parent.samplingareapage.overwriteTemp
         if self.mapPanel is None:
             self.mapPanel = RLiSetupMapPanel(self, samplingType=self.parent.samplingareapage.samplingtype)
             self.sizer.Add(item=self.mapPanel, flag=wx.EXPAND, pos=(1, 0))
@@ -1578,7 +1610,8 @@
 
         self.rast = self.parent.startpage.rast
         self.vect = self.parent.startpage.vect
-        self.vect_cats = obtainCategories(self.vect, layer=self.vectlayer)
+        self.vect_cats = obtainCategories(self.vect,
+                                          layer=self.parent.startpage.vectorlayer)
 
         self.areanum = len(self.vect_cats)
         if self.areanum == 0:



More information about the grass-commit mailing list