I have script that calculates length of lines and adds values to attribute table. It works for vector layer, but for memory layer it skip always one value, as you can see on the pic. I calculated length by field calculator to show that it moves the value one row up.
Here is the code:
from PyQt4.QtCore import *
layer = qgis.utils.iface.activeLayer()
provider = layer.dataProvider()
lengths = [feat.geometry().length()
for feat in layer.getFeatures() ]
field = QgsField("length5", QVariant.Double)
provider.addAttributes([field])
layer.updateFields()
idx = layer.fieldNameIndex('length5')
for length in lengths:
new_values = {idx : float(length)}
provider.changeAttributeValues({lengths.index(length):new_values})
How should I edit code to get right value in right row? I suppose that the problem is in last line: the "{lengths.index(length):" doesn't work proper for memory layer. Am I right?
(Working in QGIS 2.12)