Skip to content

Commit f1c95a9

Browse files
committed
Grass bindings improvements in SEXTANTE
Fixed minor bugs in SAGA an GDAL merge algorithms
1 parent fb51c27 commit f1c95a9

File tree

7 files changed

+124
-100
lines changed

7 files changed

+124
-100
lines changed

python/plugins/sextante/gdal/merge.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,12 @@ def processAlgorithm(self, progress):
6060
commands.append("-separate")
6161
if self.getParameterValue(merge.PCT):
6262
commands.append("-pct")
63-
commands.append("-of")
63+
commands.append("-o")
6464
out = self.getOutputValue(merge.OUTPUT)
65+
commands.append(out)
66+
commands.append("-of")
6567
commands.append(GdalUtils.getFormatShortNameFromFilename(out))
6668
commands.append(self.getParameterValue(merge.INPUT).replace(";", " "))
67-
commands.append(out)
69+
6870

6971
GdalUtils.runGdal(commands, progress)

python/plugins/sextante/grass/GrassAlgorithm.py

+57-22
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@
5353
from sextante.parameters.ParameterNumber import ParameterNumber
5454

5555
class GrassAlgorithm(GeoAlgorithm):
56+
57+
5658

5759
GRASS_REGION_EXTENT_PARAMETER = "GRASS_REGION_PARAMETER"
5860
GRASS_REGION_CELLSIZE_PARAMETER = "GRASS_REGION_CELLSIZE_PARAMETER"
@@ -62,6 +64,8 @@ def __init__(self, descriptionfile):
6264
self.descriptionFile = descriptionfile
6365
self.defineCharacteristicsFromFile()
6466
self.numExportedLayers = 0
67+
#GRASS console output, needed to do postprocessing in case GRASS dumps results to the console
68+
self.consoleOutput = []
6569

6670
def getCopy(self):
6771
newone = GrassAlgorithm(self.descriptionFile)
@@ -153,8 +157,9 @@ def getDefaultCellsize(self):
153157

154158
if cellsize == 0:
155159
cellsize = 1
156-
return cellsize
157-
160+
return cellsize
161+
162+
158163
def processAlgorithm(self, progress):
159164
if SextanteUtils.isWindows():
160165
path = GrassUtils.grassPath()
@@ -163,6 +168,7 @@ def processAlgorithm(self, progress):
163168

164169
commands = []
165170
self.exportedLayers = {}
171+
outputCommands = []
166172

167173
# if GRASS session has been created outside of this algorithm then get the list of layers loaded in GRASS
168174
# otherwise start a new session
@@ -171,21 +177,7 @@ def processAlgorithm(self, progress):
171177
self.exportedLayers = GrassUtils.getSessionLayers()
172178
else:
173179
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+
189181

190182
#1: Export layer to grass mapset
191183
for param in self.parameters:
@@ -197,6 +189,7 @@ def processAlgorithm(self, progress):
197189
if value in self.exportedLayers.keys():
198190
continue
199191
else:
192+
self.setSessionProjection(value, commands)
200193
commands.append(self.exportRasterLayer(value))
201194
if isinstance(param, ParameterVector):
202195
if param.value == None:
@@ -205,6 +198,7 @@ def processAlgorithm(self, progress):
205198
if value in self.exportedLayers.keys():
206199
continue
207200
else:
201+
self.setSessionProjection(value, commands)
208202
commands.append(self.exportVectorLayer(value))
209203
if isinstance(param, ParameterTable):
210204
pass
@@ -219,14 +213,29 @@ def processAlgorithm(self, progress):
219213
if layer in self.exportedLayers.keys():
220214
continue
221215
else:
216+
self.setSessionProjection(value, commands)
222217
commands.append(self.exportRasterLayer(layer))
223218
elif param.datatype == ParameterMultipleInput.TYPE_VECTOR_ANY:
224219
for layer in layers:
225220
if layer in self.exportedLayers.keys():
226221
continue
227222
else:
223+
self.setSessionProjection(value, commands)
228224
commands.append(self.exportVectorLayer(layer))
229225

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+
230239
#2: set parameters and outputs
231240
command = self.grassName
232241
for param in self.parameters:
@@ -276,11 +285,13 @@ def processAlgorithm(self, progress):
276285
filename = out.value
277286
#Raster layer output: adjust region to layer before exporting
278287
commands.append("g.region rast=" + out.name + uniqueSufix)
288+
outputCommands.append("g.region rast=" + out.name)
279289
command = "r.out.gdal -c createopt=\"TFW=YES,COMPRESS=LZW\""
280290
command += " input="
281291
command += out.name + uniqueSufix
282292
command += " output=\"" + filename + "\""
283-
commands.append(command)
293+
commands.append(command)
294+
outputCommands.append(command)
284295

