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?