in QGIS 2.18 I had this script
##formula_1=boolean
##formula_2=boolean
##formula_3=boolean
##result=output string
if formula_1 == True:
result= 1
elif formula_2 == True:
result= 2
elif formula_3 == True:
result= 3
else:
result= 4
So I can fill a string based on the selected Check Box. Now, in QGIS 3.4, How can I create a script to do the same?
1 Answer 1
Check the Join attributes by location script on QGIS GitHub. You can see from the script how to effectively add boolean check boxes. I think the most important part to extract from the script is the following (edited the names slightly):
class ExampleProcessingAlgorithm(QgsProcessingAlgorithm):
PREDICATE = "PREDICATE"
def initAlgorithm(self, config=None):
self.predicates = (
('Option 1', self.tr('Option 1')),
('Option 2', self.tr('Option 2')),
('Option 3', self.tr('Option 3'))
)
predicate = QgsProcessingParameterEnum(self.PREDICATE,
self.tr('Settings'),
options=[p[1] for p in self.predicates],
allowMultiple=True, defaultValue=[0])
predicate.setMetadata({
'widget_wrapper': {
'class': 'processing.gui.wrappers.EnumWidgetWrapper',
'useCheckBoxes': True,
'columns': 2}})
self.addParameter(predicate)
def processAlgorithm(self, parameters, context, feedback):
options = [self.predicates[i][0] for i in
self.parameterAsEnums(parameters, self.PREDICATE, context)]
if 'Option 1' in options:
print('Do something')
Solution find. EDIT
This script does what was requested, it can only be used in the Modeler.
from PyQt5.QtCore import QCoreApplication
from qgis.core import (QgsProcessing,
QgsFeatureSink,
QgsProcessingException,
QgsProcessingAlgorithm,
QgsProcessingParameterEnum,
QgsProcessingParameterFeatureSource,
QgsProcessingParameterFeatureSink,
QgsFields,
QgsProcessingParameterExpression,
QgsFeatureRequest,
QgsGeometry,
QgsProcessingUtils,
QgsProcessingParameterBoolean,
QgsProcessingParameterField,
QgsProcessingParameterString,
QgsProcessingOutputNumber,
QgsProcessingOutputString,)
import processing
from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
from processing.tools import vector
class ExampleProcessingAlgorithm(QgsProcessingAlgorithm):
EXPRESSIONOUT = 'OUT'
BOOLEAN1 = 'BOOLEAN1'
BOOLEAN2 = 'BOOLEAN2'
BOOLEAN3 = 'BOOLEAN3'
def tr(self, string):
return QCoreApplication.translate('Processing', string)
def createInstance(self):
return ExampleProcessingAlgorithm()
def name(self):
return 'Boolean'
def displayName(self):
return self.tr('Boolean')
def group(self):
return self.tr('Boolean')
def groupId(self):
return 'Boolean'
def shortHelpString(self):
return self.tr("Example algorithm short description")
def initAlgorithm(self, config=None):
self.addOutput(QgsProcessingOutputString(self.EXPRESSIONOUT, self.tr('OUT')))
self.addParameter(QgsProcessingParameterBoolean(self.BOOLEAN1,
self.tr('Option 1'),
defaultValue=False))
self.addParameter(QgsProcessingParameterBoolean(self.BOOLEAN2,
self.tr('Option 2'),
defaultValue=False))
self.addParameter(QgsProcessingParameterBoolean(self.BOOLEAN3,
self.tr('Option 3'),
defaultValue=False))
def processAlgorithm(self, parameters, context, feedback):
atOUT = 'start'
BOOLEAN1 = self.parameterAsBool(parameters, self.BOOLEAN1, context)
BOOLEAN2 = self.parameterAsBool(parameters, self.BOOLEAN2, context)
BOOLEAN3 = self.parameterAsBool(parameters, self.BOOLEAN3, context)
if BOOLEAN1==True:
OUT = '1'
if BOOLEAN2==True:
OUT = '2'
if BOOLEAN3==True:
OUT = '3'
return {self.EXPRESSIONOUT: OUT}
It receive three boolean as input and has a string as output that fit in a Field Calculator for example.
Hope it helps someone else.