Skip to content
Permalink
Browse files

add trandform context to QgsBearingUtils::bearingTrueNorth

  • Loading branch information
nirvn committed Jan 30, 2018
1 parent 0a9f019 commit eb42afed9bdf4374e936233ae9e7d4a82a12baef
@@ -24,6 +24,7 @@ Utilities for calculating bearings and directions.
public:

static double bearingTrueNorth( const QgsCoordinateReferenceSystem &crs,
const QgsCoordinateTransformContext &transformContext,
const QgsPointXY &point );
%Docstring
Returns the direction to true north from a specified point and for a specified
@@ -125,7 +125,7 @@ void QgsDecorationNorthArrow::render( const QgsMapSettings &mapSettings, QgsRend
{
try
{
mRotationInt = QgsBearingUtils:: bearingTrueNorth( mapSettings.destinationCrs(), context.extent().center() );
mRotationInt = QgsBearingUtils:: bearingTrueNorth( mapSettings.destinationCrs(), mapSettings.transformContext(), context.extent().center() );
}
catch ( QgsException & )
{
@@ -463,10 +463,11 @@ void QgsLayoutItemPicture::updateMapRotation()
{
QgsPointXY center = mRotationMap->extent().center();
QgsCoordinateReferenceSystem crs = mRotationMap->crs();
QgsCoordinateTransformContext transformContext = mLayout->project()->transformContext();

try
{
double bearing = QgsBearingUtils::bearingTrueNorth( crs, center );
double bearing = QgsBearingUtils::bearingTrueNorth( crs, transformContext, center );
rotation += bearing;
}
catch ( QgsException &e )
@@ -17,16 +17,15 @@

#include "qgsbearingutils.h"
#include "qgscoordinatereferencesystem.h"
#include "qgscoordinatetransformcontext.h"
#include "qgspointxy.h"
#include "qgscoordinatetransform.h"
#include "qgsexception.h"

double QgsBearingUtils::bearingTrueNorth( const QgsCoordinateReferenceSystem &crs, const QgsPointXY &point )
double QgsBearingUtils::bearingTrueNorth( const QgsCoordinateReferenceSystem &crs, const QgsCoordinateTransformContext &transformContext, const QgsPointXY &point )
{
// step 1 - transform point into WGS84 geographic crs
Q_NOWARN_DEPRECATED_PUSH
QgsCoordinateTransform transform( crs, QgsCoordinateReferenceSystem::fromEpsgId( 4326 ) );
Q_NOWARN_DEPRECATED_POP
QgsCoordinateTransform transform( crs, QgsCoordinateReferenceSystem::fromEpsgId( 4326 ), transformContext );

if ( !transform.isValid() )
{
@@ -19,6 +19,7 @@
#define QGSBEARINGUTILS_H

class QgsCoordinateReferenceSystem;
class QgsCoordinateTransformContext;
class QgsPointXY;

#include "qgis_core.h"
@@ -39,6 +40,7 @@ class CORE_EXPORT QgsBearingUtils
* vertical. An exception will be thrown if the bearing could not be calculated.
*/
static double bearingTrueNorth( const QgsCoordinateReferenceSystem &crs,
const QgsCoordinateTransformContext &transformContext,
const QgsPointXY &point );

};
@@ -16,6 +16,7 @@

from qgis.core import (QgsBearingUtils,
QgsCoordinateReferenceSystem,
QgsCoordinateTransformContext,
QgsPointXY
)

@@ -32,26 +33,28 @@ def testTrueNorth(self):

# short circuit - already a geographic crs
crs = QgsCoordinateReferenceSystem.fromEpsgId(4326)
self.assertEqual(QgsBearingUtils.bearingTrueNorth(crs, QgsPointXY(0, 0)), 0)
self.assertEqual(QgsBearingUtils.bearingTrueNorth(crs, QgsPointXY(44, 0)), 0)
self.assertEqual(QgsBearingUtils.bearingTrueNorth(crs, QgsPointXY(44, -43)), 0)
self.assertEqual(QgsBearingUtils.bearingTrueNorth(crs, QgsPointXY(44, 43)), 0)
transformContext = QgsCoordinateTransformContext()

self.assertEqual(QgsBearingUtils.bearingTrueNorth(crs, QgsPointXY(44, 200)), 0)
self.assertEqual(QgsBearingUtils.bearingTrueNorth(crs, QgsPointXY(44, -200)), 0)
self.assertEqual(QgsBearingUtils.bearingTrueNorth(crs, transformContext, QgsPointXY(0, 0)), 0)
self.assertEqual(QgsBearingUtils.bearingTrueNorth(crs, transformContext, QgsPointXY(44, 0)), 0)
self.assertEqual(QgsBearingUtils.bearingTrueNorth(crs, transformContext, QgsPointXY(44, -43)), 0)
self.assertEqual(QgsBearingUtils.bearingTrueNorth(crs, transformContext, QgsPointXY(44, 43)), 0)

self.assertEqual(QgsBearingUtils.bearingTrueNorth(crs, transformContext, QgsPointXY(44, 200)), 0)
self.assertEqual(QgsBearingUtils.bearingTrueNorth(crs, transformContext, QgsPointXY(44, -200)), 0)

# no short circuit
crs = QgsCoordinateReferenceSystem.fromEpsgId(3111)
self.assertAlmostEqual(QgsBearingUtils.bearingTrueNorth(crs, QgsPointXY(2508807, 2423425)), 0.06, 2)
self.assertAlmostEqual(QgsBearingUtils.bearingTrueNorth(crs, transformContext, QgsPointXY(2508807, 2423425)), 0.06, 2)

# try a south-up crs
crs = QgsCoordinateReferenceSystem.fromEpsgId(2053)
self.assertAlmostEqual(QgsBearingUtils.bearingTrueNorth(crs, QgsPointXY(29, -27.55)), -180.0, 1)
self.assertAlmostEqual(QgsBearingUtils.bearingTrueNorth(crs, transformContext, QgsPointXY(29, -27.55)), -180.0, 1)

# try a north pole crs
crs = QgsCoordinateReferenceSystem.fromEpsgId(3575)
self.assertAlmostEqual(QgsBearingUtils.bearingTrueNorth(crs, QgsPointXY(-780770, 652329)), 129.9, 1)
self.assertAlmostEqual(QgsBearingUtils.bearingTrueNorth(crs, QgsPointXY(513480, 873173)), -149.5, 1)
self.assertAlmostEqual(QgsBearingUtils.bearingTrueNorth(crs, transformContext, QgsPointXY(-780770, 652329)), 129.9, 1)
self.assertAlmostEqual(QgsBearingUtils.bearingTrueNorth(crs, transformContext, QgsPointXY(513480, 873173)), -149.5, 1)


if __name__ == '__main__':

0 comments on commit eb42afe

Please sign in to comment.
You can’t perform that action at this time.