I have a line vector layer which has multiple segments within individual polygons in a separate polygon layer. I have spatially joined the attributes so that each line segment now has the same ID as its corresponding polygon.

I need to calculate the lengths of the lines within each polygon according to the polygon ID. Is there an expression I can use to do this? I have already calculated the lengths of the individual segments but I need to get the sum of these according to each unique polygon ID.

I am a novice at complex SQL expressions within QGIS, ideally I would like a new column displaying the info.

1 Answer 1


You could use the Sum Line Lengths tool from the toolbar:

Vector > Analysis Tools > Sum Line Lengths

You will receive a polygon output with the sum of all lines within each polygon.

Alternatively, you could use the GroupStats plugin (which you can download/install from Plugins > Manage and Install Plugins). I wrote a somewhat similar answer here where you could change the options to find the sum of each ID. However, this will only display the results (which you could copy to a text editor or csv file). You would have to manually input the results using the Field Calculator to your layer.

  • 1
    Thank you very much for your help Joseph, worked perfectly!
    – Ian
    Commented Dec 1, 2015 at 11:49
  • @Ian - Most welcome buddy! Glad it worked for you :)
    – Joseph
    Commented Dec 1, 2015 at 11:50

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.