As part of a plugin project, I am coding a bunch of scripts for QGIS 2.18. My idea is to use a CSV (which is actually the result of the Distance Matrix process) as an attribute table and utilise the selectFeature, deleteFeatures already present in QGIS. Later I would use the same process but to addJoin, addField, calculateField, etc.
But on to the point, this is the code I have:
uri = "file:///C:/matrice_temp.csv?delimiter=%s" % (",") table = QgsVectorLayer(uri, 'Test', 'delimitedtext') progress.setText(str(table.isValid())) #this prints: True progress.setText(str(table.isReadOnly())) #this prints: False caps = table.dataProvider().capabilities() progress.setText(str(caps)) #this prints: 131264 progress.setText(str(QgsVectorDataProvider.DeleteFeatures)) #this prints: 2 progress.setText("") table.startEditing() request = QgsFeatureRequest().setFilterExpression(u'"InputID" = "TargetID"') request.setSubsetOfAttributes() request.setFlags(QgsFeatureRequest.NoGeometry) fids = [f.id() for f in table.getFeatures(request)] a = table.dataProvider().deleteFeatures(fids) #returns False progress.setText(str(a)) #this prints: False table.commitChanges()
I've amased a couple indications from the answers I've been looking to, if they might help you, to describe the QgsVector Layer:
- it is valid
- it is NOT read only
- it's dataprovider as some capabilities I'm unable to understand (http://qgis.org/api/classQgsVectorDataProvider.html)
- it's dataprovider returns 2 for the deleteFeatures capability
- it's not shown here, but the getFeatures selects the right number of features
- and I get a False for the deleteFeatures
I have never done this in PyQGIS before (but I have with DBF tables in ArcPy) so I must be missing something big on how to treat attribute tables without geometries.