Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
finished conversion of models into python code
git-svn-id: http://sextante.googlecode.com/svn/trunk/soft/bindings/qgis-plugin@46 881b9c09-3ef8-f3c2-ec3d-21d735c97f4d
  • Loading branch information
volayaf committed Mar 18, 2012
1 parent 50a8ce3 commit b7d940e
Show file tree
Hide file tree
Showing 12 changed files with 113 additions and 32 deletions.
3 changes: 1 addition & 2 deletions src/sextante/core/AlgorithmProvider.py
Expand Up @@ -2,7 +2,6 @@
import os
from PyQt4 import QtGui
class AlgorithmProvider():

def __init__(self):
name = "ACTIVATE_" + self.getName().upper().replace(" ", "_")
SextanteConfig.addSetting(Setting(self.getName(), name, "Activate", True))
Expand All @@ -20,7 +19,7 @@ def loadAlgorithms(self):
else:
self._loadAlgorithms()

#methods to be overriden.
#methods to be overridden.
#==============================

#Algorithm loading should take place here
Expand Down
2 changes: 1 addition & 1 deletion src/sextante/core/QGisLayers.py
Expand Up @@ -24,7 +24,7 @@ def getVectorLayers(shapetype=-1):
layers = QGisLayers.iface.legendInterface().layers()
vector = list()
for layer in layers:
if layer.type() == layer.VectorLayer :
if layer.type() == layer.VectorLayer:
if shapetype == QGisLayers.ALL_TYPES or layer.geometryType() == shapetype:
vector.append(layer)
return vector
Expand Down
1 change: 0 additions & 1 deletion src/sextante/gui/ParametersDialog.py
Expand Up @@ -255,7 +255,6 @@ def setParamValue(self, param, widget):
value.append(options[index])
return param.setValue(value)
else:

return param.setValue(str(widget.text()))


Expand Down
95 changes: 82 additions & 13 deletions src/sextante/modeler/ModelerAlgorithm.py
Expand Up @@ -7,12 +7,15 @@
from sextante.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
import os.path
from sextante.parameters.ParameterMultipleInput import ParameterMultipleInput
from sextante.outputs.OutputRaster import OutputRaster
from sextante.outputs.OutputHTML import OutputHTML
from sextante.outputs.OutputTable import OutputTable
from sextante.outputs.OutputVector import OutputVector

class ModelerAlgorithm(GeoAlgorithm):

def __deepcopy__(self,memo):
newone = ModelerAlgorithm()
#newone.__dict__.update(self.__dict__)
newone.algs = copy.deepcopy(self.algs, memo)
newone.algParameters = copy.deepcopy(self.algParameters,memo)
newone.algOutputs = copy.deepcopy(self.algOutputs,memo)
Expand Down Expand Up @@ -192,7 +195,7 @@ def prepareAlgorithm(self, alg, iAlg):
layerslist = []
for token in tokens:
i, paramname = token.split("|")
aap = AlgorithmAndParameter(i, paramname)
aap = AlgorithmAndParameter(int(i), paramname)
value = self.getValueFromAlgorithmAndParameter(aap)
layerslist.append(str(value))
value = ";".join(layerslist)
Expand All @@ -203,12 +206,12 @@ def prepareAlgorithm(self, alg, iAlg):
if not param.setValue(value):
raise GeoAlgorithmExecutionException("Wrong value: " + str(value))
for out in alg.outputs:
val = self.algOutputs[iAlg][out.name]
if val:
name = str(iAlg) + out.name
out.value = self.getOutputFromName(name).value
else:
out.value = None
val = self.algOutputs[iAlg][out.name]
if val:
name = str(iAlg) + out.name
out.value = self.getOutputFromName(name).value
else:
out.value = None


def getValueFromAlgorithmAndParameter(self, aap):
Expand All @@ -220,7 +223,7 @@ def getValueFromAlgorithmAndParameter(self, aap):
if aap.param == param.name:
return param.value
else:
return self.producedOutputs[aap.alg][aap.param]
return self.producedOutputs[int(aap.alg)][aap.param]

def processAlgorithm(self, progress):
self.producedOutputs = []
Expand All @@ -242,16 +245,82 @@ def processAlgorithm(self, progress):

progress.setFinished()


