[Qgis-user] Can't delete features of shapefile with pyqgis in QGIS 2.8.6

Javier Sánchez Portero javiersanp at gmail.com
Sat Oct 28 02:47:56 PDT 2017


Hello list.

I can't delete features in a shapefile layer with pyqgis using QGIS
2.8.6-Wien and GDAL 1.11.3. The same code works properly in newer versions.
I haven't found any issue in the tracker or message in the list about it.
Could you confirm if it's a problem with my code or it's a QGIS bug?

The code is below.

Best regards, Javier

from qgis.core import *
from PyQt4.QtCore import QVariant

qgs = QgsApplication([], False)
qgs.initQgis()


fields = QgsFields()
fields.append(QgsField("first", QVariant.Int))
fields.append(QgsField("second", QVariant.String, len=20))

fn = "my_shapes.shp"
writer = QgsVectorFileWriter(fn, "CP1250", fields,
                             QGis.WKBPoint, None, "ESRI Shapefile")
del writer

fet = QgsFeature()
fet.setGeometry(QgsGeometry.fromPoint(QgsPoint(10, 10)))
fet.setAttributes([1, "text"])

layer = QgsVectorLayer(fn, 'test', 'ogr')
pr = layer.dataProvider()

pr.addFeatures([fet])
assert layer.featureCount() == 1

to_clean = [f.id() for f in layer.getFeatures()]
assert len(to_clean) == 1

pr.deleteFeatures(to_clean)
assert layer.featureCount() == 0
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-user/attachments/20171028/900cd82a/attachment.html>


More information about the Qgis-user mailing list