Skip to content

Commit

Permalink
added more functionality to iterative execution
Browse files Browse the repository at this point in the history
fixed minor bugs

git-svn-id: http://sextante.googlecode.com/svn/trunk/soft/bindings/qgis-plugin@145 881b9c09-3ef8-f3c2-ec3d-21d735c97f4d
  • Loading branch information
volayaf committed Apr 23, 2012
1 parent 12d6f65 commit d933f2b
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 37 deletions.
4 changes: 2 additions & 2 deletions src/sextante/core/GeoAlgorithm.py
Expand Up @@ -144,7 +144,7 @@ def resolveTemporaryOutputs(self):
def setOutputCRSFromInputLayers(self):
layers = QGisLayers.getAllLayers()
for param in self.parameters:
if isinstance(param, (ParameterRaster, ParameterVector ,ParameterMultipleInput)):
if isinstance(param, (ParameterRaster, ParameterVector, ParameterMultipleInput)):
inputlayers = param.value.split(";")
for inputlayer in inputlayers:
for layer in layers:
Expand All @@ -166,7 +166,7 @@ def addParameter(self, param):
def setParameterValue(self, paramName, value):
for param in self.parameters:
if param.name == paramName:
param.value = value
return param.setValue(value)

def setOutputValue(self, outputName, value):
for out in self.outputs:
Expand Down
42 changes: 40 additions & 2 deletions src/sextante/gui/AlgorithmExecutor.py
@@ -1,5 +1,7 @@
from PyQt4.QtGui import *
from sextante.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
from sextante.core.QGisLayers import QGisLayers
from sextante.core.SextanteUtils import SextanteUtils

class AlgorithmExecutor:

Expand All @@ -16,8 +18,44 @@ def runalg(alg, progress):
return False

@staticmethod
def runalgIterating(alg,paramtoIter,progress):
return False
def runalgIterating(alg,paramToIter,progress):
#generate all single-feature layers
settings = QSettings()
systemEncoding = settings.value( "/UI/encoding", "System" ).toString()
layerfile = alg.getParameterFromName(paramToIter)
layer = QGisLayers.getObjectFromUri(layerfile, False)
provider = layer.dataProvider()
allAttrs = provider.attributeIndexes()
provider.select( allAttrs )
feat = QgsFeature()
filelist = []
outputs = {}
while provider.nextFeature(feat):
output = SextanteUtils.getTempFilename("shp")
filelist.append(output)
writer = QgsVectorFileWriter(output, systemEncoding,provider.fields(), provider.geometryType(), provider.crs() )
writer.addFeature(feat)
del writer
#now run all the algorithms
for out in alg.outputs:
output[out.name] = out.value

i = 1
for f in filelist:
alg.setOutputValue(paramToIter, f)
for out in alg.outputs:
filename = outputs[out.name]
if filename:
filename = filename[:filename.rfind(".")] + "_" + str(i) + filename[filename.rfind("."):]
out.value = filename
if AlgorithmExecutor.runalg(alg, SilentProgress()):
progress.setValue(i/len(f))
i+=1
else:
return False;

return True


class SilentProgress():

Expand Down
5 changes: 3 additions & 2 deletions src/sextante/gui/ParametersDialog.py
Expand Up @@ -135,6 +135,7 @@ def setParamValue(self, param, widget):
def accept(self):
try:
if self.setParamValues():
keepOpen = SextanteConfig.getSetting(SextanteConfig.KEEP_DIALOG_OPEN)
self.buttonBox.button(QtGui.QDialogButtonBox.Ok).setEnabled(False)
buttons = self.paramTable.iterateButtons
iterateParam = None
Expand All @@ -154,10 +155,10 @@ def accept(self):
ret = AlgorithmExecutor.runalg(self.alg, self)
QApplication.restoreOverrideCursor()
if ret:
SextantePostprocessing.handleAlgorithmResults(self.alg)
SextantePostprocessing.handleAlgorithmResults(self.alg, not keepOpen)

self.dialog.executed = True
if not SextanteConfig.getSetting(SextanteConfig.KEEP_DIALOG_OPEN):
if not keepOpen:
self.dialog.close()
else:
self.progressLabel.setText("")
Expand Down
9 changes: 4 additions & 5 deletions src/sextante/gui/SextantePostprocessing.py
Expand Up @@ -9,12 +9,11 @@
from PyQt4.QtGui import *
from sextante.core.SextanteConfig import SextanteConfig
import os
from sextante.outputs.OutputFile import OutputFile
class SextantePostprocessing:

