Skip to content
Permalink
Browse files

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
volayaf committed Mar 18, 2012
1 parent 50a8ce3 commit b7d940ef318cf9f5d09fcf038dc3eb8855df4907
@@ -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))
@@ -20,7 +19,7 @@ def loadAlgorithms(self):
else:
self._loadAlgorithms()

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

#Algorithm loading should take place here
@@ -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
@@ -255,7 +255,6 @@ def setParamValue(self, param, widget):
value.append(options[index])
return param.setValue(value)
else:

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


@@ -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)
@@ -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)
@@ -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):
@@ -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 = []
@@ -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():

@@ -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):
@@ -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)
@@ -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()
@@ -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(";")
@@ -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
@@ -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"):
@@ -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
@@ -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):

@@ -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):

@@ -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)
@@ -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.