2

I have a processing script that will carry out a selection by expression of the input layer, then do a join attributes by location with a join layer.

The next stage will be to add a new field and calculate the field, I got a python script that runs this but I am struggling to adapt it into the processing tool.

The code is below:

def initAlgorithm(self, config=None):
    """
    Here we define the inputs and output of the algorithm, along
    with some other properties.
    """

    # We add the input vector features source. It can have any kind of
    # geometry.
    self.addParameter(
        QgsProcessingParameterFeatureSource(
            self.INPUT,
            self.tr('NRD Layer'),
            [QgsProcessing.TypeVectorPoint]
        )
    )
    self.addParameter(
        QgsProcessingParameterFeatureSource(
            self.JOIN,
            self.tr('Parliamentary Constituency Layer'),
            [QgsProcessing.TypeVectorPolygon]
        )
    )
    self.addParameter(
        QgsProcessingParameterField(
            self.JOIN_FIELDS,
            self.tr('Choose Join Field'),
            parentLayerParameterName = self.JOIN,
            allowMultiple=False,
            optional=False
        )
    )
    self.addParameter(
        QgsProcessingParameterVectorDestination(
            self.OUTPUT,
            self.tr('Output layer')
        )
    )
def processAlgorithm(self, parameters, context, feedback):
    selectData = processing.run(
        'qgis:selectbyexpression',
        {
            "INPUT":parameters['INPUT'],
            "EXPRESSION":'\"mcmcode\" !=\'1\' AND \"mcmcode\" !=\'9\' AND \"mcmcode\" !=\'960\' AND \"mcmcode\" !=\'999\'',
            "METHOD":0,
            'OUTPUT':parameters['OUTPUT']
        },
        context=context,
        feedback=feedback 
    )
    joinoutput = processing.run(
    'qgis:joinattributesbylocation', 
    {
         'INPUT':QgsProcessingFeatureSourceDefinition(selectData['OUTPUT'], True),
         'JOIN':parameters['JOIN'] ,
         'PREDICATE':[0],
         'JOIN_FIELDS':parameters['join_field'],
         'METHOD':1,
         'DISCARD_NONMATCHING':True,
         'PREFIX':'',
         'OUTPUT':parameters['OUTPUT']
    }, 
    context = context, 
    feedback = feedback
    )
    NRDLayer = joinoutput['OUTPUT']
    F_Onset = QgsField('F_Onset',QVariant.Double)
    NRDLayer.dataProvider().addAttributes([F_Onset])
    NRDLayer.updateFields()

    return {'OUTPUT':joinoutput['OUTPUT']}

The error suggests you can't use dataprovider on joinoutput['OUTPUT'].

I thought I might need to use ParameterasSink to get my output in order to be able to use things like .featureCount(), .dataProvider() with it etc.

However not understanding this very well I would rather avoid that. The tool works up to that point.

Any ideas on what to do in QGIS 3?

2
  • 1
    check this solution gis.stackexchange.com/a/304791/49538
    – Fran Raga
    Commented Jul 15, 2019 at 9:36
  • Thanks, I am trying to avoid using parametersAsSink, firstly because my current method (QgsProcessingParameterVectorDestination) works up to this point, and secondly because i dont quite understand it or know how to apply it (it seems far more complicated? What is the main difference?) Do you know when one has to use parametersAsSink? If I am adding and calculating a field do I need to?
    – Carlos GC
    Commented Jul 15, 2019 at 10:05

1 Answer 1

2

Okay so it was actually quite simple, you can't use the output of the algorithm straight away, you first need to do this:

layer = self.parameterAsVectorLayer(parameters, self.OUTPUT, context)

Then you can run functions like layer.getFeatures() etc. on that.

Don't know if there are better ways to do what I am trying to do but this works! Anyone who has better ways feel welcome to post a comment.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.