2929
3030from qgis .PyQt .QtGui import QIcon
3131
32+ from qgis .core import (QgsRasterFileWriter ,
33+ QgsProcessing ,
34+ QgsProcessingParameterDefinition ,
35+ QgsProcessingParameterMultipleLayers ,
36+ QgsProcessingParameterEnum ,
37+ QgsProcessingParameterString ,
38+ QgsProcessingParameterBoolean ,
39+ QgsProcessingParameterRasterDestination )
3240from processing .algs .gdal .GdalAlgorithm import GdalAlgorithm
33- from processing .core .parameters import (ParameterBoolean ,
34- ParameterString ,
35- ParameterSelection ,
36- ParameterMultipleInput )
37- from processing .core .outputs import OutputRaster
38- from processing .tools .system import isWindows
39- from processing .tools import dataobjects
40-
4141from processing .algs .gdal .GdalUtils import GdalUtils
4242
43+ from processing .tools .system import isWindows
44+
4345pluginPath = os .path .split (os .path .split (os .path .dirname (__file__ ))[0 ])[0 ]
4446
4547
4648class merge (GdalAlgorithm ):
4749
4850 INPUT = 'INPUT'
49- OPTIONS = 'OPTIONS'
5051 PCT = 'PCT'
5152 SEPARATE = 'SEPARATE'
52- RTYPE = 'RTYPE'
53+ OPTIONS = 'OPTIONS'
54+ DATA_TYPE = 'DATA_TYPE'
5355 OUTPUT = 'OUTPUT'
5456
55- TYPE = ['Byte' , 'Int16' , 'UInt16' , 'UInt32' , 'Int32' , 'Float32' , 'Float64' ]
56-
57- def icon (self ):
58- return QIcon (os .path .join (pluginPath , 'images' , 'gdaltools' , 'merge.png' ))
57+ TYPES = ['Byte' , 'Int16' , 'UInt16' , 'UInt32' , 'Int32' , 'Float32' , 'Float64' , 'CInt16' , 'CInt32' , 'CFloat32' , 'CFloat64' ]
5958
6059 def __init__ (self ):
6160 super ().__init__ ()
6261
6362 def initAlgorithm (self , config = None ):
64- self .addParameter (ParameterMultipleInput (self .INPUT ,
65- self .tr ('Input layers' ),
66- dataobjects .TYPE_RASTER ))
67- self .addParameter (ParameterString (self .OPTIONS ,
68- self .tr ('Additional creation options' ),
69- optional = True ,
70- metadata = {'widget_wrapper' : 'processing.algs.gdal.ui.RasterOptionsWidget.RasterOptionsWidgetWrapper' }))
71- self .addParameter (ParameterBoolean (self .PCT ,
72- self .tr ('Grab pseudocolor table from first layer' ),
73- False ))
74- self .addParameter (ParameterBoolean (self .SEPARATE ,
75- self .tr ('Place each input file into a separate band' ),
76- False ))
77- self .addParameter (ParameterSelection (self .RTYPE ,
78- self .tr ('Output raster type' ),
79- self .TYPE , 5 ))
80-
81- self .addOutput (OutputRaster (self .OUTPUT , self .tr ('Merged' )))
63+ self .addParameter (QgsProcessingParameterMultipleLayers (self .INPUT ,
64+ self .tr ('Input layers' ),
65+ QgsProcessing .TypeRaster ))
66+ self .addParameter (QgsProcessingParameterBoolean (self .PCT ,
67+ self .tr ('Grab pseudocolor table from first layer' ),
68+ defaultValue = False ))
69+ self .addParameter (QgsProcessingParameterBoolean (self .SEPARATE ,
70+ self .tr ('Place each input file into a separate band' ),
71+ defaultValue = False ))
72+
73+ options_param = QgsProcessingParameterString (self .OPTIONS ,
74+ self .tr ('Additional creation parameters' ),
75+ defaultValue = '' ,
76+ optional = True )
77+ options_param .setFlags (options_param .flags () | QgsProcessingParameterDefinition .FlagAdvanced )
78+ options_param .setMetadata ({
79+ 'widget_wrapper' : {
80+ 'class' : 'processing.algs.gdal.ui.RasterOptionsWidget.RasterOptionsWidgetWrapper' }})
81+ self .addParameter (options_param )
82+
83+ self .addParameter (QgsProcessingParameterEnum (self .DATA_TYPE ,
84+ self .tr ('Output data type' ),
85+ self .TYPES ,
86+ allowMultiple = False ,
87+ defaultValue = 5 ))
88+
89+ self .addParameter (QgsProcessingParameterRasterDestination (self .OUTPUT ,
90+ self .tr ('Merged' )))
8291
8392 def name (self ):
8493 return 'merge'
@@ -89,25 +98,36 @@ def displayName(self):
8998 def group (self ):
9099 return self .tr ('Raster miscellaneous' )
91100
101+ def icon (self ):
102+ return QIcon (os .path .join (pluginPath , 'images' , 'gdaltools' , 'merge.png' ))
103+
92104 def getConsoleCommands (self , parameters , context , feedback ):
105+ layers = self .parameterAsLayerList (parameters , self .INPUT , context )
106+ out = self .parameterAsOutputLayer (parameters , self .OUTPUT , context )
107+
93108 arguments = []
94- arguments .append ('-ot' )
95- arguments .append (self .TYPE [self .getParameterValue (self .RTYPE )])
96- if self .getParameterValue (self .SEPARATE ):
97- arguments .append ('-separate' )
98- if self .getParameterValue (self .PCT ):
109+ if self .parameterAsBool (parameters , self .PCT , context ):
99110 arguments .append ('-pct' )
100- opts = self .getParameterValue (self .OPTIONS )
101- if opts :
111+
112+ if self .parameterAsBool (parameters , self .SEPARATE , context ):
113+ arguments .append ('-separate' )
114+
115+ arguments .append ('-ot' )
116+ arguments .append (self .TYPES [self .parameterAsEnum (parameters , self .DATA_TYPE , context )])
117+
118+ arguments .append ('-of' )
119+ arguments .append (QgsRasterFileWriter .driverForExtension (os .path .splitext (out )[1 ]))
120+
121+ options = self .parameterAsString (parameters , self .OPTIONS , context )
122+ if options :
102123 arguments .append ('-co' )
103- arguments .append (opts )
124+ arguments .append (options )
104125
105126 arguments .append ('-o' )
106- out = self .getOutputValue (self .OUTPUT )
107127 arguments .append (out )
108- arguments . append ( '-of' )
109- arguments . append ( GdalUtils . getFormatShortNameFromFilename ( out ))
110- arguments .extend ( self . getParameterValue ( self . INPUT ). split ( ';' ))
128+
129+ for layer in layers :
130+ arguments .append ( layer . source ( ))
111131
112132 commands = []
113133 if isWindows ():
0 commit comments