I wrote the following code in order to automatically extract the objects' length of a line layer, but I think the « measureLine » expression is not working. This expression works very well PyQgis 2.0. Is there a new expression in PyQgis 3.0 ?

def dist_seg(layer):
  d = QgsDistanceArea()
  for i, element in enumerate(features):
    m = d.measureLength(element.geometry())
    L.append((i, round(m,1)))
  return L

I think you should use d.measureLength. I didn't test it. In meantime, you can try it, and on this are all changes in PyQGIS3: https://qgis.org/api/api_break.html#qgis_api_break_3_0_QgsDistanceArea

| improve this answer | |
  • Many thanks Neven. The correct expression is as follow: – ennine Jul 26 '18 at 11:51
  • m = d.measureLength(element.geometry()) – ennine Jul 26 '18 at 11:51

Try to replace

m = d.measureLine(element.geometry().asPolyline())


m = element.length()
| improve this answer | |

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.