I have collected points alongside road in form of csv files with z-values. When imported in QGIS I used Set Z value and Points to path to make line. When I use right button in Identify Features tool I can see that all vertices have appropriate z values. However, when I use $length function in Field Calculator I get exactly same values as if I have draw line but hand without z values. Also, how can I calculate average slope for same line?
$length function will only get you the 'horizontal' length along the line, not the true length allowing for the slope. To calculate this:
- Take your vector layer you created using 'Points to path' and use 'Explode lines'. This will split your single line into multiple lines with breaks located at every vertex.
- On this new layer, open field calculator and create a new field "h_length" using the function
$length. This is the 'horizontal' length of each segment.
- Also in field calculator, create a field "z_start" using the function
z(start_point($geometry)), and another field "z_end" using the function
z(end_point($geometry)). These denote the elevation at the start and end of each line segment.
- Calculate the level difference between the start and end point in a new field "z_diff" using the function
"z_start" - "z_end".
- Calculate the 'sloped' length of each line segment in a new field "s_length" based on Pythagoras using the function
sqrt( ("h_length" ^2) + ( "z_diff" ^2)).
- If you want to see the gradient of each segment, create a new field "gradient" using the function
"h_length" / "z_diff"(i.e. a 1 in 40 slope will calculate as 40, swap this round if you require).
- Use "View" > "Statistical summary" to view statistics of the attribute "s_length" in your 'Exploded' layer. The 'sum' statistic will give you the total sloped length of all segments.
To get the average slope of the entire line, follow the same principles by calculating the 'horizontal' distance and the difference in elevation between the first and last points.