53
53
from sextante .parameters .ParameterNumber import ParameterNumber
54
54
55
55
class GrassAlgorithm (GeoAlgorithm ):
56
+
57
+
56
58
57
59
GRASS_REGION_EXTENT_PARAMETER = "GRASS_REGION_PARAMETER"
58
60
GRASS_REGION_CELLSIZE_PARAMETER = "GRASS_REGION_CELLSIZE_PARAMETER"
@@ -62,6 +64,8 @@ def __init__(self, descriptionfile):
62
64
self .descriptionFile = descriptionfile
63
65
self .defineCharacteristicsFromFile ()
64
66
self .numExportedLayers = 0
67
+ #GRASS console output, needed to do postprocessing in case GRASS dumps results to the console
68
+ self .consoleOutput = []
65
69
66
70
def getCopy (self ):
67
71
newone = GrassAlgorithm (self .descriptionFile )
@@ -153,8 +157,9 @@ def getDefaultCellsize(self):
153
157
154
158
if cellsize == 0 :
155
159
cellsize = 1
156
- return cellsize
157
-
160
+ return cellsize
161
+
162
+
158
163
def processAlgorithm (self , progress ):
159
164
if SextanteUtils .isWindows ():
160
165
path = GrassUtils .grassPath ()
@@ -163,6 +168,7 @@ def processAlgorithm(self, progress):
163
168
164
169
commands = []
165
170
self .exportedLayers = {}
171
+ outputCommands = []
166
172
167
173
# if GRASS session has been created outside of this algorithm then get the list of layers loaded in GRASS
168
174
# otherwise start a new session
@@ -171,21 +177,7 @@ def processAlgorithm(self, progress):
171
177
self .exportedLayers = GrassUtils .getSessionLayers ()
172
178
else :
173
179
GrassUtils .startGrassSession ()
174
-
175
-
176
- #self.calculateRegion()
177
- region = str (self .getParameterValue (self .GRASS_REGION_EXTENT_PARAMETER ))
178
- regionCoords = region .split ("," )
179
- command = "g.region"
180
- command += " n=" + str (regionCoords [3 ])
181
- command += " s=" + str (regionCoords [2 ])
182
- command += " e=" + str (regionCoords [1 ])
183
- command += " w=" + str (regionCoords [0 ])
184
- if self .getParameterValue (self .GRASS_REGION_CELLSIZE_PARAMETER ) == 0 :
185
- command += " res=" + str (self .getDefaultCellsize ())
186
- else :
187
- command += " res=" + str (self .getParameterValue (self .GRASS_REGION_CELLSIZE_PARAMETER ));
188
- commands .append (command )
180
+
189
181
190
182
#1: Export layer to grass mapset
191
183
for param in self .parameters :
@@ -197,6 +189,7 @@ def processAlgorithm(self, progress):
197
189
if value in self .exportedLayers .keys ():
198
190
continue
199
191
else :
192
+ self .setSessionProjection (value , commands )
200
193
commands .append (self .exportRasterLayer (value ))
201
194
if isinstance (param , ParameterVector ):
202
195
if param .value == None :
@@ -205,6 +198,7 @@ def processAlgorithm(self, progress):
205
198
if value in self .exportedLayers .keys ():
206
199
continue
207
200
else :
201
+ self .setSessionProjection (value , commands )
208
202
commands .append (self .exportVectorLayer (value ))
209
203
if isinstance (param , ParameterTable ):
210
204
pass
@@ -219,14 +213,29 @@ def processAlgorithm(self, progress):
219
213
if layer in self .exportedLayers .keys ():
220
214
continue
221
215
else :
216
+ self .setSessionProjection (value , commands )
222
217
commands .append (self .exportRasterLayer (layer ))
223
218
elif param .datatype == ParameterMultipleInput .TYPE_VECTOR_ANY :
224
219
for layer in layers :
225
220
if layer in self .exportedLayers .keys ():
226
221
continue
227
222
else :
223
+ self .setSessionProjection (value , commands )
228
224
commands .append (self .exportVectorLayer (layer ))
229
225
226
+ region = str (self .getParameterValue (self .GRASS_REGION_EXTENT_PARAMETER ))
227
+ regionCoords = region .split ("," )
228
+ command = "g.region"
229
+ command += " n=" + str (regionCoords [3 ])
230
+ command += " s=" + str (regionCoords [2 ])
231
+ command += " e=" + str (regionCoords [1 ])
232
+ command += " w=" + str (regionCoords [0 ])
233
+ if self .getParameterValue (self .GRASS_REGION_CELLSIZE_PARAMETER ) == 0 :
234
+ command += " res=" + str (self .getDefaultCellsize ())
235
+ else :
236
+ command += " res=" + str (self .getParameterValue (self .GRASS_REGION_CELLSIZE_PARAMETER ));
237
+ commands .append (command )
238
+
230
239
#2: set parameters and outputs
231
240
command = self .grassName
232
241
for param in self .parameters :
@@ -276,11 +285,13 @@ def processAlgorithm(self, progress):
276
285
filename = out .value
277
286
#Raster layer output: adjust region to layer before exporting
278
287
commands .append ("g.region rast=" + out .name + uniqueSufix )
288
+ outputCommands .append ("g.region rast=" + out .name )
279
289
command = "r.out.gdal -c createopt=\" TFW=YES,COMPRESS=LZW\" "
280
290
command += " input="
281
291
command += out .name + uniqueSufix
282
292
command += " output=\" " + filename + "\" "
283
- commands .append (command )
293
+ commands .append (command )
294
+ outputCommands .append (command )
284
295
285
296
if isinstance (out , OutputVector ):
286
297
filename = out .value
@@ -290,6 +301,7 @@ def processAlgorithm(self, progress):
290
301
command += " olayer=" + os .path .basename (out .value )[:- 4 ]
291
302
command += " type=auto"
292
303
commands .append (command )
304
+ outputCommands .append ("g.region rast=" + out .name )
293
305
294
306
#4 Run GRASS
295
307
loglines = []
@@ -299,14 +311,25 @@ def processAlgorithm(self, progress):
299
311
loglines .append (line )
300
312
if SextanteConfig .getSetting (GrassUtils .GRASS_LOG_COMMANDS ):
301
313
SextanteLog .addToLog (SextanteLog .LOG_INFO , loglines )
302
- GrassUtils .executeGrass (commands , progress );
314
+ self .consoleOutput = GrassUtils .executeGrass (commands , progress , outputCommands );
315
+ self .postProcessResults ();
303
316
# if the session has been created outside of this algorithm, add the new GRASS layers to it
304
317
# otherwise finish the session
305
318
if existingSession :
306
319
GrassUtils .addSessionLayers (self .exportedLayers )
307
- else :
320
+ else :
308
321
GrassUtils .endGrassSession ()
309
322
323
+ def postProcessResults (self ):
324
+ name = self .commandLineName ().replace ('.' ,'_' )
325
+ try :
326
+ module = __import__ ('sextante.grass.postproc.' + name )
327
+ except ImportError :
328
+ return
329
+ if hasattr (module , 'postProcessResults' ):
330
+ func = getattr (module ,'postProcessResults' )
331
+ func (self )
332
+
310
333
def exportVectorLayer (self , orgFilename ):
311
334
#only export to an intermediate shp if the layer is not file-based.
312
335
#We assume that almost all file formats will be supported by ogr
@@ -336,13 +359,25 @@ def exportVectorLayer(self, orgFilename):
336
359
return command
337
360
338
361
362
+ def setSessionProjection (self , layer , commands ):
363
+ if not GrassUtils .projectionSet :
364
+ qGisLayer = QGisLayers .getObjectFromUri (layer )
365
+ if qGisLayer :
366
+ proj4 = str (qGisLayer .crs ().toProj4 ())
367
+ command = "g.proj"
368
+ command += " -c"
369
+ command += " proj4=\" " + proj4 + "\" "
370
+ commands .append (command )
371
+ GrassUtils .projectionSet = True
372
+
373
+
339
374
def exportRasterLayer (self , layer ):
340
375
destFilename = self .getTempFilename ()
341
376
self .exportedLayers [layer ]= destFilename
342
- command = "r.in.gdal "
377
+ command = "r.external "
343
378
command += " input=\" " + layer + "\" "
344
379
command += " band=1"
345
- command += " out =" + destFilename ;
380
+ command += " output =" + destFilename ;
346
381
command += " --overwrite -o"
347
382
return command
348
383
0 commit comments