[GIS] How to get the length of each line segment in a line shapefile in QGIS

qgisqgis-2qgis-openlayers-plugin

I am new to QGIS. I drew a line depicting a small length of road. But I drew it in segments and then at last right clicked at the end point and gave id=1.

I extracted the nodes, their geometry. But what I need is the length of individual segment, its starting node coordinates, ending node coordinates.

for eg:enter image description here

I need data in the attribute table as id;start_X;start_Y;end_X;end_Y;length

I have a large dataset of road network to be done in the same way.

Best Answer

You can use Explode Lines in the Processing toolbox to turn each segment into an individual line. Have a look here:

Simple automated way to split all lines in layer at vertices using QGIS?

You can then open the attribute table of the layer with the newly split lines and use the field calculator to calculate the length into a new attribute with the...

$length 

...expression.

Getting the start and stop nodes is trickier, but you can perhaps use the WKT from geom function and then split the result on commas. If you use the expression:

geomToWKT( $geometry ) 

...you get a result that looks something like this:

LINESTRING(357595.01178169692866504 6202328.19801435992121696, 400845.94642397749703377 6225465.47612086869776249)

So if you split that on the comma, either in Excel (if you export the table and rejoin it in QGIS) or with a regular expression in the Field Calculator, you might be able to get start and end nodes.

However, I'm not sure if WKT preserves the order of the nodes (start vs end node etc), but someone else around here can probably confirm whether that's the case.

Edit: Also, if you select all your lines, open the attribute table, copy all, and then paste it into Excel, the first column will be the WKT "Linestring(312423.4353425..." representation of the geometry. That way you can extract the start and end nodes in Excel.

Have a look here:

Is there any way to extract coordinates of a road segment using QGIS?

Related Question