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?
-
1check this solution gis.stackexchange.com/a/304791/49538Fran Raga– Fran Raga2019年07月15日 09:36:33 +00:00Commented 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– Carlos GC2019年07月15日 10:05:50 +00:00Commented Jul 15, 2019 at 10:05
1 Answer 1
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.
Explore related questions
See similar questions with these tags.