I am comparing geometries of input layer and output layer features. I want to delete features in output layers which has equal geometry to input feature. I am using following code to do that.

for feat in drain.getFeatures():
    for f in outLayer.getFeatures():
        if feat.geometry().equals(f.geometry()):

This code deletes features in output layer but while removing output layer from the Qgis desktop, input layer geometries disappears but features are there in attribute table.

What changes can I make in the code to make it work properly? Here I am adding an image to make my doubt more clearer.

enter image description here

Here, "drain"is an input layer which is open in Qgis screen, it has attributes but features are not displayed(or visible) on the screen. It was coming correctly before executing the code given above.


Decide either to work with the data provider or the layer, not both

Working directly on the layer has the advantage, that all internals of QGIS are notified about changes/deletions, whereas changes on the data provider just happen, but QGIS will have no information about that internally (just as if those changes would be done completely outside of QGIS). So any cached information inside QGIS (like in the attribute table) will still be there until loaded next time.

To delete the feature on the layer do


Don't update a deleted feature

layer.updateFeature() will apply any geometry or attribute changes you have done to you your feature to the data source. Since you have just deleted your feature there is nothing there to update any more.

If working on the layer, put it into edit mode

It's not required if you work on the data source, but if you work on the layer, it needs to be editable.

with edit(outLayer):
    for drainFeature in drain.getFeatures():
        for outFeature in outLayer.getFeatures():
            if drainFeature.geometry().equals(outFeature.geometry()):
  • with edit() is giving name error. what can i do about it? I had tried outLayer.startEditing() does it work the same way? – VaP Mar 20 at 7:33
  • from qgis.core import edit should do the trick. startEditing() also works but you need to be careful to commit the changes in the end and handle exceptions. – Matthias Kuhn Mar 20 at 7:51
  • this solution didn't work. Input layer is not displaying just like before. Looking forward to get more suggestions. – VaP Mar 20 at 8:56
  • What exactly do you mean with "input layer is not displaying just like before"? – Matthias Kuhn Mar 20 at 9:31
  • I have edited the question to make it clear. Please check and give suggestions. – VaP Mar 20 at 10:06

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.