Skip to content

Instantly share code, notes, and snippets.

@agiudiceandrea
Created August 17, 2023 18:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save agiudiceandrea/db1c87387ff4e16fb00c163a3d495de2 to your computer and use it in GitHub Desktop.
Save agiudiceandrea/db1c87387ff4e16fb00c163a3d495de2 to your computer and use it in GitHub Desktop.
from qgis.core import *
from qgis.gui import *
import math
@qgsfunction(args='auto', group='Custom', referenced_columns=[])
def bearing(point1, point2, feature, parent):
"""
Returns the bearing, on the EPSG:7030 (WGS 84) ellipsoid, in degrees,
clock-wise from North, between two points with coordinates
expressed in the EPSG:4326 (WGS 84) CRS.
<br>
<h2>Syntax</h2>
<p>bearing( point_a, point_b )</p>
<h2>Arguments</h2>
<p>point_a point geometry</p>
<p>point_b point geometry</p>
<h2>Examples</h2>
<ul>
<li>bearing( make_point( 16.123, 42.123 ), make_point( 16.345, 42.345 ) ) -> 36,543008086155425</li>
</ul>
"""
d = QgsDistanceArea()
d.setEllipsoid('WGS84')
return math.degrees(d.bearing(point1.asPoint(), point2.asPoint())) % 360
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment