Thursday 27 April 2017

How to calculate azimuth in QGIS field calculator?



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

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...