33
33
from processing .core .parameters import ParameterSelection
34
34
from processing .core .parameters import ParameterNumber
35
35
from processing .core .parameters import ParameterBoolean
36
+ from processing .core .parameters import ParameterString
36
37
from processing .core .outputs import OutputRaster
38
+ from processing .algs .gdal .OgrAlgorithm import OgrAlgorithm
37
39
from processing .algs .gdal .GdalUtils import GdalUtils
40
+ from processing .tools .system import isWindows
38
41
39
42
40
- class rasterize (GdalAlgorithm ):
43
+ class rasterize (OgrAlgorithm ):
41
44
42
45
INPUT = 'INPUT'
43
46
FIELD = 'FIELD'
@@ -47,8 +50,17 @@ class rasterize(GdalAlgorithm):
47
50
WRITEOVER = 'WRITEOVER'
48
51
RTYPE = 'RTYPE'
49
52
OUTPUT = 'OUTPUT'
50
-
51
53
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'
52
64
53
65
def commandLineName (self ):
54
66
return "gdalogr:rasterize"
@@ -70,13 +82,43 @@ def defineCharacteristics(self):
70
82
self .tr ('Vertical' ), 0.0 , 99999999.999999 , 100.0 ))
71
83
self .addParameter (ParameterSelection (self .RTYPE , self .tr ('Raster type' ),
72
84
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 ))
74
105
self .addOutput (OutputRaster (self .OUTPUT ,
75
106
self .tr ('Output layer: mandatory to choose an existing raster layer if the (*) option is selected' )))
76
107
77
108
def processAlgorithm (self , progress ):
78
109
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
+
80
122
arguments = []
81
123
arguments .append ('-a' )
82
124
arguments .append (str (self .getParameterValue (self .FIELD )))
@@ -95,13 +137,27 @@ def processAlgorithm(self, progress):
95
137
arguments .append ('-tr' )
96
138
arguments .append (str (self .getParameterValue (self .WIDTH )))
97
139
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 )
99
157
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 )
103
160
104
161
arguments .append (unicode (self .getOutputValue (self .OUTPUT )))
105
-
106
162
GdalUtils .runGdal (['gdal_rasterize' ,
107
163
GdalUtils .escapeAndJoin (arguments )], progress )
0 commit comments