1

How can I get the length of all lines with python in QGIS.

For example if I have:

enter image description here

How can I say, I have 23 m (example) of lines?

I found that if I click in one part of the line:

enter image description here

But really I wouldn't know how can I get the Length(Cartesian) or Length(Ellipsoidal) in Python.

Because I do the next code but I don't find any fields with the name "length" but in the above image I can see that inside of "Derived" There is a field.

print(features[0])
print(features[0].geometry())
lista = features[0].fields().toList()

And for example when I right click in the layer, and I open "open attribute table" I see left image, but on the other hand I show that the same line has a field "length" how you can see in the right image:

enter image description here

Vince
  • 20,017
  • 15
  • 45
  • 64
  • There is no field, it's derived properties of a geometry. Get the feature geometry and look at https://qgis.org/pyqgis/master/core/QgsGeometry.html?highlight=qgsgeometry#qgis.core.QgsGeometry.length. – J. Monticolo Mar 09 '20 at 09:53
  • a similar question was: How to sum values of field: https://gis.stackexchange.com/questions/37712/sum-values-in-a-field/37714. If you prefer a python solution, you would iterate over all features, get their length und sum them up. – Andreas Müller Mar 09 '20 at 10:17

3 Answers3

7

Here is a way to sum up the values with a for-loop:

lyr = iface.activeLayer()
total = 0
for feat in lyr.getFeatures():
    total += feat.geometry().length()
print(total)
Andreas Müller
  • 2,622
  • 13
  • 20
  • I did another question and surely you know how do that:https://gis.stackexchange.com/questions/353447/apply-steiner-algorithm-to-a-layer-with-python-in-qgis – ZacariasSatrustegui2 Mar 09 '20 at 11:34
7

Another (fun) way using SQL in Python:

from qgis.core import QgsVectorLayer
vlayer = QgsVectorLayer("?query=select sum(length(geometry)) sum_length from mylines", "vlayer", "virtual" )
mysum = [ft["sum_length"] for ft in vlayer.getFeatures()][0]
print (mysum)

The sum can also be viewed as an attribute table loading the virtual layer:

QgsProject.instance().addMapLayer(vlayer)

or the whole thing without SQL:

mysum = sum([ft.geometry().length() for ft in iface.activeLayer().getFeatures()])
print (mysum)
Jakob
  • 7,471
  • 1
  • 23
  • 41
  • I did another question and surely you know how do that: https://gis.stackexchange.com/questions/353447/apply-steiner-algorithm-to-a-layer-with-python-in-qgis – ZacariasSatrustegui2 Mar 09 '20 at 11:33
  • @Jakob I don't understand the SQL-Query: why do you use sum in the SQL-Example? And what does it mean to put the name sum_length after the sum? I would expect to get the sum of all lines, scince sum() is an aggregate of all geometries? Btw: the list comprehension is very pythonic! – Andreas Müller Mar 10 '20 at 09:17
  • Your right sum is redundant. I first made the SQL, but the Python list comprehension returns a list, so I just sum the list, even thou it is a one entry list. I rewrite to mysum = [ft["sum_length"] for ft in vlayer.getFeatures()][0] to clarity it is only SQL doing the sum. – Jakob Mar 10 '20 at 12:25
2

What you want to do is

feature.geometry().length() 

will give you the length in the same "QgsUnitTypes.DistanceUnit" as the crs of your layer. if your layer is in Degrees than your length will be too.

This method returns Cartesian length.

Kalak
  • 3,848
  • 8
  • 26