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.
1 Answer 1
When I check the properties of a delimited text layer in the GUI, I get
Capabilities of this layer
Create Spatial Index, Fast Access to Features at ID, Curved Geometries
... no delete features.
You might want to look into the code of the Editable GeoCSV plugin to see how they achieve their goal.
-
I'll look into that thanks! it's clear that I am missing something on Attribute Table objects in pyqgisMPel– MPel2017年01月15日 16:14:31 +00:00Commented Jan 15, 2017 at 16:14