285296
if isinstance(out, OutputVector):
286297
filename = out.value
@@ -290,6 +301,7 @@ def processAlgorithm(self, progress):
290301
command += " olayer=" + os.path.basename(out.value)[:-4]
291302
command += " type=auto"
292303
commands.append(command)
304+
outputCommands.append("g.region rast=" + out.name)
293305

294306
#4 Run GRASS
295307
loglines = []
@@ -299,14 +311,25 @@ def processAlgorithm(self, progress):
299311
loglines.append(line)
300312
if SextanteConfig.getSetting(GrassUtils.GRASS_LOG_COMMANDS):
301313
SextanteLog.addToLog(SextanteLog.LOG_INFO, loglines)
302-
GrassUtils.executeGrass(commands, progress);
314+
self.consoleOutput = GrassUtils.executeGrass(commands, progress, outputCommands);
315+
self.postProcessResults();
303316
# if the session has been created outside of this algorithm, add the new GRASS layers to it
304317
# otherwise finish the session
305318
if existingSession:
306319
GrassUtils.addSessionLayers(self.exportedLayers)
307-
else:
320+
else:
308321
GrassUtils.endGrassSession()
309322

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+
310333
def exportVectorLayer(self, orgFilename):
311334
#only export to an intermediate shp if the layer is not file-based.
312335
#We assume that almost all file formats will be supported by ogr
@@ -336,13 +359,25 @@ def exportVectorLayer(self, orgFilename):
336359
return command
337360

338361

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+
339374
def exportRasterLayer(self, layer):
340375
destFilename = self.getTempFilename()
341376
self.exportedLayers[layer]= destFilename
342-
command = "r.in.gdal"
377+
command = "r.external"
343378
command +=" input=\"" + layer + "\""
344379
command +=" band=1"
345-
command +=" out=" + destFilename;
380+
command +=" output=" + destFilename;
346381
command +=" --overwrite -o"
347382
return command
348383

python/plugins/sextante/grass/GrassAlgorithmProvider.py

-2
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,13 @@ def initializeSettings(self):
4747
SextanteConfig.addSetting(Setting(self.getDescription(), GrassUtils.GRASS_WIN_SHELL, "Msys folder", GrassUtils.grassWinShell()))
4848
SextanteConfig.addSetting(Setting(self.getDescription(), GrassUtils.GRASS_LOG_COMMANDS, "Log execution commands", False))
4949
SextanteConfig.addSetting(Setting(self.getDescription(), GrassUtils.GRASS_LOG_CONSOLE, "Log console output", False))
50-
SextanteConfig.addSetting(Setting(self.getDescription(), GrassUtils.GRASS_LATLON, "Coordinates are lat/lon", False))
5150
SextanteConfig.addSetting(Setting(self.getDescription(), GrassUtils.GRASS_HELP_FOLDER, "GRASS help folder", GrassUtils.grassHelpPath()))
5251

5352
def unload(self):
5453
AlgorithmProvider.unload(self)
5554
if SextanteUtils.isWindows() or SextanteUtils.isMac():
5655
SextanteConfig.removeSetting(GrassUtils.GRASS_FOLDER)
5756
SextanteConfig.removeSetting(GrassUtils.GRASS_WIN_SHELL)
58-
SextanteConfig.removeSetting(GrassUtils.GRASS_LATLON)
5957
SextanteConfig.removeSetting(GrassUtils.GRASS_HELP_FOLDER)
6058
SextanteConfig.removeSetting(GrassUtils.GRASS_LOG_COMMANDS)
6159
SextanteConfig.removeSetting(GrassUtils.GRASS_LOG_CONSOLE)

0 commit comments

Comments
 (0)