2

In the good ould days of QGIS 2 I created a processing script, that takes as input a table and a table field (table has only one record) and returns the string attribute value of the table field:

##mygroup=group
##myname=name
##input=table
##a=field input
##text=output string
formeln=processing.getObject(input)
for fo in formeln.getFeatures():
 text = fo[a]

Embedding this script in graphical modeler in QGIS 2 and using the output e.g. in a Field calculator algorithm lets me chose its output string as input for (here) the formula:

enter image description here

So far so good, now on to QGIS 3. At least I succeeded (?) in rewriting this script, my result:

from PyQt5.QtCore import QCoreApplication
from qgis.core import (QgsProcessing,
 QgsFeatureSink,
 QgsProcessingException,
 QgsProcessingAlgorithm,
 QgsProcessingParameterFeatureSource,
 QgsProcessingParameterField,
 QgsProcessingParameterString,
 QgsProcessingParameterFeatureSink,
 )
import processing
class ExampleProcessingAlgorithm(QgsProcessingAlgorithm):
 FORMULA_COLLECTION = 'FORMULA_COLLECTION'
 FORMULA_ID = 'FORMULA_ID'
 OUTPUT_STRING = 'OUTPUT'
 def tr(self, string):
 return QCoreApplication.translate('Processing', string)
 def createInstance(self):
 return ExampleProcessingAlgorithm()
 def name(self):
 return 'output_table_field_value_as_string'
 def displayName(self):
 return self.tr('Table field value as string')
 def group(self):
 return self.tr('giswg')
 def groupId(self):
 return 'giswg'
 def shortHelpString(self):
 return self.tr("This returns the value of the specified field of a table. Implies, that the table has exacly one record.")
 def initAlgorithm(self, config=None):
 #input parameter
 self.addParameter(QgsProcessingParameterFeatureSource(self.FORMULA_COLLECTION, self.tr('The formula collection'), [QgsProcessing.TypeVector]))
 self.addParameter(QgsProcessingParameterField(self.FORMULA_ID, None, self.tr('The formula identificator'), self.FORMULA_COLLECTION, QgsProcessingParameterField.String))
 #output parameter
 self.addParameter(QgsProcessingParameterString(self.OUTPUT_STRING, self.tr('Output string'), ''))
 def processAlgorithm(self, parameters, context, feedback):
 formula_collection = self.parameterAsSource(parameters, self.FORMULA_COLLECTION, context)
 formula_id = self.parameterAsString(parameters, self.FORMULA_ID, context)
 out_string = self.parameterAsString(parameters, self.OUTPUT_STRING, context)
 #this portion is taken from the old QGIS 2 script
 for fo in formula_collection.getFeatures():
 out_string = fo[formula_id]
 return {self.OUTPUT_STRING: out_string}

Now I would like to do the same in QGIS 3, but the 'Formula' field refuses to let me chose the output of my script as input:

enter image description here

Any suggestions what might be wrong with my script or what else I'm probably missing?

asked Sep 19, 2018 at 14:21

1 Answer 1

3

You're adding your string output as an input parameter. It should be:

self.addOutput(QgsProcessingOutputString(self.OUTPUT_STRING, self.tr('Output string')))
answered Sep 19, 2018 at 18:47

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.