Переглянути джерело

d.to.rast: new module for wxGUI and d.mon to save map display content as raster map (merge from trunk, https://trac.osgeo.org/grass/changeset/62281)

git-svn-id: https://svn.osgeo.org/grass/grass/branches/releasebranch_7_0@62394 15284696-431f-4ddb-bdfa-cd5b030d7da7
Anna Petrášová 10 роки тому
батько
коміт
42388b536b

+ 1 - 0
gui/wxpython/core/utils.py

@@ -1002,6 +1002,7 @@ command2ltype = {'d.rast'         : 'raster',
                  'd.colortable'   : 'colortable',
                  'd.graph'        : 'graph',
                  'd.out.file'     : 'export',
+                 'd.to.rast'      : 'torast',
                  'd.text'         : 'text'
                  }
 ltype2command = {}

+ 10 - 0
gui/wxpython/lmgr/frame.py

@@ -660,6 +660,16 @@ class GMFrame(wx.Frame):
             GUI(parent=self, show=False).ParseCommand(command,
                                                       completed=(self.GetMapDisplay().DOutFileOptData,
                                                                  '', ''))
+        elif layertype == 'torast':
+            if len(command) <= 1:
+                task = GUI(parent=self, show=True).ParseCommand(command,
+                                                                completed=(self.GetMapDisplay().DToRastOptData,
+                                                                '', ''))
+            else:
+                task = GUI(parent=self, show=None).ParseCommand(command,
+                                                                completed=(self.GetMapDisplay().DToRastOptData,
+                                                                '', ''))
+                self.GetMapDisplay().DToRast(command=task.get_cmd())
         else:
             # add layer into layer tree
             lname, found = GetLayerNameFromCmd(command, fullyQualified = True,

+ 71 - 1
gui/wxpython/mapdisp/frame.py

@@ -35,7 +35,7 @@ from core.render        import Map
 from vdigit.toolbars    import VDigitToolbar
 from mapdisp.toolbars   import MapToolbar, NvizIcons
 from mapdisp.gprint     import PrintOptions
-from core.gcmd          import GError, GMessage
+from core.gcmd          import GError, GMessage, RunCommand
 from dbmgr.dialogs      import DisplayAttributesDialog
 from core.utils         import ListOfCatsToRange, GetLayerNameFromCmd, _
 from gui_core.dialogs import GetImageHandlers, ImageSizeDialog
@@ -649,6 +649,76 @@ class MapFrame(SingleMapFrame):
 
         self.DOutFile(dcmd)
 
+    def DToRast(self, command):
+        """Saves currently loaded composition of layers as a raster map.
+        """
+        if self.IsPaneShown('3d'):
+            self._giface.WriteError(_('d.to.rast can be used only in 2D mode.'))
+            return
+        outputRaster = None
+        overwrite = False
+        for param in command[1:]:
+            try:
+                p, val = param.split('=')
+                if p == 'output':
+                    outputRaster = val
+            except ValueError:
+                if param.startswith('--overwrite'):
+                    overwrite = True
+
+        if not outputRaster:
+            return
+        # output file as PNG
+        tmpName = 'd_to_rast_tmp'
+        pngFile = grass.tempfile(create=False) + '.png'
+        dOutFileCmd = ['d.out.file', 'output=' + pngFile, 'format=png']
+        self.DOutFile(dOutFileCmd)
+        # import back as red, green, blue rasters
+        returncode, messages = RunCommand('r.in.png', input=pngFile, output=tmpName,
+                                          quiet=True, overwrite=overwrite, getErrorMsg=True)
+        if not returncode == 0:
+            self._giface.WriteError(_('Failed to run d.to.rast:\n') + messages)
+            return
+        # set region for composite
+        grass.use_temp_region()
+        returncode, messages = RunCommand('g.region', rast=tmpName + '.r',
+                                          quiet=True, getErrorMsg=True)
+        if not returncode == 0:
+            grass.del_temp_region()
+            self._giface.WriteError(_('Failed to run d.to.rast:\n') + messages)
+            return
+        # composite
+        returncode, messages = RunCommand('r.composite', red=tmpName + '.r',
+                                          green=tmpName + '.g', blue=tmpName + '.b',
+                                          output=outputRaster, quiet=True,
+                                          overwrite=overwrite, getErrorMsg=True)
+        grass.del_temp_region()
+        RunCommand('g.remove', type='rast', flags='f', quiet=True,
+                   names=[tmpName + '.r', tmpName + '.g', tmpName + '.b'])
+        if not returncode == 0:
+            self._giface.WriteError(_('Failed to run d.to.rast:\n') + messages)
+            grass.try_remove(pngFile)
+            return
+
+        # alignExtent changes only region variable
+        oldRegion = self.GetMap().GetCurrentRegion().copy()
+        self.GetMap().AlignExtentFromDisplay()
+        region = self.GetMap().GetCurrentRegion().copy()
+        self.GetMap().region.update(oldRegion)
+        RunCommand('r.region', map=outputRaster, n=region['n'], s=region['s'],
+                   e=region['e'], w=region['w'], quiet=True)
+
+        grass.try_remove(pngFile)
+
+    def DToRastOptData(self, dcmd, layer, params, propwin):
+        """Dummy function which is called when d.to.rast is called
+        and returns parsed and validated command which is then passed
+        to DToRast method."""
+        if not dcmd:
+            return
+
+        self.DToRast(dcmd)
+
     def _prepareSaveToFile(self):
         """!Get wildcards and format extensions."""
         if self.IsPaneShown('3d'):

+ 9 - 4
gui/wxpython/mapdisp/main.py

@@ -80,8 +80,9 @@ class DMonMap(Map):
 
         # generated file for g.pnmcomp output for rendering the map
         self.mapfile = monFile['map'] + '.ppm'
-        # signal sent when d.out.file appears in cmd file, attribute is cmd
+        # signal sent when d.out.file/d.to.rast appears in cmd file, attribute is cmd
         self.saveToFile = Signal('DMonMap.saveToFile')
+        self.dToRast = Signal('DMonMap.dToRast')
         # signal sent when d.what.rast/vect appears in cmd file, attribute is cmd
         self.query = Signal('DMonMap.query')
 
@@ -99,12 +100,15 @@ class DMonMap(Map):
             fd.close()
             # detect d.out.file, delete the line from the cmd file and export graphics
             if len(lines) > 0:
-                if lines[-1].startswith('d.out.file'):
-                    dOutFileCmd = lines[-1].strip()
+                if lines[-1].startswith('d.out.file') or lines[-1].startswith('d.to.rast'):
+                    dCmd = lines[-1].strip()
                     fd = open(self.cmdfile, 'w')
                     fd.writelines(lines[:-1])
                     fd.close()
-                    self.saveToFile.emit(cmd=utils.split(dOutFileCmd))
+                    if lines[-1].startswith('d.out.file'):
+                        self.saveToFile.emit(cmd=utils.split(dCmd))
+                    else:
+                        self.dToRast.emit(cmd=utils.split(dCmd))
                     return
                 if lines[-1].startswith('d.what'):
                     dWhatCmd = lines[-1].strip()
@@ -360,6 +364,7 @@ class MapApp(wx.App):
         # self.SetTopWindow(Map)
         self.mapFrm.GetMapWindow().SetAlwaysRenderEnabled(True)
         self.Map.saveToFile.connect(lambda cmd: self.mapFrm.DOutFile(cmd))
+        self.Map.dToRast.connect(lambda cmd: self.mapFrm.DToRast(cmd))
         self.Map.query.connect(lambda ltype, maps: self.mapFrm.SetQueryLayersAndActivate(ltype=ltype, maps=maps))
         self.mapFrm.Show()
         

+ 1 - 0
scripts/Makefile

@@ -3,6 +3,7 @@ MODULE_TOPDIR = ..
 SUBDIRS = \
 	d.correlate \
 	d.out.file \
+	d.to.rast \
 	d.polar \
 	d.rast.edit \
 	d.rast.leg \