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()])
  • Perhaps the canvas needs to be refreshed (iface.mapCanvas().refresh())? Or the layers (iface.mapCanvas().refreshAllLayers())? – Joseph Jun 19 '17 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. – Donald Singh Jun 20 '17 at 14:52
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()

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.