@staticmethod
def handleAlgorithmResults(alg):
showResults = False;
def handleAlgorithmResults(alg, showResults = True):
htmlResults = False;
for out in alg.outputs:
if out.hidden:
continue
Expand All @@ -29,7 +28,7 @@ def handleAlgorithmResults(alg):
QMessageBox.critical(None, "Error", str(e))
elif isinstance(out, OutputHTML):
SextanteResults.addResult(out.description, out.value)
showResults = True
if showResults:
htmlResults = True
if showResults and htmlResults:
dlg = ResultsDialog()
dlg.exec_()
21 changes: 11 additions & 10 deletions src/sextante/modeler/ModelerDialog.py
Expand Up @@ -179,22 +179,23 @@ def saveModel(self):
filename = self.alg.descriptionFile
else:
filename = str(QtGui.QFileDialog.getSaveFileName(self, "Save Model", ModelerUtils.modelsFolder(), "SEXTANTE models (*.model)"))
if not filename.endswith(".model"):
filename += ".model"
self.alg.descriptionFile = filename
if filename:
if not filename.endswith(".model"):
filename += ".model"
self.alg.descriptionFile = filename
if filename:
text = self.alg.serialize()
fout = open(filename, "w")
fout.write(text)
fout.close()
self.update = True

#if help strings were defined before saving the model for the first time, we do it here
if self.help:
f = open(self.alg.descriptionFile + ".help", "wb")
pickle.dump(self.help, f)
f.close()
self.help = None
#if help strings were defined before saving the model for the first time, we do it here
if self.help:
f = open(self.alg.descriptionFile + ".help", "wb")
pickle.dump(self.help, f)
f.close()
self.help = None
QtGui.QMessageBox.information(self, "Model saving", "Model was correctly saved.")

def openModel(self):
filename = QtGui.QFileDialog.getOpenFileName(self, "Open Model", ModelerUtils.modelsFolder(), "SEXTANTE models (*.model)")
Expand Down
17 changes: 9 additions & 8 deletions src/sextante/r/EditRScriptDialog.py
Expand Up @@ -3,6 +3,7 @@
from PyQt4.QtGui import *
from sextante.r.RUtils import RUtils
import pickle
from sextante.gui.HelpEditionDialog import HelpEditionDialog

class EditRScriptDialog(QtGui.QDialog):
def __init__(self, alg):
Expand Down Expand Up @@ -51,14 +52,14 @@ def saveAlgorithm(self):
fout.write(text)
fout.close()
self.update = True
self.close()

#if help strings were defined before saving the model for the first time, we do it here
if self.help:
f = open(self.alg.descriptionFile + ".help", "wb")
pickle.dump(self.help, f)
f.close()
self.help = None
#if help strings were defined before saving the model for the first time, we do it here
if self.help:
f = open(self.alg.descriptionFile + ".help", "wb")
pickle.dump(self.help, f)
f.close()
self.help = None
QtGui.QMessageBox.information(self, "Script saving", "Script was correctly saved.")
#self.close()

def cancelPressed(self):
self.update = False
Expand Down
17 changes: 9 additions & 8 deletions src/sextante/script/EditScriptDialog.py
Expand Up @@ -57,20 +57,21 @@ def saveAlgorithm(self):
filename = self.alg.descriptionFile
else:
filename = QtGui.QFileDialog.getSaveFileName(self, "Save Script", ScriptUtils.scriptsFolder(), "Python scripts (*.py)")

if filename:
text = self.text.toPlainText()
fout = open(filename, "w")
fout.write(text)
fout.close()
self.update = True
self.close()

#if help strings were defined before saving the model for the first time, we do it here
if self.help:
f = open(self.alg.descriptionFile + ".help", "wb")
pickle.dump(self.help, f)
f.close()
self.help = None
#if help strings were defined before saving the model for the first time, we do it here
if self.help:
f = open(self.alg.descriptionFile + ".help", "wb")
pickle.dump(self.help, f)
f.close()
self.help = None
#self.close()
QtGui.QMessageBox.information(self, "Script saving", "Script was correctly saved.")

def cancelPressed(self):
self.update = False
Expand Down

0 comments on commit d933f2b

Please sign in to comment.