20
20
from builtins import range
21
21
from builtins import object
22
22
23
-
24
23
__author__ = 'Victor Olaya'
25
24
__date__ = 'August 2012'
26
25
__copyright__ = '(C) 2012, Victor Olaya'
29
28
30
29
__revision__ = '$Format:%H$'
31
30
31
+ import os
32
32
import sys
33
33
34
34
from qgis .PyQt .QtCore import QCoreApplication , QSettings
35
35
36
36
from processing .core .ProcessingConfig import ProcessingConfig
37
- from processing .tools .system import isWindows , getTempFilenameInTempFolder
37
+ from processing .tools .system import isWindows , getTempFilenameInTempFolder , getTempDirInTempFolder
38
38
from processing .tools .vector import VectorWriter , TableWriter
39
39
from processing .tools import dataobjects
40
40
41
+ from qgis .core import QgsExpressionContext , QgsExpressionContextUtils , QgsExpression , QgsExpressionContextScope
42
+
43
+ def _expressionContext (alg ):
44
+ context = QgsExpressionContext ()
45
+ context .appendScope (QgsExpressionContextUtils .globalScope ())
46
+ context .appendScope (QgsExpressionContextUtils .projectScope ())
47
+ processingScope = QgsExpressionContextScope ()
48
+ for param in alg .parameters :
49
+ processingScope .setVariable ('%s_value' % param .name , '' )
50
+ context .appendScope (processingScope )
51
+ return context
41
52
42
53
class Output (object ):
43
54
@@ -82,6 +93,44 @@ def setValue(self, value):
82
93
return True
83
94
except :
84
95
return False
96
+
97
+ def _resolveTemporary (self , alg ):
98
+ ext = self .getDefaultFileExtension ()
99
+ return getTempFilenameInTempFolder (self .name + '.' + ext )
100
+
101
+ def _supportedExtensions (self ):
102
+ return []
103
+
104
+ def resolveValue (self , alg ):
105
+ if not self .hidden and not bool (self .value ):
106
+ self .value = self ._resolveTemporary (alg )
107
+ else :
108
+ exp = QgsExpression (self .value )
109
+ if exp .hasParserError ():
110
+ raise ValueError (self .tr ("Error in output expression: " ) + exp .parserErrorString ())
111
+ self .value = exp .evaluate (_expressionContext (alg ))
112
+ if exp .hasEvalError ():
113
+ raise ValueError ("Error evaluating output expression: " + exp .evalErrorString ())
114
+
115
+
116
+
117
+ print self .value
118
+ if ":" not in self .value :
119
+ if not os .path .isabs (self .value ):
120
+ self .value = os .path .join (ProcessingConfig .getSetting (ProcessingConfig .OUTPUT_FOLDER ),
121
+ self .value )
122
+ supported = self ._supportedExtensions ()
123
+ if supported :
124
+ idx = self .value .rfind ('.' )
125
+ if idx == - 1 :
126
+ self .value = self .value + '.' + self .getDefaultFileExtension ()
127
+ else :
128
+ ext = self .value [idx + 1 :]
129
+ if ext not in supported :
130
+ self .value = self .value + '.' + self .getDefaultFileExtension ()
131
+
132
+ def expressionContext (self , alg ):
133
+ return _expressionContext (alg )
85
134
86
135
def typeName (self ):
87
136
return self .__class__ .__name__ .replace ('Output' , '' ).lower ()
@@ -93,7 +142,9 @@ def tr(self, string, context=''):
93
142
94
143
95
144
class OutputDirectory (Output ):
96
- directory = True
145
+
146
+ def resolveValue (self , alg ):
147
+ self .value = getTempDirInTempFolder ()
97
148
98
149
99
150
class OutputExtent (Output ):
@@ -118,10 +169,7 @@ def setValue(self, value):
118
169
class OutputCrs (Output ):
119
170
120
171
def __init__ (self , name = '' , description = '' ):
121
- self .name = name
122
- self .description = description
123
- self .value = None
124
- self .hidden = True
172
+ Output .__init__ (self , name , description , True )
125
173
126
174
127
175
class OutputFile (Output ):
@@ -136,7 +184,7 @@ def getFileFilter(self, alg):
136
184
else :
137
185
return self .tr ('%s files(*.%s)' , 'OutputFile' ) % (self .ext , self .ext )
138
186
139
- def getDefaultFileExtension (self , alg ):
187
+ def getDefaultFileExtension (self ):
140
188
return self .ext or 'file'
141
189
142
190
@@ -145,17 +193,14 @@ class OutputHTML(Output):
145
193
def getFileFilter (self , alg ):
146
194
return self .tr ('HTML files(*.html)' , 'OutputHTML' )
147
195
148
- def getDefaultFileExtension (self , alg ):
196
+ def getDefaultFileExtension (self ):
149
197
return 'html'
150
198
151
199
152
200
class OutputNumber (Output ):
153
201
154
202
def __init__ (self , name = '' , description = '' ):
155
- self .name = name
156
- self .description = description
157
- self .value = None
158
- self .hidden = True
203
+ Output .__init__ (self , name , description , True )
159
204
160
205
161
206
class OutputRaster (Output ):
@@ -168,11 +213,8 @@ def getFileFilter(self, alg):
168
213
exts [i ] = self .tr ('%s files (*.%s)' , 'OutputVector' ) % (exts [i ].upper (), exts [i ].lower ())
169
214
return ';;' .join (exts )
170
215
171
- def getDefaultFileExtension (self , alg ):
172
- supported = alg .provider .getSupportedOutputRasterLayerExtensions ()
173
- default = ProcessingConfig .getSetting (ProcessingConfig .DEFAULT_OUTPUT_RASTER_LAYER_EXT )
174
- ext = default if default in supported else supported [0 ]
175
- return ext
216
+ def getDefaultFileExtension (self ):
217
+ return ProcessingConfig .getSetting (ProcessingConfig .DEFAULT_OUTPUT_RASTER_LAYER_EXT )
176
218
177
219
def getCompatibleFileName (self , alg ):
178
220
"""
@@ -189,18 +231,17 @@ def getCompatibleFileName(self, alg):
189
231
return self .value
190
232
else :
191
233
if self .compatible is None :
192
- self .compatible = getTempFilenameInTempFolder (
193
- self .name + '.' + self .getDefaultFileExtension (alg ))
234
+ supported = alg .provider .getSupportedOutputRasterLayerExtensions ()
235
+ default = ProcessingConfig .getSetting (ProcessingConfig .DEFAULT_OUTPUT_RASTER_LAYER_EXT )
236
+ ext = default if default in supported else supported [0 ]
237
+ self .compatible = getTempFilenameInTempFolder (self .name + '.' + ext )
194
238
return self .compatible
195
239
196
240
197
241
class OutputString (Output ):
198
242
199
243
def __init__ (self , name = '' , description = '' ):
200
- self .name = name
201
- self .description = description
202
- self .value = None
203
- self .hidden = True
244
+ Output .__init__ (self , name , description , True )
204
245
205
246
206
247
class OutputTable (Output ):
@@ -209,13 +250,13 @@ class OutputTable(Output):
209
250
compatible = None
210
251
211
252
def getFileFilter (self , alg ):
212
- exts = ['csv ' ]
253
+ exts = ['dbf ' ]
213
254
for i in range (len (exts )):
214
255
exts [i ] = exts [i ].upper () + ' files(*.' + exts [i ].lower () + ')'
215
256
return ';;' .join (exts )
216
257
217
- def getDefaultFileExtension (self , alg ):
218
- return alg . provider . getSupportedOutputTableExtensions ()[ 0 ]
258
+ def getDefaultFileExtension (self ):
259
+ return "dbf"
Collapse comment Comment on line R259
Code has comments. Press enter to view. 219
260
220
261
def getCompatibleFileName (self , alg ):
221
262
"""Returns a filename that is compatible with the algorithm
@@ -233,7 +274,7 @@ def getCompatibleFileName(self, alg):
233
274
else :
234
275
if self .compatible is None :
235
276
self .compatible = getTempFilenameInTempFolder (
236
- self .name + '.' + self . getDefaultFileExtension ( alg ) )
277
+ self .name + '.' + alg . provider . getSupportedOutputTableExtensions ()[ 0 ] )
237
278
return self .compatible
238
279
239
280
def getTableWriter (self , fields ):
@@ -286,14 +327,13 @@ def getFileFilter(self, alg):
286
327
exts [i ] = self .tr ('%s files (*.%s)' , 'OutputVector' ) % (exts [i ].upper (), exts [i ].lower ())
287
328
return ';;' .join (exts )
288
329
289
- def getDefaultFileExtension (self , alg ):
290
- supported = alg .provider .getSupportedOutputVectorLayerExtensions ()
330
+ def getDefaultFileExtension (self ):
291
331
if self .hasGeometry ():
292
332
default = ProcessingConfig .getSetting (ProcessingConfig .DEFAULT_OUTPUT_VECTOR_LAYER_EXT )
293
333
else :
294
334
default = 'dbf'
295
- ext = default if default in supported else supported [ 0 ]
296
- return ext
335
+ return default
336
+
297
337
298
338
def getCompatibleFileName (self , alg ):
299
339
"""Returns a filename that is compatible with the algorithm
@@ -309,8 +349,10 @@ def getCompatibleFileName(self, alg):
309
349
return self .value
310
350
else :
311
351
if self .compatible is None :
312
- self .compatible = getTempFilenameInTempFolder (
313
- self .name + '.' + self .getDefaultFileExtension (alg ))
352
+ default = self .getDefaultFileExtension ()
353
+ supported = alg .provider .getSupportedOutputVectorLayerExtensions ()
354
+ ext = default if default in supported else supported [0 ]
355
+ self .compatible = getTempFilenameInTempFolder (self .name + '.' + ext )
314
356
return self .compatible
315
357
316
358
def getVectorWriter (self , fields , geomType , crs , options = None ):
@@ -345,7 +387,13 @@ def getVectorWriter(self, fields, geomType, crs, options=None):
345
387
def dataType (self ):
346
388
return dataobjects .vectorDataType (self )
347
389
348
-
390
+ def _resolveTemporary (self , alg ):
391
+ if alg .provider .supportsNonFileBasedOutput ():
392
+ return "memory:"
393
+ else :
394
+ ext = self .getDefaultFileExtension ()
395
+ return getTempFilenameInTempFolder (self .name + '.' + ext )
396
+
349
397
350
398
def getOutputFromString (s ):
351
399
try :
0 commit comments