def getOutputType(self, i, outname):
for out in self.algs[i].outputs:
if out.name == outname:
if isinstance(out, OutputRaster):
return "output raster"
elif isinstance(out, OutputVector):
return "output vector"
elif isinstance(out, OutputTable):
return "output table"
elif isinstance(out, OutputHTML):
return "output html"


def getAsPythonCode(self):
s = []
for param in self.parameters:
s.append(str(param.getAsScriptCode()))
s.append(str(param.getAsScriptCode().lower()))
i = 0
for outs in self.algOutputs:
for out in outs.keys():
if outs[out]:
s.append("##" + out.lower() + "_alg" + str(i) +"=" + self.getOutputType(i, out))
i += 1
i = 0
iMultiple = 0
for alg in self.algs:
runline = "Sextante.runalg(\"" + alg.commandLineName() + "\n"
#TODO*****
pass
multiple= []
runline = "outputs_" + str(i) + "=Sextante.runalg(\"" + alg.commandLineName() + "\""
for param in alg.parameters:
aap = self.algParameters[i][param.name]
if aap == None:
runline += ", None"

if isinstance(param, ParameterMultipleInput):
value = self.paramValues[aap.param]
tokens = value.split(";")
layerslist = []
for token in tokens:
iAlg, paramname = token.split("|")
if float(iAlg) == float(AlgorithmAndParameter.PARENT_MODEL_ALGORITHM):
if self.ismodelparam(paramname):
value = paramname.lower()
else:
value = self.paramValues[paramname]
else:
value = "outputs_" + str(iAlg) + "['" + paramname +"']"
layerslist.append(str(value))

multiple.append("multiple_" + str(iMultiple) +"=[" + ",".join(layerslist) + "]")
runline +=", \";\".join(multiple_" + str(iMultiple) + ") "
else:
if float(aap.alg) == float(AlgorithmAndParameter.PARENT_MODEL_ALGORITHM):
if self.ismodelparam(aap.param):
runline += ", " + aap.param.lower()
else:
runline += ", " + self.paramValues[aap.param]
else:
runline += ", outputs_" + str(aap.alg) + "['" + aap.param +"']"
for out in alg.outputs:
value = self.algOutputs[i][out.name]
if value:
name = out.name.lower() + "_alg" + str(i)
else:
name = str(None)
runline += ", " + name
i += 1
s += multiple
s.append(str(runline + ")"))
return "\n".join(s)

def ismodelparam(self, paramname):
for modelparam in self.parameters:
if modelparam.name == paramname:
return True
return False

class AlgorithmAndParameter():

Expand Down
3 changes: 2 additions & 1 deletion src/sextante/modeler/ModelerDialog.py
Expand Up @@ -174,7 +174,8 @@ def repaintModel(self):
self.scene.setSceneRect(QtCore.QRectF(0, 0, 1000, 1000))
self.scene.paintModel(self.alg)
self.view.setScene(self.scene)
self.pythonText.setText("This feature is not yet available... we are still working on it ;-)")#self.alg.getAsPythonCode())
#self.pythonText.setText("This feature is not yet available... we are still working on it ;-)")#self.alg.getAsPythonCode())
self.pythonText.setText(self.alg.getAsPythonCode())


def addInput(self):
Expand Down
4 changes: 2 additions & 2 deletions src/sextante/modeler/ModelerParameterDefinitionDialog.py
Expand Up @@ -100,10 +100,10 @@ def setupUi(self):
self.horizontalLayout2.addWidget(self.yesNoCombo)
self.horizontalLayout3.addWidget(QtGui.QLabel("Shape type"))
self.shapetypeCombo = QtGui.QComboBox()
self.shapetypeCombo.addItem("Any")
self.shapetypeCombo.addItem("Point")
self.shapetypeCombo.addItem("Line")
self.shapetypeCombo.addItem("Polygon")
self.shapetypeCombo.addItem("Any")
self.horizontalLayout3.addWidget(self.shapetypeCombo)
self.verticalLayout.addLayout(self.horizontalLayout3)
self.verticalLayout.addLayout(self.horizontalLayout2)
Expand Down Expand Up @@ -174,7 +174,7 @@ def okPressed(self):
elif self.paramType == ModelerParameterDefinitionDialog.PARAMETER_VECTOR:
self.param = ParameterVector(name, description, self.shapetypeCombo.currentIndex()-1, self.yesNoCombo.currentIndex() == 1)
elif self.paramType == ModelerParameterDefinitionDialog.PARAMETER_MULTIPLE:
self.param = ParameterMultipleInput(name, description, self.datatypeCombo.currentIndex(), self.yesNoCombo.currentIndex() == 1)
self.param = ParameterMultipleInput(name, description, self.datatypeCombo.currentIndex()-1, self.yesNoCombo.currentIndex() == 1)
elif self.paramType == ModelerParameterDefinitionDialog.PARAMETER_NUMBER:
try:
vmin = str(self.minTextBox.text()).strip()
Expand Down
4 changes: 2 additions & 2 deletions src/sextante/modeler/ModelerScene.py
Expand Up @@ -35,8 +35,8 @@ def getLastAlgorithmItem(self):

