Skip to content

Commit

Permalink
Fix QgsDistanceArea.bearing()
Browse files Browse the repository at this point in the history
This method had absolutely NO unit tests, and has been returning
NaN! ARGHHhhhhhhhh
  • Loading branch information
nyalldawson committed Jun 14, 2021
1 parent 78a6e32 commit 0b499d3
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/core/qgsdistancearea.cpp
Expand Up @@ -859,7 +859,7 @@ double QgsDistanceArea::bearing( const QgsPointXY &p1, const QgsPointXY &p2 ) co
double distance = 0;
double azimuth1 = 0;
double azimuth2 = 0;
geod_inverse( mGeod.get(), pp1.y(), pp1.x(), pp2.x(), pp2.y(), &distance, &azimuth1, &azimuth2 );
geod_inverse( mGeod.get(), pp1.y(), pp1.x(), pp2.y(), pp2.x(), &distance, &azimuth1, &azimuth2 );

bearing = DEG2RAD( azimuth1 );
}
Expand Down
14 changes: 14 additions & 0 deletions tests/src/python/test_qgsdistancearea.py
Expand Up @@ -63,6 +63,20 @@ def testMeasureLine(self):
(4, length))
assert length == 4, myMessage

def testBearing(self):
"""
Test bearing calculation
"""
da = QgsDistanceArea()
self.assertAlmostEqual(da.bearing(QgsPointXY(145.047, -37.578), QgsPointXY(168.38, -16.95)), 0.84685, 5)
self.assertAlmostEqual(da.bearing(QgsPointXY(-19.57, 65.12), QgsPointXY(-2.63, 54.97)), 2.11060792, 5)

da.setSourceCrs(QgsCoordinateReferenceSystem('EPSG:3857'), QgsProject.instance().transformContext())
da.setEllipsoid(da.sourceCrs().ellipsoidAcronym())
self.assertTrue(da.willUseEllipsoid())
self.assertAlmostEqual(da.bearing(QgsPointXY(16198544, -4534850), QgsPointXY(18736872, -1877769)), 0.8723168079, 5)
self.assertAlmostEqual(da.bearing(QgsPointXY(-2074453, 9559553), QgsPointXY(-55665, 6828252)), 2.35691008, 5)

def testMeasureLineProjected(self):
# +-+
# | |
Expand Down

0 comments on commit 0b499d3

Please sign in to comment.