[GRASS-SVN] r64372 - grass/trunk/gui/wxpython/gui_core

svn_grass at osgeo.org svn_grass at osgeo.org
Fri Jan 30 10:29:50 PST 2015


Author: martinl
Date: 2015-01-30 10:29:50 -0800 (Fri, 30 Jan 2015)
New Revision: 64372

Modified:
   grass/trunk/gui/wxpython/gui_core/dialogs.py
Log:
wxGUI: fix GDALImport dialog to add multiple imported layers to layer manager


Modified: grass/trunk/gui/wxpython/gui_core/dialogs.py
===================================================================
--- grass/trunk/gui/wxpython/gui_core/dialogs.py	2015-01-30 18:17:38 UTC (rev 64371)
+++ grass/trunk/gui/wxpython/gui_core/dialogs.py	2015-01-30 18:29:50 UTC (rev 64372)
@@ -22,7 +22,7 @@
  - :class:`SqlQueryFrame`
  - :class:`SymbolDialog`
 
-(C) 2008-2011 by the GRASS Development Team
+(C) 2008-2015 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.
@@ -46,7 +46,7 @@
 from grass.pydispatch.signal import Signal
 
 from core import globalvar
-from core.gcmd import GError, RunCommand, GMessage
+from core.gcmd import GError, RunCommand, GMessage, GWarning
 from gui_core.gselect import LocationSelect, MapsetSelect, Select, \
                              OgrTypeSelect, GdalSelect, MapsetSelect, \
                              SubGroupSelect
@@ -1684,7 +1684,7 @@
         self.btn_run.Bind(wx.EVT_BUTTON, self.OnRun)
 
         self.Bind(wx.EVT_CLOSE, lambda evt: self.Destroy())
-
+        
     def doLayout(self):
         """Do layout"""
         dialogSizer = wx.BoxSizer(wx.VERTICAL)
@@ -1762,7 +1762,7 @@
         """Import/Link data (each layes as separate vector map)"""
         pass
 
-    def AddLayers(self, returncode, cmd = None):
+    def AddLayers(self, returncode, cmd = None, userData = None):
         """Add imported/linked layers into layer tree"""
         if not self.add.IsChecked() or returncode != 0:
             return
@@ -1784,14 +1784,32 @@
         # connect to this signal
         llist = self._giface.GetLayerList()
         if self.importType == 'gdal':
-            cmd = ['d.rast',
-                   'map=%s' % name]
+            if userData:
+                nBands = int(userData.get('nbands', 1))
+            else:
+                nBands = 1
+            
             if UserSettings.Get(group = 'rasterLayer', key = 'opaque', subkey = 'enabled'):
-                cmd.append('-n')
-
-            llist.AddLayer(ltype='raster',
-                           name=name, checked=True,
-                           cmd=cmd)
+                nFlag = True
+            else:
+                nFlag = False
+            
+            for i in range(1, nBands+1):
+                nameOrig = name
+                if nBands > 1:
+                    mapName, mapsetName = name.split('@')
+                    mapName += '.%d' % i
+                    name = mapName + '@' + mapsetName
+                    
+                cmd = ['d.rast',
+                       'map=%s' % name]
+                if nFlag:
+                    cmd.append('-n')
+                
+                llist.AddLayer(ltype='raster',
+                               name=name, checked=True,
+                               cmd=cmd)
+                name = nameOrig
         else:
             llist.AddLayer(ltype='vector',
                            name=name, checked=True,
@@ -1808,7 +1826,7 @@
         """
         pass
 
-    def OnCmdDone(self, cmd, returncode):
+    def OnCmdDone(self, event):
         """Do what has to be done after importing"""
         pass
 
@@ -1845,13 +1863,10 @@
                                    ogr = ogr, link = link)
         self.dsnInput.reloadDataRequired.connect(lambda data: self.list.LoadData(data))
 
-        mightNotWork = _("this might not work for multiple bands")
         if link:
-            self.add.SetLabel(_("Add linked layers into layer tree"
-                                " ({mightNotWork})".format(mightNotWork=mightNotWork)))
+            self.add.SetLabel(_("Add linked layers into layer tree"))
         else:
-            self.add.SetLabel(_("Add imported layers into layer tree"
-                                " ({mightNotWork})".format(mightNotWork=mightNotWork)))
+            self.add.SetLabel(_("Add imported layers into layer tree"))
         
         self.add.SetValue(UserSettings.Get(group = 'cmd', key = 'addNewLayer', subkey = 'enabled'))
 
@@ -1861,7 +1876,7 @@
         else:
             self.btn_run.SetLabel(_("&Import"))
             self.btn_run.SetToolTipString(_("Import selected layers"))
-        
+
         self.doLayout()
 
     def OnRun(self, event):
@@ -1886,6 +1901,7 @@
             os.environ['GRASS_VECTOR_OGR'] = '1'
         
         for layer, output in data:
+            userData = {}
             if self.importType == 'ogr':
                 if ext and layer.rfind(ext) > -1:
                     layer = layer.replace('.' + ext, '')
@@ -1909,7 +1925,23 @@
                     idsn = os.path.join(dsn, layer)
                 else:
                     idsn = dsn
-                
+
+                # check number of bands
+                nBandsStr = RunCommand('r.in.gdal',
+                                       flags = 'p',
+                                       input = idsn, read = True)
+                nBands = -1
+                if nBandsStr:
+                    try:
+                        nBands = int(nBandsStr.rstrip('\n'))
+                    except:
+                        pass
+                if nBands < 0:
+                    GWarning(_("Unable to determine number of raster bands"),
+                             parent = self)
+                    nBands = 1
+
+                userData['nbands'] = nBands
                 if self.link:
                     cmd = ['r.external',
                            'input=%s' % idsn,
@@ -1918,6 +1950,8 @@
                     cmd = ['r.in.gdal',
                            'input=%s' % idsn,
                            'output=%s' % output]
+                    if nBands > 1:
+                        cmd.append('-k')
             
             if self.overwrite.IsChecked():
                 cmd.append('--overwrite')
@@ -1931,19 +1965,19 @@
                 cmd.append('--overwrite')
             
             # run in Layer Manager
-            self._giface.RunCmd(cmd, onDone=self.OnCmdDone)
+            self._giface.RunCmd(cmd, onDone = self.OnCmdDone, userData = userData)
 
-    def OnCmdDone(self, cmd, returncode):
+    def OnCmdDone(self, event):
         """Load layers and close if required"""
         if not hasattr(self, 'AddLayers'):
             return
 
-        self.AddLayers(cmd, returncode)
+        self.AddLayers(event.returncode, event.cmd, event.userData)
 
         if self.popOGR:
             os.environ.pop('GRASS_VECTOR_OGR')
 
-        if returncode == 0 and self.closeOnFinish.IsChecked():
+        if event.returncode == 0 and self.closeOnFinish.IsChecked():
             self.Close()
 
     def _getCommand(self):
@@ -2103,12 +2137,12 @@
             # run in Layer Manager
             self._giface.RunCmd(cmd, onDone=self.OnCmdDone)
 
-    def OnCmdDone(self, cmd, returncode):
+    def OnCmdDone(self, event):
         """Load layers and close if required"""
         if not hasattr(self, 'AddLayers'):
             return
 
-        self.AddLayers(cmd, returncode)
+        self.AddLayers(event.returncode, event.cmd)
 
         if self.closeOnFinish.IsChecked():
             self.Close()



More information about the grass-commit mailing list