I try to run algorithms on layers but only on selected features with QgsProcessingFeatureSourceDefinition()
in a loop and then write the result with QgsVectorFileWriter
:
layer1 = QgsVectorLayer(path1, 'layer 1', 'ogr')
layer2 = QgsVectorLayer(path2, 'layer 2', 'ogr')
idx = layer2.dataProvider().fieldNameIndex('colL2')
col2values = layer2.uniqueValues(idx)
fields = layer1.dataProvider().fields()
writer = QgsVectorFileWriter(pathOutput, "CP1250", fields, QgsWkbTypes.LineString, layer1.crs(), "ESRI shapefile")
for value in col2values:
# Layer 1 and Layer 2 have the same column name 'colL2' but Layer 1 values are longer
selectLayer2 = QgsExpression(" \"colL2\" LIKE '" + str(value) + "' ")
selectedLayer2 = layer2.getFeatures(QgsFeatureRequest(selectLayer2))
idsLayer2 = [f.id() for f in selectedLayer2]
layer2.selectByIds(idsLayer2)
selectLayer1 = QgsExpression(" \"colL2\" LIKE '" + str(value) + "%' ")
selectedLayer1 = layer1.getFeatures(QgsFeatureRequest(selectLayer1))
idsLayer1 = [f.id() for f in selectedLayer1]
layer1.selectByIds(idsLayer1)
parameters = {
'INPUT': QgsProcessingFeatureSourceDefinition(layer1.id(), True),
'OUTPUT': 'memory:'
}
layer1_line = processing.run('qgis:polygonstolines', parameters)
parameters = {
'INPUT': QgsProcessingFeatureSourceDefinition(layer2.id(), True),
'OUTPUT': 'memory:'
}
layer2_line = processing.run('qgis:polygonstolines', parameters)
parameters = {
'INPUT': layer1_line['OUTPUT'],
'OVERLAY': layer2_line['OUTPUT'],
'OUTPUT': 'memory:'
}
differenced = processing.run('qgis:difference', parameters)
writer.addFeatures(differenced['OUTPUT'].getFeatures())
layer1.removeSelection()
layer2.removeSelection()
del writer
But I get the following error:
raise QgsProcessingException(msg) _core.QgsProcessingException: Unable to execute algorithm Could not load source layer for INPUT: layer_1_XXX not found