3333from processing .core .parameters import ParameterVector
3434from processing .core .parameters import ParameterBoolean
3535from processing .core .parameters import ParameterString
36+ from processing .core .parameters import ParameterSelection
37+ from processing .core .parameters import ParameterNumber
3638
3739from processing .core .outputs import OutputRaster
3840
41+ from processing .algs .gdal .OgrAlgorithm import OgrAlgorithm
3942from processing .algs .gdal .GdalUtils import GdalUtils
4043
4144
42- class ClipByMask (GdalAlgorithm ):
45+ class ClipByMask (GdalAlgorithm , OgrAlgorithm ):
4346
4447 INPUT = 'INPUT'
4548 OUTPUT = 'OUTPUT'
@@ -48,7 +51,18 @@ class ClipByMask(GdalAlgorithm):
4851 ALPHA_BAND = 'ALPHA_BAND'
4952 KEEP_RESOLUTION = 'KEEP_RESOLUTION'
5053 EXTRA = 'EXTRA'
51-
54+ RTYPE = 'RTYPE'
55+ TYPE = ['Byte' , 'Int16' , 'UInt16' , 'UInt32' , 'Int32' , 'Float32' , 'Float64' ]
56+ TILED = 'TILED'
57+ COMPRESS = 'COMPRESS'
58+ JPEGCOMPRESSION = 'JPEGCOMPRESSION'
59+ PREDICTOR = 'PREDICTOR'
60+ ZLEVEL = 'ZLEVEL'
61+ BIGTIFF = 'BIGTIFF'
62+ BIGTIFFTYPE = ['' , 'YES' , 'NO' , 'IF_NEEDED' , 'IF_SAFER' ]
63+ COMPRESSTYPE = ['NONE' , 'JPEG' , 'LZW' , 'PACKBITS' , 'DEFLATE' ]
64+ TFW = 'TFW'
65+
5266 def defineCharacteristics (self ):
5367 self .name , self .i18n_name = self .trAlgorithm ('Clip raster by mask layer' )
5468 self .group , self .i18n_group = self .trAlgorithm ('[GDAL] Extraction' )
@@ -64,17 +78,55 @@ def defineCharacteristics(self):
6478 self .tr ('Keep resolution of output raster' ), False ))
6579 self .addParameter (ParameterString (self .EXTRA ,
6680 self .tr ('Additional creation parameters' ), '' , optional = True ))
81+
82+ params = []
83+ params .append (ParameterSelection (self .RTYPE ,
84+ self .tr ('Output raster type' ), self .TYPE , 5 ))
85+ params .append (ParameterSelection (self .COMPRESS ,
86+ self .tr ('GeoTIFF options. Compression type:' ), self .COMPRESSTYPE , 4 ))
87+ params .append (ParameterNumber (self .JPEGCOMPRESSION ,
88+ self .tr ('Set the JPEG compression level' ),
89+ 1 , 100 , 75 ))
90+ params .append (ParameterNumber (self .ZLEVEL ,
91+ self .tr ('Set the DEFLATE compression level' ),
92+ 1 , 9 , 6 ))
93+ params .append (ParameterNumber (self .PREDICTOR ,
94+ self .tr ('Set the predictor for LZW or DEFLATE compression' ),
95+ 1 , 3 , 1 ))
96+ params .append (ParameterBoolean (self .TILED ,
97+ self .tr ('Create tiled output (only used for the GTiff format)' ), False ))
98+ params .append (ParameterSelection (self .BIGTIFF ,
99+ self .tr ('Control whether the created file is a BigTIFF or a classic TIFF' ), self .BIGTIFFTYPE , 0 ))
100+ params .append (ParameterBoolean (self .TFW ,
101+ self .tr ('Force the generation of an associated ESRI world file (.tfw))' ), False ))
102+ params .append (ParameterString (self .EXTRA ,
103+ self .tr ('Additional creation parameters' ), '' , optional = True ))
104+
105+ for param in params :
106+ param .isAdvanced = True
107+ self .addParameter (param )
108+
67109 self .addOutput (OutputRaster (self .OUTPUT , self .tr ('Clipped (mask)' )))
68110
69111 def getConsoleCommands (self ):
70112 out = self .getOutputValue (self .OUTPUT )
71113 mask = self .getParameterValue (self .MASK )
114+ ogrMask = self .ogrConnectionString (mask )[1 :- 1 ]
72115 noData = unicode (self .getParameterValue (self .NO_DATA ))
73116 addAlphaBand = self .getParameterValue (self .ALPHA_BAND )
74117 keepResolution = self .getParameterValue (self .KEEP_RESOLUTION )
75118 extra = unicode (self .getParameterValue (self .EXTRA ))
76-
119+ jpegcompression = unicode (self .getParameterValue (self .JPEGCOMPRESSION ))
120+ predictor = unicode (self .getParameterValue (self .PREDICTOR ))
121+ zlevel = unicode (self .getParameterValue (self .ZLEVEL ))
122+ tiled = unicode (self .getParameterValue (self .TILED ))
123+ compress = self .COMPRESSTYPE [self .getParameterValue (self .COMPRESS )]
124+ bigtiff = self .BIGTIFFTYPE [self .getParameterValue (self .BIGTIFF )]
125+ tfw = unicode (self .getParameterValue (self .TFW ))
126+
77127 arguments = []
128+ arguments .append ('-ot' )
129+ arguments .append (self .TYPE [self .getParameterValue (self .RTYPE )])
78130 arguments .append ('-q' )
79131 arguments .append ('-of' )
80132 arguments .append (GdalUtils .getFormatShortNameFromFilename (out ))
@@ -92,14 +144,28 @@ def getConsoleCommands(self):
92144 arguments .append ('-tap' )
93145
94146 arguments .append ('-cutline' )
95- arguments .append (mask )
147+ arguments .append (ogrMask )
96148 arguments .append ('-crop_to_cutline' )
97149
98150 if addAlphaBand :
99151 arguments .append ('-dstalpha' )
100152
101153 if len (extra ) > 0 :
102154 arguments .append (extra )
155+ if GdalUtils .getFormatShortNameFromFilename (out ) == "GTiff" :
156+ arguments .append ("-co COMPRESS=" + compress )
157+ if compress == 'JPEG' :
158+ arguments .append ("-co JPEG_QUALITY=" + jpegcompression )
159+ elif (compress == 'LZW' ) or (compress == 'DEFLATE' ):
160+ arguments .append ("-co PREDICTOR=" + predictor )
161+ if compress == 'DEFLATE' :
162+ arguments .append ("-co ZLEVEL=" + zlevel )
163+ if tiled == "True" :
164+ arguments .append ("-co TILED=YES" )
165+ if tfw == "True" :
166+ arguments .append ("-co TFW=YES" )
167+ if len (bigtiff ) > 0 :
168+ arguments .append ("-co BIGTIFF=" + bigtiff )
103169
104170 arguments .append (self .getParameterValue (self .INPUT ))
105171 arguments .append (out )
0 commit comments