def getItemsFromAAP(self, aap, isMultiple):
items = []
start = aap.alg
if start == AlgorithmAndParameter.PARENT_MODEL_ALGORITHM:
start = int(aap.alg)
if aap.alg == AlgorithmAndParameter.PARENT_MODEL_ALGORITHM:
if isMultiple:
multi = self.model.paramValues[aap.param]
tokens = multi.split(";")
Expand Down
6 changes: 6 additions & 0 deletions src/sextante/parameters/ParameterRaster.py
Expand Up @@ -11,6 +11,12 @@ def __init__(self, name="", description="", optional=False):
self.value = None

def setValue(self, obj):
if obj == None:
if self.optional:
self.value = None
return True
else:
return False
if isinstance(obj, QgsRasterLayer):
self.value = str(obj.dataProvider().dataSourceUri())
return True
Expand Down
6 changes: 6 additions & 0 deletions src/sextante/parameters/ParameterTable.py
Expand Up @@ -11,6 +11,12 @@ def __init__(self, name="", description="", optional=False):
self.value = None

def setValue(self, obj):
if obj == None:
if self.optional:
self.value = None
return True
else:
return False
if isinstance(obj, QgsVectorLayer):
self.value = str(obj.source())
if self.value.endswith("shp"):
Expand Down
6 changes: 6 additions & 0 deletions src/sextante/parameters/ParameterVector.py
Expand Up @@ -18,6 +18,12 @@ def __init__(self, name="", description="", shapetype=-1, optional=False):
self.value = None

def setValue(self, obj):
if obj == None:
if self.optional:
self.value = None
return True
else:
return False
if isinstance(obj, QgsVectorLayer):
self.value = str(obj.source())
return True
Expand Down
1 change: 1 addition & 0 deletions src/sextante/saga/SagaAlgorithm.py
Expand Up @@ -30,6 +30,7 @@
from sextante.outputs.OutputFactory import OutputFactory
from sextante.core.SextanteConfig import SextanteConfig
from sextante.core.QGisLayers import QGisLayers
from PyQt4 import QtGui

class SagaAlgorithm(GeoAlgorithm):

Expand Down
14 changes: 4 additions & 10 deletions src/sextante/script/ScriptAlgorithm.py
Expand Up @@ -14,6 +14,7 @@
from sextante.parameters.ParameterSelection import ParameterSelection
from PyQt4 import QtGui
from sextante.parameters.ParameterTableField import ParameterTableField
from sextante.outputs.OutputHTML import OutputHTML

class ScriptAlgorithm(GeoAlgorithm):

Expand Down Expand Up @@ -89,16 +90,13 @@ def processParameterLine(self,line):
param = ParameterString(tokens[0], desc, default)
elif tokens[1].lower().strip().startswith("output raster"):
out = OutputRaster()
if tokens[1].strip().endswith("*"):
self.silentOutputs.append(tokens[0])
elif tokens[1].lower().strip().startswith("output vector"):
out = OutputVector()
if tokens[1].strip().endswith("*"):
self.silentOutputs.append(tokens[0])
elif tokens[1].lower().strip().startswith("output table"):
out = OutputTable()
if tokens[1].strip().endswith("*"):
self.silentOutputs.append(tokens[0])
elif tokens[1].lower().strip().startswith("output html"):
out = OutputHTML()


if param != None:
self.addParameter(param)
Expand All @@ -121,8 +119,4 @@ def processAlgorithm(self, progress):
script+=self.script
exec(script)

for out in self.outputs:
if out.name in self.silentOutputs:
out.value = None


0 comments on commit b7d940e

Please sign in to comment.