3

I am developing a plugin for QGIS and have hit a snag that does not seem to have happened before.

After having created a point memory layer with attributes, I need to delete features. The following code was used to delete same, however, after this operation, all features disappear from the mapcanvas. The features are displayed in the browser, with the exception of the deleted ones, and the layer is still shown as an existing layer in the layer legend.

Here's snippets of the code:

def createTmpLyr(self):
 uri = "Point?crs=epsg:4248&field=id:integer&field=Pt:string(20)&index=yes"
 self.vl = QgsVectorLayer(uri, "temporary_points", "memory")
 QgsMapLayerRegistry.instance().addMapLayer(self.vl)
more code that adds point features successfully...

Code that generates a selection of points to be deleted

 self.vl.setSelectedFeatures(listOfPts)
 ntBndPts=self.vl.selectedFeatures()
 self.delNtBndPts(ntBndPts)
 self.vl.commitChanges()
def delNtBndPts(self, pts):
 print pts
 with edit(self.vl):
 for feature in pts:
 self.vl.deleteFeatures([feature.id()])
PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
asked Jun 16, 2017 at 15:10
2
  • Perhaps the canvas needs to be refreshed (iface.mapCanvas().refresh())? Or the layers (iface.mapCanvas().refreshAllLayers())? Commented Jun 19, 2017 at 9:49
  • I thought this was it, but it doesn't work. Just to confirm, there are objects in this memory layer that I can access with getFeatures() command. Saving a copy of the layer returns all the objects as well. Commented Jun 20, 2017 at 14:52

1 Answer 1

1
self.vl.setSelectedFeatures(listOfPts)
 ntBndPts=self.vl.selectedFeatures()
 self.delNtBndPts(ntBndPts)
 def delNtBndPts(self, pts):
 print pts
 self.vl.startEditing()
 for feature in pts:
 self.vl.deleteFeatures([feature.id()])
 self.vl.updateExtents()
 self.vl.commitChanges()
answered Dec 14, 2017 at 13:45

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.