Skip to main content
added 50 characters in body
Source Link
Taras
  • 34k
  • 4
  • 73
  • 148

Another option is to use the materialize() method:.

from qgis import processing

layer = QgsProject.instance().mapLayersByName('Langham_SPZs_v1_diss')[0]

outfn2 = "Langham_SPZs_final.shp"

layer.selectByExpression("number='2'")
layer2 = layer.materialize(QgsFeatureRequest().setFilterFids(layer.selectedFeatureIds()))

layer.selectByExpression("number='3'")
layer3 = layer.materialize(QgsFeatureRequest().setFilterFids(layer.selectedFeatureIds()))

processing.run("native:difference", {'INPUT' : layer3, 'OVERLAY' : layer2, 'OUTPUT': outfn2})

Note: this method is available since QGIS 3.0.


References:

Another option is to use the materialize() method:

from qgis import processing

layer = QgsProject.instance().mapLayersByName('Langham_SPZs_v1_diss')[0]

outfn2 = "Langham_SPZs_final.shp"

layer.selectByExpression("number='2'")
layer2 = layer.materialize(QgsFeatureRequest().setFilterFids(layer.selectedFeatureIds()))

layer.selectByExpression("number='3'")
layer3 = layer.materialize(QgsFeatureRequest().setFilterFids(layer.selectedFeatureIds()))

processing.run("native:difference", {'INPUT' : layer3, 'OVERLAY' : layer2, 'OUTPUT': outfn2})

References:

Another option is to use the materialize() method.

from qgis import processing

layer = QgsProject.instance().mapLayersByName('Langham_SPZs_v1_diss')[0]

outfn2 = "Langham_SPZs_final.shp"

layer.selectByExpression("number='2'")
layer2 = layer.materialize(QgsFeatureRequest().setFilterFids(layer.selectedFeatureIds()))

layer.selectByExpression("number='3'")
layer3 = layer.materialize(QgsFeatureRequest().setFilterFids(layer.selectedFeatureIds()))

processing.run("native:difference", {'INPUT' : layer3, 'OVERLAY' : layer2, 'OUTPUT': outfn2})

Note: this method is available since QGIS 3.0.


References:

Source Link
Taras
  • 34k
  • 4
  • 73
  • 148

Another option is to use the materialize() method:

from qgis import processing

layer = QgsProject.instance().mapLayersByName('Langham_SPZs_v1_diss')[0]

outfn2 = "Langham_SPZs_final.shp"

layer.selectByExpression("number='2'")
layer2 = layer.materialize(QgsFeatureRequest().setFilterFids(layer.selectedFeatureIds()))

layer.selectByExpression("number='3'")
layer3 = layer.materialize(QgsFeatureRequest().setFilterFids(layer.selectedFeatureIds()))

processing.run("native:difference", {'INPUT' : layer3, 'OVERLAY' : layer2, 'OUTPUT': outfn2})

References: