4

I have several sets of points on a layer, with all sets of points in nearly the same geographic location. I use the "points to paths" function and then explode the lines. I then use the Azimuth expression and check the line angle and I obtain to different angles depending on the line of either "90" or "270". Being that the origin points start in the same viscinity, why would the angle change from 90 to 270 on different lines?

I'd like to see all angles represented in 90 degrees and not 270 degrees.

3 Answers 3

5

Solution

To change the lines with azimuth = 270 to a value of 90 degrees:

  1. Use select by expression with this expression: degrees (azimuth (start_point($geometry), end_point($geometry))) = 270.
  2. Than run Menu Processing / Toolbox / Reverse line direction and check the box next to Selected features only.

Explanation

Angle 90 degrees corresponds to a horizontal line drawn from left to right, 270 to a horizontal line drawn from right to left. Compare it to a clock: the minute hand at a quarter past (x:15 h, 1/4 of a full circle) corresponds to an azimuth of 90 degrees, the minute hand at a quarter to (x:45 h, 3/4 of a full circle) corresponds to 270 degrees. The difference between both values is 180 degrees. So all line are horizontal, just their direction (from start- to end-point) is different.

See also this here for Calculating interior angles of polygons or lines in QGIS

1
  • Thank you @babel!
    – GISNewbie
    Commented Jun 9, 2021 at 21:10
5

If using an expression to calculate this (whether for labelling, or in Field calculator), this is perfect instance to use the with_variable function to calculate the azimuth only once:

with_variable('azi',
  degrees(azimuth(start_point($geometry), end_point($geometry))),
  if(@azi=270,90,@azi)
)

Note the use of 'azi' in single quotes as the first parameter, where you need a string to define the desired variable name. Then in the 3rd parameter, you use @azi to refer to the variable value, which is defined in that scope using the expression that is the 2nd parameter.

The reason for doing all this is to avoid recalculating the azimuth multiple times in an if expression. This is useful in other instances as well, and even more important for more complicated expressions for both performance and ease-of-changing reasons.

3

As a label, use this expression:

if (
  degrees (azimuth (start_point ($geometry), end_point ($geometry))) = 270,
  degrees (azimuth (start_point (reverse ($geometry))), end_point (reverse ($geometry)))),
  degrees (azimuth (start_point ($geometry), end_point ($geometry)))
  )

where the condition checks if the angle of the line is 270 and then applies the reverse.

2
  • 1
    Good answer, but needlessly calculates (and retypes) degrees(azimuth(...) three times. Have posted an alternate answer using with_variable to bypass this (but upvoted this one for the logic.)
    – Houska
    Commented Jun 10, 2021 at 10:39
  • @Houska i am familiar with with_variable, but i wrote the solution quickly. Excellent your solution.
    – pigreco
    Commented Jun 10, 2021 at 13:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.