I run a model with an if-condition with sucess. After exporting the code the scirpt gives me an error:
"Unable to execute algorithm Field name is not set. Please enter a field name"
The code-snippet seems to be okay
"""
Model exported as python.
Name : BlnField
Group : Test
With QGIS : 31100
"""
from qgis.core import QgsProcessing
from qgis.core import QgsProcessingAlgorithm
from qgis.core import QgsProcessingMultiStepFeedback
from qgis.core import QgsProcessingParameterBoolean
from qgis.core import QgsProcessingParameterVectorLayer
from qgis.core import QgsProcessingParameterFeatureSink
from qgis.core import QgsProcessingParameterDefinition
from qgis.core import QgsExpression
import processing
class Blnfield(QgsProcessingAlgorithm):
def initAlgorithm(self, config=None):
param = QgsProcessingParameterBoolean('blnF', 'Soll ein Feld "Fl_m²" angelegt und die Flächen berechnet werden?', optional=False, defaultValue=False)
param.setFlags(param.flags() | QgsProcessingParameterDefinition.FlagAdvanced)
self.addParameter(param)
self.addParameter(QgsProcessingParameterVectorLayer('l', 'l', types=[QgsProcessing.TypeVectorAnyGeometry], defaultValue=None))
self.addParameter(QgsProcessingParameterFeatureSink('Resfc', 'Ergebnis:', type=QgsProcessing.TypeVectorAnyGeometry, createByDefault=True, defaultValue=None))
def processAlgorithm(self, parameters, context, model_feedback):
# Use a multi-step feedback, so that individual child algorithm progress reports are adjusted for the
# overall progress through the model
feedback = QgsProcessingMultiStepFeedback(1, model_feedback)
results = {}
outputs = {}
# Field calculator
alg_params = {
'FIELD_LENGTH': 10,
'FIELD_NAME': QgsExpression(' if(@blnF ,\'Fl_m2\',\'\')').evaluate(),
'FIELD_PRECISION': 3,
'FIELD_TYPE': 1,
'FORMULA': ' $area ',
'INPUT': parameters['l'],
'NEW_FIELD': True,
'OUTPUT': parameters['Resfc']
}
outputs['FieldCalculator'] = processing.run('qgis:fieldcalculator', alg_params, context=context, feedback=feedback, is_child_algorithm=True)
results['Resfc'] = outputs['FieldCalculator']['OUTPUT']
return results
def name(self):
return 'BlnField'
def displayName(self):
return 'BlnField'
def group(self):
return 'Test'
def groupId(self):
return 'Test'
def createInstance(self):
return Blnfield()