Saturday 2 February 2019

fields attributes - How to get sum / total length of lines per polygon in QGIS?


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.



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.


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...