2

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):
  L=[]
  features=layer.getFeatures()
  d = QgsDistanceArea()
  for i, element in enumerate(features):
    m = d.measureLength(element.geometry())
    L.append((i, round(m,1)))
  return L

2 Answers 2

2

Try to replace

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

with

m = element.length()
1

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

2
  • Many thanks Neven. The correct expression is as follow:
    – ennine
    Commented Jul 26, 2018 at 11:51
  • m = d.measureLength(element.geometry())
    – ennine
    Commented Jul 26, 2018 at 11:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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