I create a selection with getFeatures(QgsFeatureRequest()) and I want to calculate the length of the result which is a QgsFeatureIterator :

select = QgsExpression(" myexpression ")
selectedfeats = layer.getFeatures(QgsFeatureRequest(select ))

total = 0
for feature in selectedfeats :
    geom = feature.geometry()
    total += geom.length()

But it doesn't work it returns 0 whereas the selectedfeats is not empty.

  • 1
    There is no error in your code. Show how you create the layer and your expression – BERA Feb 25 at 12:43
  • Possibly print a count of selectedfeats to insure it is not empty going into the for loop. Your code is working correctly for me getting lengths of polylines in a layer. Also - gis.stackexchange.com/questions/299531/… – Cary H Feb 25 at 23:54

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.