QGIS – How to Get Sum/Total Length of Lines per Polygon

fields-attributesgroupingqgis

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.

Best Answer

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.

Related Question