Skip to main content
fixed method name
Source Link
Matt
  • 17.9k
  • 4
  • 23
  • 59

There are most likely several approaches to tackle your issue, but I would probably lean on @Matt's suggestion.

There is a special method for deleting selected features called deleteSelectedFeaturedeleteSelectedFeatures().

Keep in mind that the output of processing will inherit the name of your 'INPUT' variable.

layer1 = QgsProject.instance().mapLayersByName('grid_test2')[0]
layer2 = QgsProject.instance().mapLayersByName('grid_test')[0]

selection = processing.run("native:selectbylocation", 
                   {'INPUT': layer1,
                    'PREDICATE': [0,4,7],
                    'INTERSECT': layer2,
                    'METHOD':0})['OUTPUT']

layer_to_edit = QgsProject.instance().mapLayersByName(selection.name())[0]

layer_to_edit.startEditing()
layer_to_edit.deleteSelectedFeatures()
layer_to_edit.commitChanges()

If you though want to retrieve a list with selected features, then try this:

layer1 = QgsProject.instance().mapLayersByName('grid_test2')[0]
layer2 = QgsProject.instance().mapLayersByName('grid_test')[0]

selection = processing.run("native:selectbylocation", 
                   {'INPUT': layer1,
                    'PREDICATE': [0,4,7],
                    'INTERSECT': layer2,
                    'METHOD':0})['OUTPUT']

layer_to_edit = QgsProject.instance().mapLayersByName(selection.name())[0]

selected_ids = [feat.id() for feat in selection.selectedFeatures()]

layer_to_edit.startEditing()
for fid in selected_ids:
    layer_to_edit.deleteFeature(fid)
layer_to_edit.commitChanges()

References:

There are most likely several approaches to tackle your issue, but I would probably lean on @Matt's suggestion.

There is a special method for deleting selected features called deleteSelectedFeature().

Keep in mind that the output of processing will inherit the name of your 'INPUT' variable.

layer1 = QgsProject.instance().mapLayersByName('grid_test2')[0]
layer2 = QgsProject.instance().mapLayersByName('grid_test')[0]

selection = processing.run("native:selectbylocation", 
                   {'INPUT': layer1,
                    'PREDICATE': [0,4,7],
                    'INTERSECT': layer2,
                    'METHOD':0})['OUTPUT']

layer_to_edit = QgsProject.instance().mapLayersByName(selection.name())[0]

layer_to_edit.startEditing()
layer_to_edit.deleteSelectedFeatures()
layer_to_edit.commitChanges()

If you though want to retrieve a list with selected features, then try this:

layer1 = QgsProject.instance().mapLayersByName('grid_test2')[0]
layer2 = QgsProject.instance().mapLayersByName('grid_test')[0]

selection = processing.run("native:selectbylocation", 
                   {'INPUT': layer1,
                    'PREDICATE': [0,4,7],
                    'INTERSECT': layer2,
                    'METHOD':0})['OUTPUT']

layer_to_edit = QgsProject.instance().mapLayersByName(selection.name())[0]

selected_ids = [feat.id() for feat in selection.selectedFeatures()]

layer_to_edit.startEditing()
for fid in selected_ids:
    layer_to_edit.deleteFeature(fid)
layer_to_edit.commitChanges()

References:

There are most likely several approaches to tackle your issue, but I would probably lean on @Matt's suggestion.

There is a special method for deleting selected features called deleteSelectedFeatures().

Keep in mind that the output of processing will inherit the name of your 'INPUT' variable.

layer1 = QgsProject.instance().mapLayersByName('grid_test2')[0]
layer2 = QgsProject.instance().mapLayersByName('grid_test')[0]

selection = processing.run("native:selectbylocation", 
                   {'INPUT': layer1,
                    'PREDICATE': [0,4,7],
                    'INTERSECT': layer2,
                    'METHOD':0})['OUTPUT']

layer_to_edit = QgsProject.instance().mapLayersByName(selection.name())[0]

layer_to_edit.startEditing()
layer_to_edit.deleteSelectedFeatures()
layer_to_edit.commitChanges()

If you though want to retrieve a list with selected features, then try this:

layer1 = QgsProject.instance().mapLayersByName('grid_test2')[0]
layer2 = QgsProject.instance().mapLayersByName('grid_test')[0]

selection = processing.run("native:selectbylocation", 
                   {'INPUT': layer1,
                    'PREDICATE': [0,4,7],
                    'INTERSECT': layer2,
                    'METHOD':0})['OUTPUT']

layer_to_edit = QgsProject.instance().mapLayersByName(selection.name())[0]

selected_ids = [feat.id() for feat in selection.selectedFeatures()]

layer_to_edit.startEditing()
for fid in selected_ids:
    layer_to_edit.deleteFeature(fid)
layer_to_edit.commitChanges()

References:

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

There are most likely several approaches to tackle your issue, but I would probably lean on @Matt's suggestion.

There is a special method for deleting selected features called deleteSelectedFeature().

Keep in mind that the output of processing will inherit the name of your 'INPUT' variable.

layer1 = QgsProject.instance().mapLayersByName('grid_test2')[0]
layer2 = QgsProject.instance().mapLayersByName('grid_test')[0]

selection = processing.run("native:selectbylocation", 
                   {'INPUT': layer1,
                    'PREDICATE': [0,4,7],
                    'INTERSECT': layer2,
                    'METHOD':0})['OUTPUT']

layer_to_edit = QgsProject.instance().mapLayersByName(selection.name())[0]

layer_to_edit.startEditing()
layer_to_edit.deleteSelectedFeatures()
layer_to_edit.commitChanges()

If you though want to retrieve a list with selected features, then try this:

layer1 = QgsProject.instance().mapLayersByName('grid_test2')[0]
layer2 = QgsProject.instance().mapLayersByName('grid_test')[0]

selection = processing.run("native:selectbylocation", 
                   {'INPUT': layer1,
                    'PREDICATE': [0,4,7],
                    'INTERSECT': layer2,
                    'METHOD':0})['OUTPUT']

layer_to_edit = QgsProject.instance().mapLayersByName(selection.name())[0]

selected_ids = [feat.id() for feat in selection.selectedFeatures()]

layer_to_edit.startEditing()
for fid in selected_ids:
    layer_to_edit.deleteFeature(fid)
layer_to_edit.commitChanges()

References: