17
17
from sextante .outputs .OutputTable import OutputTable
18
18
from sextante .outputs .OutputHTML import OutputHTML
19
19
import copy
20
+ from sextante .core .SextanteVectorWriter import SextanteVectorWriter
21
+ from sextante .core .SextanteConfig import SextanteConfig
22
+ from sextante .gdal .GdalUtils import GdalUtils
20
23
21
24
class GeoAlgorithm :
22
25
@@ -103,18 +106,23 @@ def checkParameterValuesBeforeExecuting(self):
103
106
This check is called from the parameters dialog, and also when calling from the console'''
104
107
return None
105
108
#=========================================================
106
-
109
+
110
+
107
111
def execute (self , progress ):
108
112
'''The method to use to call a SEXTANTE algorithm.
109
113
Although the body of the algorithm is in processAlgorithm(),
110
114
it should be called using this method, since it performs
111
115
some additional operations.
112
116
Raises a GeoAlgorithmExecutionException in case anything goes wrong.'''
113
- self .setOutputCRSFromInputLayers ()
114
- self .resolveTemporaryOutputs ()
115
- self .checkOutputFileExtensions ()
117
+
116
118
try :
117
- self .processAlgorithm (progress )
119
+ self .setOutputCRSFromInputLayers ()
120
+ self .resolveTemporaryOutputs ()
121
+ self .checkOutputFileExtensions ()
122
+ self .runPreExecutionScript (progress )
123
+ self .processAlgorithm (progress )
124
+ self .convertUnsupportedFormats (progress )
125
+ self .runPostExecutionScript (progress )
118
126
except GeoAlgorithmExecutionException , gaee :
119
127
SextanteLog .addToLog (SextanteLog .LOG_ERROR , gaee .msg )
120
128
raise gaee
@@ -132,6 +140,62 @@ def execute(self, progress):
132
140
SextanteLog .addToLog (SextanteLog .LOG_ERROR , lines )
133
141
raise GeoAlgorithmExecutionException (errstring )
134
142
143
+
144
+ def runPostExecutionScript (self , progress ):
145
+ scriptFile = SextanteConfig .getSetting (SextanteConfig .POST_EXECUTION_SCRIPT )
146
+ self .runHookScript (scriptFile , progress );
147
+
148
+ def runPreExecutionScript (self , progress ):
149
+ scriptFile = SextanteConfig .getSetting (SextanteConfig .PRE_EXECUTION_SCRIPT )
150
+ self .runHookScript (scriptFile , progress );
151
+
152
+ def runHookScript (self , filename , progress ):
153
+ if not os .path .exists (filename ):
154
+ return
155
+ try :
156
+ script = "import sextante\n "
157
+ ns = {}
158
+ ns ['progress' ] = progress
159
+ ns ['alg' ] = self
160
+ f = open (filename )
161
+ lines = f .readlines ()
162
+ for line in lines :
163
+ script += line
164
+ exec (script ) in ns
165
+ except : # a wrong script should not cause problems, so we swallow all exceptions
166
+ pass
167
+
168
+ def convertUnsupportedFormats (self , progress ):
169
+ i = 0
170
+ progress .setText ("Converting outputs" )
171
+ for out in self .outputs :
172
+ if isinstance (out , OutputVector ):
173
+ if out .compatible is not None :
174
+ layer = QGisLayers .getObjectFromUri (out .compatible )
175
+ provider = layer .dataProvider ()
176
+ writer = out .getVectorWriter ( provider .fields (), provider .geometryType (), provider .crs ())
177
+ features = QGisLayers .features (layer )
178
+ for feature in features :
179
+ writer .addFeature (feature )
180
+ elif isinstance (out , OutputRaster ):
181
+ if out .compatible is not None :
182
+ layer = QGisLayers .getObjectFromUri (out .compatible )
183
+ provider = layer .dataProvider ()
184
+ writer = QgsRasterFileWriter (out .value )
185
+ format = self .getFormatShortNameFromFilename (out .value )
186
+ writer .setOutputFormat (format );
187
+ writer .writeRaster (layer .pipe (), layer .width (), layer .height (), layer .extent (), layer .crs ())
188
+ progress .setPercentage (100 * i / float (len (self .outputs )))
189
+
190
+ def getFormatShortNameFromFilename (self , filename ):
191
+ ext = filename [filename .rfind ("." )+ 1 :]
192
+ supported = GdalUtils .getSupportedRasters ()
193
+ for name in supported .keys ():
194
+ exts = supported [name ]
195
+ if ext in exts :
196
+ return name
197
+ return "GTiff"
198
+
135
199
def checkOutputFileExtensions (self ):
136
200
'''Checks if the values of outputs are correct and have one of the supported output extensions.
137
201
If not, it adds the first one of the supported extensions, which is assumed to be the default one'''
@@ -140,11 +204,11 @@ def checkOutputFileExtensions(self):
140
204
if not os .path .isabs (out .value ):
141
205
continue
142
206
if isinstance (out , OutputRaster ):
143
- exts = self . provider .getSupportedOutputRasterLayerExtensions ()
207
+ exts = QGisLayers .getSupportedOutputRasterLayerExtensions ()
144
208
elif isinstance (out , OutputVector ):
145
- exts = self . provider .getSupportedOutputVectorLayerExtensions ()
209
+ exts = QGisLayers .getSupportedOutputVectorLayerExtensions ()
146
210
elif isinstance (out , OutputTable ):
147
- exts = self . provider .getSupportedOutputTableExtensions ()
211
+ exts = QGisLayers .getSupportedOutputTableExtensions ()
148
212
elif isinstance (out , OutputHTML ):
149
213
exts = ["html" , "htm" ]
150
214
else :
0 commit comments