# Distance-direction computation of lines with exactly/more than two nodes?

A distance and direction tutorial provided as an Answer at How to add Direction and Distance to attribute table? uses:

1. xat (0 ), xat (-1 ) and yat (0 ), yat (-1) for start and end points of lines; in addition to
2. (*atan((xat(-1)-xat(0))/(yat(-1)-yat(0)))) * 180/3.14159 + (180 *(((yat(-1)-yat(0)) < 0) + (((xat(-1)-xat(0)) < 0 AND (yat(-1) - yat(0)) >0)2))) to calculate direction

I would like to know any difference in application if lines with more than 1 part and/or nodes are used.

Would there be any difference between using lines with 2 nodes vs lines with more than 2 nodes and segments?

Which assumptions would QGIS automatically use/make in the following scenarios below:

a - regarding direction: the overall direction is derived from the difference in start and end node OR would it be the average of each individual computed segment.

b - regarding distance: is it the distance between start node and end node OR is it the sum total of each segment that is represented in the distance

• @PolyGeo or VeM: I want to upvote this question but find the code a mess. Can you tidy it? Is it even necessary? Thanks. – Martin F Jun 12 '14 at 17:39
• @martinf I have just tried to but it is not within my area of expertise so those efforts may be somewhat superficial compared to what you were thinking of. – PolyGeo Jun 12 '14 at 23:27

A) Direction is calculated from the difference between start and end point

B) Distance of line is best to be calculated simpy by `\$length` and is the sum of all individual parts

Also note nhopton's comment on how to get bearing of each individual segment:

A most elegant solution, thanks. I'd mention that if you need to determine the bearing for each segment of a polyline you can do this by splitting the line shapefile with the 'Split Feature' plug-in. Then load the new (split) shapefile and follow the above procedure.

EDIT

Distance `as crow flies` is calculated as:

``````sqrt( (xat(0) - xat(-1))^2 + (yat(0) - yat(-1))^2 )
``````
• Thank you to you both! But just one more query then regarding the distance in the direction-distance tutorial above. would the distance value be the \$length (i.e. sum of individual parts) or the distance, as the crow flies between start and end node. – VeM Jan 30 '14 at 7:51
• I don't know, which distance you mean? The formula you gave calculates angle, not distance, and distance calculated by `\$length` is sum of parts. I edited my answer to include formula for distance calculated "as crow flies" – Zbynek Jan 30 '14 at 8:08
• The formula for as crow flies is perfect! Thank you! – VeM Jan 30 '14 at 8:56