Vector intersection - angles¶
You can calculate bearing in the Field Calculator in QGIS using the xat(n) yat(n) funtions. This works in UTM (metric) coordinates over small distances (hundreds of km), but something more sophisticated would be needed for large distances or for decimal degrees.
Open the attribute table for your line layer, toggle editing, and click the Field Calculator button to open the dialog:
Create a new field as decimal with 1 or 2 precision.
Paste this code into the "Expression" box, and click "OK": (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)))
The first part calculates the inverse tangent of the x and y differences and converts it to degrees (180/pi). The second part adds either 180 or 360 to the resulting figure to give a bearing from 0-360°.