16
16
* *
17
17
***************************************************************************
18
18
"""
19
+ from future import standard_library
20
+ standard_library .install_aliases ()
21
+ from builtins import str
22
+ from builtins import range
19
23
20
24
21
25
__author__ = 'Victor Olaya'
58
62
sessionExportedLayers = {}
59
63
60
64
61
- class SagaAlgorithm212 (GeoAlgorithm ):
65
+ class SagaAlgorithm (GeoAlgorithm ):
62
66
63
67
OUTPUT_EXTENT = 'OUTPUT_EXTENT'
64
68
@@ -68,55 +72,57 @@ def __init__(self, descriptionfile):
68
72
self .allowUnmatchingGridExtents = False
69
73
self .descriptionFile = descriptionfile
70
74
self .defineCharacteristicsFromFile ()
75
+ self ._icon = None
71
76
72
77
def getCopy (self ):
73
- newone = SagaAlgorithm212 (self .descriptionFile )
78
+ newone = SagaAlgorithm (self .descriptionFile )
74
79
newone .provider = self .provider
75
80
return newone
76
81
77
82
def getIcon (self ):
78
- return QIcon (os .path .join (pluginPath , 'images' , 'saga.png' ))
83
+ if self ._icon is None :
84
+ self ._icon = QIcon (os .path .join (pluginPath , 'images' , 'saga.png' ))
85
+ return self ._icon
79
86
80
87
def defineCharacteristicsFromFile (self ):
81
- lines = open (self .descriptionFile )
82
- line = lines .readline ().strip ('\n ' ).strip ()
83
- self .name = line
84
- if '|' in self .name :
85
- tokens = self .name .split ('|' )
86
- self .name = tokens [0 ]
87
- #cmdname is the name of the algorithm in SAGA, that is, the name to use to call it in the console
88
- self .cmdname = tokens [1 ]
88
+ with open (self .descriptionFile ) as lines :
89
+ line = lines .readline ().strip ('\n ' ).strip ()
90
+ self .name = line
91
+ if '|' in self .name :
92
+ tokens = self .name .split ('|' )
93
+ self .name = tokens [0 ]
94
+ #cmdname is the name of the algorithm in SAGA, that is, the name to use to call it in the console
95
+ self .cmdname = tokens [1 ]
89
96
90
- else :
91
- self .cmdname = self .name
92
- self .i18n_name = QCoreApplication .translate ("SAGAAlgorithm" , unicode (self .name ))
93
- #_commandLineName is the name used in processing to call the algorithm
94
- #Most of the time will be equal to the cmdname, but in same cases, several processing algorithms
95
- #call the same SAGA one
96
- self ._commandLineName = self .createCommandLineName (self .name )
97
- self .name = decoratedAlgorithmName (self .name )
98
- self .i18n_name = QCoreApplication .translate ("SAGAAlgorithm" , unicode (self .name ))
99
- line = lines .readline ().strip ('\n ' ).strip ()
100
- self .undecoratedGroup = line
101
- self .group = decoratedGroupName (self .undecoratedGroup )
102
- self .i18n_group = QCoreApplication .translate ("SAGAAlgorithm" , self .group )
103
- line = lines .readline ().strip ('\n ' ).strip ()
104
- while line != '' :
105
- if line .startswith ('Hardcoded' ):
106
- self .hardcodedStrings .append (line [len ('Hardcoded|' ):])
107
- elif line .startswith ('Parameter' ):
108
- self .addParameter (getParameterFromString (line ))
109
- elif line .startswith ('AllowUnmatching' ):
110
- self .allowUnmatchingGridExtents = True
111
- elif line .startswith ('Extent' ):
112
- # An extent parameter that wraps 4 SAGA numerical parameters
113
- self .extentParamNames = line [6 :].strip ().split (' ' )
114
- self .addParameter (ParameterExtent (self .OUTPUT_EXTENT ,
115
- 'Output extent' , '' ))
116
97
else :
117
- self .addOutput (getOutputFromString (line ))
98
+ self .cmdname = self .name
99
+ self .i18n_name = QCoreApplication .translate ("SAGAAlgorithm" , str (self .name ))
100
+ #_commandLineName is the name used in processing to call the algorithm
101
+ #Most of the time will be equal to the cmdname, but in same cases, several processing algorithms
102
+ #call the same SAGA one
103
+ self ._commandLineName = self .createCommandLineName (self .name )
104
+ self .name = decoratedAlgorithmName (self .name )
105
+ self .i18n_name = QCoreApplication .translate ("SAGAAlgorithm" , str (self .name ))
106
+ line = lines .readline ().strip ('\n ' ).strip ()
107
+ self .undecoratedGroup = line
108
+ self .group = decoratedGroupName (self .undecoratedGroup )
109
+ self .i18n_group = QCoreApplication .translate ("SAGAAlgorithm" , self .group )
118
110
line = lines .readline ().strip ('\n ' ).strip ()
119
- lines .close ()
111
+ while line != '' :
112
+ if line .startswith ('Hardcoded' ):
113
+ self .hardcodedStrings .append (line [len ('Hardcoded|' ):])
114
+ elif line .startswith ('Parameter' ):
115
+ self .addParameter (getParameterFromString (line ))
116
+ elif line .startswith ('AllowUnmatching' ):
117
+ self .allowUnmatchingGridExtents = True
118
+ elif line .startswith ('Extent' ):
119
+ # An extent parameter that wraps 4 SAGA numerical parameters
120
+ self .extentParamNames = line [6 :].strip ().split (' ' )
121
+ self .addParameter (ParameterExtent (self .OUTPUT_EXTENT ,
122
+ 'Output extent' ))
123
+ else :
124
+ self .addOutput (getOutputFromString (line ))
125
+ line = lines .readline ().strip ('\n ' ).strip ()
120
126
121
127
def processAlgorithm (self , progress ):
122
128
commands = list ()
@@ -162,7 +168,7 @@ def processAlgorithm(self, progress):
162
168
layers = param .value .split (';' )
163
169
if layers is None or len (layers ) == 0 :
164
170
continue
165
- if param .datatype == ParameterMultipleInput .TYPE_RASTER :
171
+ if param .datatype == dataobjects .TYPE_RASTER :
166
172
for i , layerfile in enumerate (layers ):
167
173
if layerfile .endswith ('sdat' ):
168
174
layerfile = param .value [:- 4 ] + "sgrd"
@@ -172,10 +178,10 @@ def processAlgorithm(self, progress):
172
178
if exportCommand is not None :
173
179
commands .append (exportCommand )
174
180
param .value = ";" .join (layers )
175
- elif param .datatype in [ParameterMultipleInput .TYPE_VECTOR_ANY ,
176
- ParameterMultipleInput .TYPE_VECTOR_LINE ,
177
- ParameterMultipleInput .TYPE_VECTOR_POLYGON ,
178
- ParameterMultipleInput .TYPE_VECTOR_POINT ]:
181
+ elif param .datatype in [dataobjects .TYPE_VECTOR_ANY ,
182
+ dataobjects .TYPE_VECTOR_LINE ,
183
+ dataobjects .TYPE_VECTOR_POLYGON ,
184
+ dataobjects .TYPE_VECTOR_POINT ]:
179
185
for layerfile in layers :
180
186
layer = dataobjects .getObjectFromUri (layerfile , False )
181
187
if layer :
@@ -194,28 +200,27 @@ def processAlgorithm(self, progress):
194
200
continue
195
201
if isinstance (param , (ParameterRaster , ParameterVector , ParameterTable )):
196
202
value = param .value
197
- if value in self .exportedLayers .keys ():
203
+ if value in list ( self .exportedLayers .keys () ):
198
204
command += ' -' + param .name + ' "' \
199
205
+ self .exportedLayers [value ] + '"'
200
206
else :
201
207
command += ' -' + param .name + ' "' + value + '"'
202
208
elif isinstance (param , ParameterMultipleInput ):
203
209
s = param .value
204
- for layer in self .exportedLayers .keys ():
210
+ for layer in list ( self .exportedLayers .keys () ):
205
211
s = s .replace (layer , self .exportedLayers [layer ])
206
212
command += ' -' + param .name + ' "' + s + '"'
207
213
elif isinstance (param , ParameterBoolean ):
208
214
if param .value :
209
215
command += ' -' + param .name
210
216
elif isinstance (param , ParameterFixedTable ):
211
217
tempTableFile = getTempFilename ('txt' )
212
- f = open (tempTableFile , 'w' )
213
- f .write ('\t ' .join ([col for col in param .cols ]) + '\n ' )
214
- values = param .value .split (',' )
215
- for i in range (0 , len (values ), 3 ):
216
- s = values [i ] + '\t ' + values [i + 1 ] + '\t ' + values [i + 2 ] + '\n '
217
- f .write (s )
218
- f .close ()
218
+ with open (tempTableFile , 'w' ) as f :
219
+ f .write ('\t ' .join ([col for col in param .cols ]) + '\n ' )
220
+ values = param .value .split (',' )
221
+ for i in range (0 , len (values ), 3 ):
222
+ s = values [i ] + '\t ' + values [i + 1 ] + '\t ' + values [i + 2 ] + '\n '
223
+ f .write (s )
219
224
command += ' -' + param .name + ' "' + tempTableFile + '"'
220
225
elif isinstance (param , ParameterExtent ):
221
226
# 'We have to substract/add half cell size, since SAGA is
@@ -225,11 +230,11 @@ def processAlgorithm(self, progress):
225
230
values = param .value .split (',' )
226
231
for i in range (4 ):
227
232
command += ' -' + self .extentParamNames [i ] + ' ' \
228
- + unicode (float (values [i ]) + offset [i ])
233
+ + str (float (values [i ]) + offset [i ])
229
234
elif isinstance (param , (ParameterNumber , ParameterSelection )):
230
- command += ' -' + param .name + ' ' + unicode (param .value )
235
+ command += ' -' + param .name + ' ' + str (param .value )
231
236
else :
232
- command += ' -' + param .name + ' "' + unicode (param .value ) + '"'
237
+ command += ' -' + param .name + ' "' + str (param .value ) + '"'
233
238
234
239
for out in self .outputs :
235
240
command += ' -' + out .name + ' "' + out .getCompatibleFileName (self ) + '"'
@@ -288,7 +293,7 @@ def editCommands(self, commands):
288
293
return commands
289
294
290
295
def getOutputCellsize (self ):
291
- """Tries to guess the cellsize of the output, searching for
296
+ """Tries to guess the cell size of the output, searching for
292
297
a parameter with an appropriate name for it.
293
298
"""
294
299
@@ -310,7 +315,7 @@ def exportRasterLayer(self, source):
310
315
del sessionExportedLayers [source ]
311
316
layer = dataobjects .getObjectFromUri (source , False )
312
317
if layer :
313
- filename = unicode (layer .name ())
318
+ filename = str (layer .name ())
314
319
else :
315
320
filename = os .path .basename (source )
316
321
validChars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789:'
@@ -333,7 +338,7 @@ def checkParameterValuesBeforeExecuting(self):
333
338
if isinstance (param , ParameterRaster ):
334
339
files = [param .value ]
335
340
elif (isinstance (param , ParameterMultipleInput ) and
336
- param .datatype == ParameterMultipleInput .TYPE_RASTER ):
341
+ param .datatype == dataobjects .TYPE_RASTER ):
337
342
if param .value is not None :
338
343
files = param .value .split (";" )
339
344
for f in files :
@@ -342,7 +347,7 @@ def checkParameterValuesBeforeExecuting(self):
342
347
continue
343
348
if layer .bandCount () > 1 :
344
349
return self .tr ('Input layer %s has more than one band.\n '
345
- 'Multiband layers are not supported by SAGA' % unicode (layer .name ()))
350
+ 'Multiband layers are not supported by SAGA' % str (layer .name ()))
346
351
if not self .allowUnmatchingGridExtents :
347
352
if extent is None :
348
353
extent = (layer .extent (), layer .height (), layer .width ())
0 commit comments