PyQgis: How can I preserve order or index when adding features to a Vector Layer from

المشرف العام

Administrator
طاقم الإدارة
As I read data from a text file and add points to a QgsVectorLayer (as well as related attributes), each new record gets 'stacked' on top of the previous one in the Attribute Table (and within the Vector Layer as well) and I end up with the first record read having the highest index and the last record having index 0 (LIFO-style) and this is not what I want (I would like to preserve the index from my input file).

Short of a solution, I am currently inserting an 'AutoID' field that is incrementing as I read my text file but I find this rather clumsy :

geom = QgsGeometry()feat = QgsFeature()layer = QgsVectorLayer("Point?field=AutoID:integer& field=Line:string& field=Point:string& field=Easting:double& field=Northing:double& field=Elevation:double", file_name, "memory")layer.startEditing()i = 0with open(data_file, 'rU') as filein: ##read the text file, get data and attributes in corresponding variables feat.setGeometry(geom.fromPoint(QgsPoint(x, y))) feat.setAttributes([i, line, point, x, y, z]) layer.addFeature(feat,) i += 1This is the end result: the last record read (AutoID 5326) gets index 0 in the table, so compared to my data file, the Attribute Table is just 'reversed' which is no big deal, but annoying. I need the index of my Vector Layer to be the same than that of my data file.




أكثر...
 
أعلى