Is there any way to calculate the azimuth of two points in the QGIS field calculator? The coordinates are stored in an attribute table.
x1 | y1 | x2 | y2
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It only takes a minute to sign up.Sign up to join this community
You can use function editor tab in the field calculator and make your azimuth function like this:
from qgis.core import * from qgis.gui import * @qgsfunction(args="auto", group='Custom') def azimuth(x1,y1,x2,y2,feature,parent): p1 = QgsPoint(x1,y1) p2 = QgsPoint(x2,y2) a = p1.azimuth(p2) if a < 0: a += 360 return a
Once you run/save it (and just to make sure restart the calculator) you should be able to use this function in the expression tab either by typing it or selecting from Functions list under custom as:
azimuth(x1,y1,x2,y2) #or variation azimuth("x1","y1","x2","y2")
All credit for this goes to Anita Graser and her How to create illuminated contours, Tanaka-style.
In your original question, you have points stored as X1, Y1 and X2, Y2. However, you can also automatically calculate azimuth without requiring endpoints to lines using this function:
and if you would like to round it off, use:
This is highly useful for multipart polyline files, without any requirement to calculate endpoints to lines (as was required in previous versions of QGIS, and in other 3D CAD software).