3333from processing .core .parameters import ParameterSelection
3434from processing .core .parameters import ParameterNumber
3535from processing .core .parameters import ParameterBoolean
36+ from processing .core .parameters import ParameterString
3637from processing .core .outputs import OutputRaster
38+ from processing .algs .gdal .OgrAlgorithm import OgrAlgorithm
3739from processing .algs .gdal .GdalUtils import GdalUtils
40+ from processing .tools .system import isWindows
3841
3942
40- class rasterize (GdalAlgorithm ):
43+ class rasterize (OgrAlgorithm ):
4144
4245 INPUT = 'INPUT'
4346 FIELD = 'FIELD'
@@ -47,8 +50,17 @@ class rasterize(GdalAlgorithm):
4750 WRITEOVER = 'WRITEOVER'
4851 RTYPE = 'RTYPE'
4952 OUTPUT = 'OUTPUT'
50-
5153 TYPE = ['Byte' , 'Int16' , 'UInt16' , 'UInt32' , 'Int32' , 'Float32' , 'Float64' ]
54+ NO_DATA = 'NO_DATA'
55+ TILED = 'TILED'
56+ COMPRESS = 'COMPRESS'
57+ JPEGCOMPRESSION = 'JPEGCOMPRESSION'
58+ PREDICTOR = 'PREDICTOR'
59+ ZLEVEL = 'ZLEVEL'
60+ BIGTIFF = 'BIGTIFF'
61+ BIGTIFFTYPE = ['' , 'YES' , 'NO' , 'IF_NEEDED' , 'IF_SAFER' ]
62+ COMPRESSTYPE = ['NONE' , 'JPEG' , 'LZW' , 'PACKBITS' , 'DEFLATE' ]
63+ TFW = 'TFW'
5264
5365 def commandLineName (self ):
5466 return "gdalogr:rasterize"
@@ -70,13 +82,43 @@ def defineCharacteristics(self):
7082 self .tr ('Vertical' ), 0.0 , 99999999.999999 , 100.0 ))
7183 self .addParameter (ParameterSelection (self .RTYPE , self .tr ('Raster type' ),
7284 self .TYPE , 0 ))
73-
85+ self .addParameter (ParameterString (self .NO_DATA ,
86+ self .tr ("Nodata value" ),
87+ '-9999' ))
88+ self .addParameter (ParameterSelection (self .COMPRESS ,
89+ self .tr ('GeoTIFF options. Compression type:' ), self .COMPRESSTYPE , 0 ))
90+ self .addParameter (ParameterNumber (self .JPEGCOMPRESSION ,
91+ self .tr ('Set the JPEG compression level' ),
92+ 1 , 100 , 75 ))
93+ self .addParameter (ParameterNumber (self .ZLEVEL ,
94+ self .tr ('Set the DEFLATE compression level' ),
95+ 1 , 9 , 6 ))
96+ self .addParameter (ParameterNumber (self .PREDICTOR ,
97+ self .tr ('Set the predictor for LZW or DEFLATE compression' ),
98+ 1 , 3 , 1 ))
99+ self .addParameter (ParameterBoolean (self .TILED ,
100+ self .tr ('Create tiled output (only used for the GTiff format)' ), False ))
101+ self .addParameter (ParameterSelection (self .BIGTIFF ,
102+ self .tr ('Control whether the created file is a BigTIFF or a classic TIFF' ), self .BIGTIFFTYPE , 0 ))
103+ self .addParameter (ParameterBoolean (self .TFW ,
104+ self .tr ('Force the generation of an associated ESRI world file (.tfw))' ), False ))
74105 self .addOutput (OutputRaster (self .OUTPUT ,
75106 self .tr ('Output layer: mandatory to choose an existing raster layer if the (*) option is selected' )))
76107
77108 def processAlgorithm (self , progress ):
78109 writeOver = self .getParameterValue (self .WRITEOVER )
79-
110+ inLayer = self .getParameterValue (self .INPUT )
111+ ogrLayer = self .ogrConnectionString (inLayer )[1 :- 1 ]
112+ noData = str (self .getParameterValue (self .NO_DATA ))
113+ jpegcompression = str (self .getParameterValue (self .JPEGCOMPRESSION ))
114+ predictor = str (self .getParameterValue (self .PREDICTOR ))
115+ zlevel = str (self .getParameterValue (self .ZLEVEL ))
116+ tiled = str (self .getParameterValue (self .TILED ))
117+ compress = self .COMPRESSTYPE [self .getParameterValue (self .COMPRESS )]
118+ bigtiff = self .BIGTIFFTYPE [self .getParameterValue (self .BIGTIFF )]
119+ tfw = str (self .getParameterValue (self .TFW ))
120+ out = self .getOutputValue (self .OUTPUT )
121+
80122 arguments = []
81123 arguments .append ('-a' )
82124 arguments .append (str (self .getParameterValue (self .FIELD )))
@@ -95,13 +137,27 @@ def processAlgorithm(self, progress):
95137 arguments .append ('-tr' )
96138 arguments .append (str (self .getParameterValue (self .WIDTH )))
97139 arguments .append (str (self .getParameterValue (self .HEIGHT )))
98-
140+ if len (noData ) > 0 :
141+ arguments .append ('-a_nodata' )
142+ arguments .append (noData )
143+ if (GdalUtils .getFormatShortNameFromFilename (out ) == "GTiff" ) and (writeOver is False ):
144+ arguments .append ("-co COMPRESS=" + compress )
145+ if compress == 'JPEG' :
146+ arguments .append ("-co JPEG_QUALITY=" + jpegcompression )
147+ elif (compress == 'LZW' ) or (compress == 'DEFLATE' ):
148+ arguments .append ("-co PREDICTOR=" + predictor )
149+ if compress == 'DEFLATE' :
150+ arguments .append ("-co ZLEVEL=" + zlevel )
151+ if tiled == "True" :
152+ arguments .append ("-co TILED=YES" )
153+ if tfw == "True" :
154+ arguments .append ("-co TFW=YES" )
155+ if len (bigtiff ) > 0 :
156+ arguments .append ("-co BIGTIFF=" + bigtiff )
99157 arguments .append ('-l' )
100- arguments .append (
101- os .path .basename (os .path .splitext (unicode (self .getParameterValue (self .INPUT )))[0 ]))
102- arguments .append (unicode (self .getParameterValue (self .INPUT )))
158+ arguments .append (self .ogrLayerName (inLayer ))
159+ arguments .append (ogrLayer )
103160
104161 arguments .append (unicode (self .getOutputValue (self .OUTPUT )))
105-
106162 GdalUtils .runGdal (['gdal_rasterize' ,
107163 GdalUtils .escapeAndJoin (arguments )], progress )
0 commit comments