16
16
* *
17
17
***************************************************************************
18
18
"""
19
- from builtins import str
20
19
21
20
__author__ = 'Médéric Ribreux'
22
21
__date__ = 'January 2016'
26
25
27
26
__revision__ = '$Format:%H$'
28
27
29
- from qgis .core import QgsProcessingParameterDefinition
30
-
28
+ from qgis .core import (QgsProcessing ,
29
+ QgsProcessingParameterDefinition ,
30
+ QgsProcessingParameterMultipleLayers ,
31
+ QgsProcessingParameterCrs ,
32
+ QgsProcessingParameterEnum ,
33
+ QgsProcessingParameterString ,
34
+ QgsProcessingParameterNumber ,
35
+ QgsProcessingParameterBoolean ,
36
+ QgsProcessingOutputFolder ,
37
+ QgsProcessingParameterFileDestination ,
38
+ QgsProcessingParameterFolderDestination )
31
39
from processing .algs .gdal .GdalAlgorithm import GdalAlgorithm
32
- from processing .core .parameters import ParameterString
33
- from processing .core .parameters import ParameterSelection
34
- from processing .core .parameters import ParameterNumber
35
- from processing .core .parameters import ParameterMultipleInput
36
- from processing .core .parameters import ParameterCrs
37
- from processing .core .parameters import ParameterBoolean
38
- from processing .core .outputs import OutputDirectory
39
- from processing .tools .system import isWindows
40
- from processing .tools import dataobjects
41
40
from processing .algs .gdal .GdalUtils import GdalUtils
42
- import re
41
+ from processing . tools . system import isWindows
43
42
44
43
45
44
class retile (GdalAlgorithm ):
46
45
47
46
INPUT = 'INPUT'
48
- RTYPE = 'RTYPE'
49
- ONLYPYRAMIDS = 'ONLYPYRAMIDS'
50
- PYRAMIDLEVELS = 'PYRAMIDLEVELS'
51
- PIXELSIZE = 'PIXELSIZE'
52
- ALGORITHM = 'ALGORITHM'
53
- USEDIRFOREACHROW = 'USEDIRFOREACHROW'
54
- S_SRS = 'S_SRS'
55
- TARGETDIR = 'TARGETDIR'
56
- CSVFILE = 'CSVFILE'
57
- CSVDELIM = 'CSVDELIM'
58
- TILEINDEX = 'TILEINDEX'
59
- TILEINDEXFIELD = 'TILEINDEXFIELD'
60
- FORMAT = 'FORMAT'
47
+ TILE_SIZE_X = 'TILE_SIZE_X'
48
+ TILE_SIZE_Y = 'TILE_SIZE_Y'
49
+ OVERLAP = 'OVERLAP'
50
+ LEVELS = 'LEVELS'
61
51
62
- TYPE = ['Byte' , 'Int16' , 'UInt16' , 'UInt32' , 'Int32' , 'Float32' , 'Float64' ]
63
- ALGO = ['near' , 'bilinear' , 'cubic' , 'cubicspline' , 'lanczos' ]
52
+ SOURCE_CRS = 'SOURCE_CRS'
53
+ FORMAT = 'FORMAT'
54
+ RESAMPLING = 'RESAMPLING'
55
+ OPTIONS = 'OPTIONS'
56
+ DATA_TYPE = 'DATA_TYPE'
57
+ DELIMITER = 'DELIMITER'
58
+ ONLY_PYRAMIDS = 'ONLY_PYRAMIDS'
59
+ DIR_FOR_ROW = 'DIR_FOR_ROW'
60
+ OUTPUT = 'OUTPUT'
61
+ OUTPUT_CSV = 'OUTPUT_CSV'
64
62
65
- def commandName (self ):
66
- return "gdal_retile"
63
+ TYPES = ['Byte' , 'Int16' , 'UInt16' , 'UInt32' , 'Int32' , 'Float32' , 'Float64' , 'CInt16' , 'CInt32' , 'CFloat32' , 'CFloat64' ]
67
64
68
65
def __init__ (self ):
69
66
super ().__init__ ()
70
67
71
68
def initAlgorithm (self , config = None ):
72
- # Required parameters
73
- self .addParameter (ParameterMultipleInput (self .INPUT ,
74
- self .tr ('Input layers' ),
75
- dataobjects .TYPE_RASTER ))
76
- # Advanced parameters
69
+ self .methods = ((self .tr ('Nearest neighbour' ), 'near' ),
70
+ (self .tr ('Bilinear' ), 'bilinear' ),
71
+ (self .tr ('Cubic' ), 'cubic' ),
72
+ (self .tr ('Cubic spline' ), 'cubicspline' ),
73
+ (self .tr ('Lanczos windowed sinc' ), 'lanczos' ),)
74
+
75
+ self .addParameter (QgsProcessingParameterMultipleLayers (self .INPUT ,
76
+ self .tr ('Input files' ),
77
+ QgsProcessing .TypeRaster ))
78
+ self .addParameter (QgsProcessingParameterNumber (self .TILE_SIZE_X ,
79
+ self .tr ('Tile width' ),
80
+ type = QgsProcessingParameterNumber .Integer ,
81
+ minValue = 0 ,
82
+ defaultValue = 256 ))
83
+ self .addParameter (QgsProcessingParameterNumber (self .TILE_SIZE_Y ,
84
+ self .tr ('Tile height' ),
85
+ type = QgsProcessingParameterNumber .Integer ,
86
+ minValue = 0 ,
87
+ defaultValue = 256 ))
88
+ self .addParameter (QgsProcessingParameterNumber (self .OVERLAP ,
89
+ self .tr ('Overlap in pixels between consecutive tiles' ),
90
+ type = QgsProcessingParameterNumber .Integer ,
91
+ minValue = 0 ,
92
+ defaultValue = 0 ))
93
+ self .addParameter (QgsProcessingParameterNumber (self .LEVELS ,
94
+ self .tr ('Number of pyramids levels to build' ),
95
+ type = QgsProcessingParameterNumber .Integer ,
96
+ minValue = 0 ,
97
+ defaultValue = 1 ))
98
+
77
99
params = []
78
- params .append (ParameterString (self .PIXELSIZE ,
79
- self . tr ( 'Pixel size to be used for the output file (XSIZE YSIZE like 512 512) ' ),
80
- None , False , True ))
81
- params .append (ParameterSelection (self .ALGORITHM ,
82
- self .tr ('Resampling algorithm ' ), self . ALGO , 0 , False , optional = True ))
83
- params . append ( ParameterCrs ( self .S_SRS ,
84
- self . tr ( 'Override source CRS' ), None , True ))
85
- params . append ( ParameterNumber ( self . PYRAMIDLEVELS ,
86
- self . tr ( 'Number of pyramids levels to build' ) ,
87
- None , None , None , True ))
88
- params . append ( ParameterBoolean ( self . ONLYPYRAMIDS ,
89
- self . tr ( 'Build only the pyramids' ),
90
- False , True ))
91
- params . append ( ParameterSelection ( self .RTYPE ,
92
- self .tr ('Output raster type ' ),
93
- self . TYPE , 5 , False , optional = True ))
94
- params . append ( ParameterSelection ( self . FORMAT ,
95
- self . tr ( 'Output raster format' ),
96
- list ( GdalUtils . getSupportedRasters (). keys ()), 0 , False , optional = True ))
97
- params . append ( ParameterBoolean ( self . USEDIRFOREACHROW ,
98
- self . tr ( 'Use a directory for each row' ),
99
- False , True ))
100
- params .append (ParameterString (self .CSVFILE ,
101
- self . tr ( 'Name of the csv file containing the tile(s) georeferencing information ' ),
102
- None , False , True ))
103
- params . append ( ParameterString ( self . CSVDELIM ,
104
- self . tr ( 'Column delimiter used in the CSV file' ),
105
- None , False , True ))
106
- params .append (ParameterString (self .TILEINDEX ,
107
- self . tr ( 'name of shape file containing the result tile(s) index ' ),
108
- None , False , True ))
109
- params .append (ParameterString (self .TILEINDEXFIELD ,
110
- self . tr ( 'name of the attribute containing the tile name in the result shape file ' ),
111
- None , False , True ))
100
+ params .append (QgsProcessingParameterCrs (self .SOURCE_CRS ,
101
+ self . tr ( 'Source coordinate reference system ' ),
102
+ optional = True ))
103
+ params .append (QgsProcessingParameterEnum (self .RESAMPLING ,
104
+ self .tr ('Resampling method ' ),
105
+ options = [ i [ 0 ] for i in self .methods ] ,
106
+ allowMultiple = False ,
107
+ defaultValue = 0 ))
108
+ params . append ( QgsProcessingParameterString ( self . DELIMITER ,
109
+ self . tr ( 'Column delimiter used in the CSV file' ),
110
+ defaultValue = ';' ,
111
+ optional = True ))
112
+
113
+ options_param = QgsProcessingParameterString ( self .OPTIONS ,
114
+ self .tr ('Additional creation parameters ' ),
115
+ defaultValue = '' ,
116
+ optional = True )
117
+ options_param . setMetadata ({
118
+ 'widget_wrapper' : {
119
+ 'class' : 'processing.algs.gdal.ui.RasterOptionsWidget.RasterOptionsWidgetWrapper' }})
120
+ params . append ( options_param )
121
+
122
+ params .append (QgsProcessingParameterEnum (self .DATA_TYPE ,
123
+ self . tr ( 'Output data type ' ),
124
+ self . TYPES ,
125
+ allowMultiple = False ,
126
+ defaultValue = 5 ))
127
+
128
+ params .append (QgsProcessingParameterBoolean (self .ONLY_PYRAMIDS ,
129
+ self . tr ( 'Build only the pyramids ' ),
130
+ defaultValue = False ))
131
+ params .append (QgsProcessingParameterBoolean (self .DIR_FOR_ROW ,
132
+ self . tr ( 'Use separate directory for each tiles row ' ),
133
+ defaultValue = False ))
112
134
113
135
for param in params :
114
136
param .setFlags (param .flags () | QgsProcessingParameterDefinition .FlagAdvanced )
115
137
self .addParameter (param )
116
138
117
- self .addOutput (OutputDirectory (self .TARGETDIR ,
118
- self .tr ('The directory where the tile result is created' )))
139
+ self .addParameter (QgsProcessingParameterFolderDestination (self .OUTPUT ,
140
+ self .tr ('Output directory' )))
141
+
142
+ self .addOutput (QgsProcessingOutputFolder (self .OUTPUT , self .tr ('Output directory' )))
143
+
144
+ output_csv_param = QgsProcessingParameterFileDestination (self .OUTPUT_CSV ,
145
+ self .tr ('CSV file containing the tile(s) georeferencing information' ),
146
+ 'CSV files (*.csv)' ,
147
+ optional = True )
148
+ output_csv_param .setCreateByDefault (False )
149
+ self .addParameter (output_csv_param )
119
150
120
151
def name (self ):
121
152
return 'retile'
@@ -126,65 +157,58 @@ def displayName(self):
126
157
def group (self ):
127
158
return self .tr ('Raster miscellaneous' )
128
159
129
- def getConsoleCommands (self , parameters , context , feedback ):
160
+ def commandName (self ):
161
+ return "gdal_retile"
130
162
163
+ def getConsoleCommands (self , parameters , context , feedback ):
131
164
arguments = []
132
165
133
- if self .getParameterValue (self .RTYPE ):
134
- arguments .append ('-ot' )
135
- arguments .append (self .TYPE [self .getParameterValue (self .RTYPE )])
136
-
137
- arguments .append ('-of' )
138
- arguments .append (list (GdalUtils .getSupportedRasters ().keys ())[self .getParameterValue (self .FORMAT )])
166
+ arguments .append ('-ps' )
167
+ arguments .append (str (self .parameterAsInt (parameters , self .TILE_SIZE_X , context )))
168
+ arguments .append (str (self .parameterAsInt (parameters , self .TILE_SIZE_Y , context )))
139
169
140
- if self .getParameterValue (self .PIXELSIZE ):
141
- pixelSize = self .getParameterValue (self .PIXELSIZE )
142
- if re .match (r'\d+ \d+' , pixelSize ):
143
- xsize , ysize = pixelSize .split (' ' )
144
- arguments .append ('-ps' )
145
- arguments .append (xsize )
146
- arguments .append (ysize )
170
+ arguments .append ('-overlap' )
171
+ arguments .append (str (self .parameterAsInt (parameters , self .OVERLAP , context )))
147
172
148
- if self .getParameterValue (self .ONLYPYRAMIDS ):
149
- arguments .append ('-pyramidOnly' )
150
-
151
- if self .getParameterValue (self .USEDIRFOREACHROW ):
152
- arguments .append ('-useDirForEachRow' )
173
+ arguments .append ('-levels' )
174
+ arguments .append (str (self .parameterAsInt (parameters , self .LEVELS , context )))
153
175
154
- ssrs = str ( self .getParameterValue ( self .S_SRS ) )
155
- if len ( ssrs ) > 0 :
176
+ crs = self .parameterAsCrs ( parameters , self .SOURCE_CRS , context )
177
+ if crs . isValid () :
156
178
arguments .append ('-s_srs' )
157
- arguments .append (ssrs )
158
-
159
- if self .getParameterValue (self .PYRAMIDLEVELS ):
160
- arguments .append ('-levels' )
161
- arguments .append (str (self .getParameterValue (self .PYRAMIDLEVELS )))
179
+ arguments .append (crs .authid ())
162
180
163
181
arguments .append ('-r' )
164
- arguments .append (self .ALGO [self .getParameterValue ( self .ALGORITHM ) ])
182
+ arguments .append (self .methods [self .parameterAsEnum ( parameters , self .RESAMPLING , context )][ 1 ])
165
183
166
- # Handle CSV
167
- if self .getParameterValue (self .CSVFILE ):
168
- arguments .append ('-csv' )
169
- arguments .append (self .getParameterValue (self .CSVFILE ))
184
+ arguments .append ('-ot' )
185
+ arguments .append (self .TYPES [self .parameterAsEnum (parameters , self .DATA_TYPE , context )])
186
+
187
+ options = self .parameterAsString (parameters , self .OPTIONS , context )
188
+ if options :
189
+ arguments .append ('-co' )
190
+ arguments .append (options )
170
191
171
- if self .getParameterValue (self .CSVFILE ) and self .getParameterValue (self .CSVDELIM ):
172
- arguments .append ('-csvDelim' )
173
- arguments .append (self .getParameterValue (self .CSVDELIM ))
192
+ if self .parameterAsBool (parameters , self .DIR_FOR_ROW , context ):
193
+ arguments .append ('-pyramidOnly' )
174
194
175
- # Handle Shp
176
- if self .getParameterValue (self .TILEINDEX ):
177
- arguments .append ('-tileIndex' )
178
- arguments .append (self .getParameterValue (self .TILEINDEX ))
195
+ if self .parameterAsBool (parameters , self .ONLY_PYRAMIDS , context ):
196
+ arguments .append ('-useDirForEachRow' )
179
197
180
- if self .getParameterValue (self .TILEINDEX ) and self .getParameterValue (self .TILEINDEXFIELD ):
181
- arguments .append ('-tileIndexField' )
182
- arguments .append (self .getParameterValue (self .TILEINDEXFIELD ))
198
+ csvFile = self .parameterAsFileOutput (parameters , self .OUTPUT_CSV , context )
199
+ if csvFile :
200
+ arguments .append ('-csv' )
201
+ arguments .append (csvFile )
202
+ delimiter = self .parameterAsString (parameters , self .DELIMITER , context )
203
+ if delimiter :
204
+ arguments .append ('-csvDelim' )
205
+ arguments .append ('"{}"' .format (delimiter ))
183
206
184
207
arguments .append ('-targetDir' )
185
- arguments .append (self .getOutputValue ( self .TARGETDIR ))
208
+ arguments .append (self .parameterAsString ( parameters , self .OUTPUT , context ))
186
209
187
- arguments .extend (self .getParameterValue (self .INPUT ).split (';' ))
210
+ layers = [l .source () for l in self .parameterAsLayerList (parameters , self .INPUT , context )]
211
+ arguments .extend (layers )
188
212
189
213
commands = []
190
214
if isWindows ():
0 commit comments