Skip to content

Commit bd06316

Browse files
committedOct 5, 2016
[processing] richer expressions in number parameters
1 parent 96406e5 commit bd06316

File tree

2 files changed

+63
-9
lines changed

2 files changed

+63
-9
lines changed
 

‎python/plugins/processing/core/parameters.py

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@
4141

4242
from processing.tools.vector import resolveFieldIndex, features
4343
from processing.tools import dataobjects
44-
from processing.core.outputs import OutputNumber
44+
from processing.core.outputs import OutputNumber, OutputRaster, OutputVector
45+
from processing.tools.dataobjects import getObject
4546

4647
def parseBool(s):
4748
if s is None or s == str(None).lower():
@@ -863,22 +864,42 @@ def evaluate(self, alg):
863864
if isinstance(self.value, basestring):
864865
self.value = self._evaluate(self.value)
865866

867+
def _layerVariables(self, element, alg=None):
868+
variables = {}
869+
layer = getObject(element.value)
870+
if layer is not None:
871+
name = element.name if alg is None else "%s_%s" % (alg.name, element.name)
872+
variables['@%s_minx' % name] = layer.extent().xMinimum()
873+
variables['@%s_miny' % name] = layer.extent().yMinimum()
874+
variables['@%s_maxx' % name] = layer.extent().yMaximum()
875+
variables['@%s_maxy' % name]= layer.extent().yMaximum()
876+
if isinstance(element, (ParameterRaster, OutputRaster)):
877+
stats = layer.dataProvider().bandStatistics(1)
878+
variables['@%s_avg' % name] = stats.mean
879+
variables['@%s_stddev' % name] = stats.stdDev
880+
variables['@%s_min' % name] = stats.minimumValue
881+
variables['@%s_max' % name] = stats.maximumValue
882+
return variables
883+
866884
def evaluateForModeler(self, value, model):
867885
if isinstance(value, numbers.Number):
868886
return value
869887
variables = {}
870888
for param in model.parameters:
871889
if isinstance(param, ParameterNumber):
872890
variables["@" + param.name] = param.value
891+
if isinstance(param, (ParameterRaster, ParameterVector)):
892+
variables.update(self._layerVariables(param))
893+
873894
for alg in model.algs.values():
874895
for out in alg.algorithm.outputs:
875896
if isinstance(out, OutputNumber):
876897
variables["@%s_%s" % (alg.name, out.name)] = out.value
898+
if isinstance(out, (OutputRaster, OutputVector)):
899+
variables.update(self._layerVariables(out, alg))
877900
for k,v in variables.iteritems():
878-
print k,v
879901
value = value.replace(k,unicode(v))
880902

881-
print value
882903
return value
883904

884905
def expressionContext(self):

‎python/plugins/processing/gui/NumberInputPanel.py

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@
3939
QgsExpressionContextScope)
4040
from qgis.gui import QgsEncodingFileDialog, QgsExpressionBuilderDialog
4141
from qgis.utils import iface
42-
from processing.core.parameters import ParameterNumber
43-
from processing.core.outputs import OutputNumber
42+
from processing.core.parameters import ParameterNumber, ParameterVector, ParameterRaster
43+
from processing.core.outputs import OutputNumber, OutputVector, OutputRaster
4444
from processing.modeler.ModelerAlgorithm import ValueFromInput, ValueFromOutput, CompoundValue
4545

4646
pluginPath = os.path.split(os.path.dirname(__file__))[0]
@@ -68,12 +68,45 @@ def showExpressionsBuilder(self):
6868
if self.modelParametersDialog is not None:
6969
context.popScope()
7070
values = self.modelParametersDialog.getAvailableValuesOfType(ParameterNumber, OutputNumber)
71-
modelerScope = QgsExpressionContextScope()
71+
variables = {}
7272
for value in values:
73-
name = value.name if isinstance(value, ValueFromInput) else "%s_%s" % (value.alg, value.output)
74-
modelerScope.setVariable(name, 1)
75-
context.appendScope(modelerScope)
73+
if isinstance(value, ValueFromInput):
74+
name = value.name
75+
element = self.modelParametersDialog.model.inputs[name].param
76+
desc = element.description
77+
else:
78+
name = "%s_%s" % (value.alg, value.output)
79+
alg = self.modelParametersDialog.model.algs[value.alg]
80+
out = alg.algorithm.getOutputFromName(value.output)
81+
desc = "Output '%s' from algorithm '%s" % (out.description, alg.description)
82+
variables[name] = desc
83+
values = self.modelParametersDialog.getAvailableValuesOfType(ParameterVector, OutputVector)
84+
values.extend(self.modelParametersDialog.getAvailableValuesOfType(ParameterRaster, OutputRaster))
85+
for value in values:
86+
if isinstance(value, ValueFromInput):
87+
name = value.name
88+
element = self.modelParametersDialog.model.inputs[name].param
89+
desc = element.description
90+
else:
91+
name = "%s_%s" % (value.alg, value.output)
92+
alg = self.modelParametersDialog.model.algs[value.alg]
93+
element = alg.algorithm.getOutputFromName(value.output)
94+
desc = "Output '%s' from algorithm '%s" % (element.description, alg.description)
95+
variables['%s_minx' % name] = "Minimum X of %s" % desc
96+
variables['%s_miny' % name] = "Maximum X of %s" % desc
97+
variables['%s_maxx' % name] = "Minimum Y of %s" % desc
98+
variables['%s_maxy' % name] = "Maximum Y of %s" % desc
99+
if isinstance(element, (ParameterRaster, OutputRaster)):
100+
variables['%s_min' % name] = "Minimum value of %s" % desc
101+
variables['%s_max' % name] = "Maximum value of %s" % desc
102+
variables['%s_avg' % name] = "Mean value of %s" % desc
103+
variables['%s_stddev' % name] = "Standard deviation of %s" % desc
104+
105+
#context.appendScope(modelerScope)
106+
#context.setHighlightedVariables(modelerScope.variableNames())
76107
dlg = QgsExpressionBuilderDialog(None, self.leText.text(), self, 'generic', context)
108+
for variable, desc in variables.iteritems():
109+
dlg.expressionBuilder().registerItem("Modeler", variable, "@" + variable, desc, highlightedItem=True)
77110
dlg.setWindowTitle(self.tr('Expression based input'))
78111
if dlg.exec_() == QDialog.Accepted:
79112
exp = QgsExpression(dlg.expressionText())

0 commit comments

Comments
 (0)
Please sign in to comment.