I have two input vector layers (one point and one line layer). I loop through every feature in the input-line-layer and calculate some things (with the help of the point layer).
What I am trying to do now is to generate an output line layer for which I want to have the following six fields:
1. QgsField('id', QVariant.String)
2. QgsField('vto', QVariant.Double)
3. QgsField('vfrom', QVariant.Double)
4. QgsField('priority', QVariant.Int)
5. QgsField('node1', QVariant.String)
6. QgsField('node2', QVariant.String)
Based on those defined fields, I want to add some features to the output layer, but I do not know how to define the fields (of the output layer) and how to add features to the output layer in QGIS3.
Note: I am using the processing algorithm template for QGIS3 from Underdark: see https://anitagraser.com/2018/03/25/processing-script-template-for-qgis3/
Based on her template, she writes this:
def processAlgorithm(self, parameters, context, feedback):
inEdges = self.parameterAsSource(parameters,
self.INPUT_VECTOR_LAYER_EDGES, context)
inNodes = self.parameterAsSource(parameters,
self.INPUT_VECTOR_LAYER_NODES, context)
(sink, dest_id) = self.parameterAsSink(parameters,
self.OUTPUT_VECTOR_LAYER_MERGED, context,
inEdges.fields(), inEdges.wkbType(), inEdges.sourceCrs())
In the line which starts with (sink, dest_id), underdark has declared an outputlayer that takes the same fields as the inputlayer. But how can I declare an outputlayer with my customized fields (which are not ident with the fields from the inputlayer)?
EDIT: As suggested from root676, I altered my code a little bit:
# 1. First of all, I defined a variable which holds the definition of my fields
outFields = QgsFields()
# 2. Then, I defined the fields
outFields.append(QgsField('id', QVariant.String))
outFields.append(QgsField('vto', QVariant.Double))
outFields.append(QgsField('vfrom', QVariant.Double))
outFields.append(QgsField('priority', QVariant.Int))
outFields.append(QgsField('node1', QVariant.String))
outFields.append(QgsField('node2', QVariant.String))
# 3. Then I created the output sink with the previously defined fields
(sink, dest_id) = self.parameterAsSink(parameters, self.OUTPUT_VECTOR_LAYER_MERGED, context,
outFields, inEdges.wkbType(), inEdges.sourceCrs())
# 4. I created a new feature that will be added to the outputlayer after I set its attribute values.
newEdge = QgsFeature()
# 5. The ERROR happens when I try to set an attribute value like this:
newEdge['id'] = edgeIdTrimmed
The error I am getting is as followed:
Traceback (most recent call last): File "", line 143, in processAlgorithm KeyError: 'id'
EDIT: I solved the problem by initializing the new QgsFeature with my defined Fields variable. See below:
newEdge = QgsFeature(outFields)