29
29
30
30
from qgis .PyQt .QtGui import QIcon
31
31
32
+ from qgis .core import (QgsRasterFileWriter ,
33
+ QgsProcessing ,
34
+ QgsProcessingParameterDefinition ,
35
+ QgsProcessingParameterMultipleLayers ,
36
+ QgsProcessingParameterEnum ,
37
+ QgsProcessingParameterString ,
38
+ QgsProcessingParameterBoolean ,
39
+ QgsProcessingParameterRasterDestination )
32
40
from 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
-
41
41
from processing .algs .gdal .GdalUtils import GdalUtils
42
42
43
+ from processing .tools .system import isWindows
44
+
43
45
pluginPath = os .path .split (os .path .split (os .path .dirname (__file__ ))[0 ])[0 ]
44
46
45
47
46
48
class merge (GdalAlgorithm ):
47
49
48
50
INPUT = 'INPUT'
49
- OPTIONS = 'OPTIONS'
50
51
PCT = 'PCT'
51
52
SEPARATE = 'SEPARATE'
52
- RTYPE = 'RTYPE'
53
+ OPTIONS = 'OPTIONS'
54
+ DATA_TYPE = 'DATA_TYPE'
53
55
OUTPUT = 'OUTPUT'
54
56
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' ]
59
58
60
59
def __init__ (self ):
61
60
super ().__init__ ()
62
61
63
62
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' )))
82
91
83
92
def name (self ):
84
93
return 'merge'
@@ -89,25 +98,36 @@ def displayName(self):
89
98
def group (self ):
90
99
return self .tr ('Raster miscellaneous' )
91
100
101
+ def icon (self ):
102
+ return QIcon (os .path .join (pluginPath , 'images' , 'gdaltools' , 'merge.png' ))
103
+
92
104
def getConsoleCommands (self , parameters , context , feedback ):
105
+ layers = self .parameterAsLayerList (parameters , self .INPUT , context )
106
+ out = self .parameterAsOutputLayer (parameters , self .OUTPUT , context )
107
+
93
108
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 ):
99
110
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 :
102
123
arguments .append ('-co' )
103
- arguments .append (opts )
124
+ arguments .append (options )
104
125
105
126
arguments .append ('-o' )
106
- out = self .getOutputValue (self .OUTPUT )
107
127
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 ( ))
111
131
112
132
commands = []
113
133
if isWindows ():
0 commit comments