0

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 = 0
with 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 += 1

This 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.
Attribute table example

  • Can you please add code – Devdatta Tengshe May 28 '14 at 3:35
  • Here is the piece of code I use and a screenshot of the result. – YeO May 28 '14 at 20:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.