I am currently trying to convert a python script from QGIS 2.18 to QGIS 3. Therefore, I am trying to integrate my python script into the template from @underdark: see here.
I am currently stepping from one error into another so I seek help as I was not able to find any other question on Stack Exchange or any documentation.
Here is some part of my script so far:
def processAlgorithm(self, parameters, context, feedback):
inEdges = self.parameterAsVectorLayer(parameters, self.INPUT_VECTOR_LAYER_EDGES, context)
inNodes = self.parameterAsVectorLayer(parameters, self.INPUT_VECTOR_LAYER_NODES, context)
outMerged = self.parameterAsOutputLayer(parameters, self.OUTPUT_VECTOR_LAYER_MERGED, context)
#DO SOME CALCULATION
# get field indices for inputlayer
idxEdgeId = inEdges.fieldNameIndex(ID)
idxPriority = inEdges.fieldNameIndex(PRIORITY)
idxLength = inEdges.fieldNameIndex(LENGTH)
idxTime = inEdges.fieldNameIndex(TIME)
idxFrom = inEdges.fieldNameIndex(FROM)
idxTo = inEdges.fieldNameIndex(TO)
idx_id = inNodes.fieldNameIndex(NODEID)
idx_x = inNodes.fieldNameIndex(COORD_X)
idx_y = inNodes.fieldNameIndex(COORD_Y)
# Set the provider to accept the data source
outdp = outMerged.dataProvider()
# define attributes and add them to the vector layer
outdp.addAttributes([QgsField(EDGEID, QVariant.String),
QgsField(NODE1, QVariant.String),
QgsField(NODE2, QVariant.String),
QgsField(PRIORITY, QVariant.Int),
QgsField(VTO, QVariant.Double),
QgsField(VFROM, QVariant.Double)])
outMerged.updateFields()
# get field indices for outputlayer
outEdgeId = outdp.fieldNameIndex(EDGEID)
outNode1 = outdp.fieldNameIndex(NODE1)
outNode2 = outdp.fieldNameIndex(NODE2)
outPriority = outdp.fieldNameIndex(PRIORITY)
outVTo = outdp.fieldNameIndex(VTO)
outVFrom = outdp.fieldNameIndex(VFROM)
listOfEdgeIds = []
count = 0
flag = 0
(...)
For your information: I have two input vector layers (one point, and one line layer) and out of that, I want to create one outputlayer (which should be a line-vector layer as well).
But currently I have trouble with the fieldNameIndex(QString)
- method in the first section of the code. I have already tried to call the method with the dataProvider()
of the input layer, but that hasn't worked out. I get error:
Traceback (most recent call last): File "C:\Users\tlichten\AppData\Roaming\QGIS\QGIS3\profiles\default\processing\scripts\merging.py", line 89, in processAlgorithm idxEdgeId = inEdges.fieldNameIndex(ID) AttributeError: 'NoneType' object has no attribute 'fieldNameIndex'
Although I have to input vector layers, I cannot select any of them if I want to start the script. The execute script window looks like this:
In QGIS2.18, there would be two drop-down menus in which to choose between the input layers.
I have adapted the initAlgorithm()
to the following:
def initAlgorithm(self, config=None):
self.addParameter(QgsProcessingParameterVectorLayer(
self.INPUT_VECTOR_LAYER_EDGES,
self.tr('Edges'),
[QgsProcessing.TypeVectorAnyGeometry]))
self.addParameter(QgsProcessingParameterRasterLayer(
self.INPUT_VECTOR_LAYER_NODES,
self.tr('Nodes')
[QgsProcessing.TypeVectorAnyGeometry]))
self.addParameter(QgsProcessingParameterRasterDestination(
self.OUTPUT_VECTOR_LAYER_MERGED,
self.tr("Output Vector Layer Merged"),
[QgsProcessing.TypeVectorAnyGeometry]))
But I can still not select any input layers if I execute the script.