3

I try to run qgis:difference on selected features of 2 layers. But the result of my code :

parameters = {'INPUT': QgsProcessingFeatureSourceDefinition(layer1.id(), True),
              'OVERLAY': QgsProcessingFeatureSourceDefinition(layer2.id(), True),
              'OUTPUT': 'memory:'}
differenced = processing.run('qgis:difference', parameters)

is a string output_... instead of a QgsVectorLayer as usual. I have added :

QgsProject.instance().addMapLayer(layer1, False)
QgsProject.instance().addMapLayer(layer2, False)

before but the algorithm doesn't work as I want.

1
  • That should be the correct way. How did you define layer1 and layer2? – Joseph Apr 25 '19 at 9:54
2

This is how I run it, maybe it can help

diff = processing.runalg("qgis:difference", layer1, layer2, False, None)
result = processing.getObject(diff['OUTPUT'])
# if you want to save it to file
_writer = QgsVectorFileWriter.writeAsVectorFormat(result, 'output_layer.shp', "utf-8", None, "ESRI Shapefile")

layer 1 and layer2 are instances of QgsVectorLayer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.