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
Answer
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.
No comments:
Post a Comment