QGIS/python/plugins/processing/modeler/CalculatorModelerAlgorithm.py

70 lines
2.8 KiB
Python
Raw Normal View History

2012-10-04 19:33:47 +02:00
# -*- coding: utf-8 -*-
"""
***************************************************************************
CalculatorModelerAlgorithm.py
---------------------
Date : August 2012
Copyright : (C) 2012 by Victor Olaya
Email : volayaf at gmail dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************
"""
__author__ = 'Victor Olaya'
__date__ = 'August 2012'
__copyright__ = '(C) 2012, Victor Olaya'
2012-10-04 19:33:47 +02:00
# This will get replaced with a git SHA1 when you do a git archive
2012-10-04 19:33:47 +02:00
__revision__ = '$Format:%H$'
2013-08-12 20:44:27 +02:00
from processing.core.GeoAlgorithm import GeoAlgorithm
from processing.core.GeoAlgorithmExecutionException import \
GeoAlgorithmExecutionException
from processing.modeler.CalculatorModelerParametersDialog import \
CalculatorModelerParametersDialog
2013-08-12 20:44:27 +02:00
from processing.parameters.ParameterString import ParameterString
from processing.parameters.ParameterNumber import ParameterNumber
from processing.outputs.OutputNumber import OutputNumber
2012-09-15 18:25:25 +03:00
class CalculatorModelerAlgorithm(GeoAlgorithm):
FORMULA = 'FORMULA'
NUMBER = 'NUMBER'
RESULT = 'RESULT'
2012-09-15 18:25:25 +03:00
AVAILABLE_VARIABLES = 10
def defineCharacteristics(self):
self.showInModeler = True
self.showInToolbox = False
self.name = 'Calculator'
self.group = 'Modeler-only tools'
self.addParameter(ParameterString(self.FORMULA, 'Formula', ''))
2012-09-15 18:25:25 +03:00
for i in range(self.AVAILABLE_VARIABLES):
self.addParameter(ParameterNumber(CalculatorModelerAlgorithm.NUMBER
+ str(i), 'dummy'))
self.addOutput(OutputNumber(self.RESULT, 'Result'))
2012-09-15 18:25:25 +03:00
def processAlgorithm(self, progress):
formula = self.getParameterValue(self.FORMULA)
for i in range(self.AVAILABLE_VARIABLES):
name = CalculatorModelerAlgorithm.NUMBER + str(i)
num = self.getParameterValue(name)
formula = formula.replace(chr(97 + i), str(num))
2012-09-15 18:25:25 +03:00
try:
result = eval(formula)
self.setOutputValue(self.RESULT, result)
except:
raise GeoAlgorithmExecutionException('Wrong formula: ' + formula)
2012-09-15 18:25:25 +03:00
def getCustomModelerParametersDialog(self, modelAlg, algIndex=None):
2012-09-15 18:25:25 +03:00
return CalculatorModelerParametersDialog(self, modelAlg, algIndex)