1

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.

PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
asked Jan 14, 2017 at 0:06

1 Answer 1

2

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.

answered Jan 14, 2017 at 8:43
1
  • I'll look into that thanks! it's clear that I am missing something on Attribute Table objects in pyqgis Commented Jan 15, 2017 at 16:14

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.