QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
qgsbearingutils.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsbearingutils.cpp
3  -------------------
4  begin : October 2016
5  copyright : (C) 2016 by Nyall Dawson
6  email : nyall dot dawson at gmail dot com
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
18 #include "qgsbearingutils.h"
20 #include "qgspoint.h"
21 #include "qgscoordinatetransform.h"
22 #include "qgsexception.h"
23 
25 {
26  // step 1 - transform point into WGS84 geographic crs
28  destCrs.createFromOgcWmsCrs( "EPSG:4326" );
29  QgsCoordinateTransform transform( crs, destCrs );
30 
31  if ( !transform.isInitialised() )
32  {
33  //raise
34  throw QgsException( QObject::tr( "Could not create transform to calculate true north" ) );
35  }
36 
37  if ( transform.isShortCircuited() )
38  return 0.0;
39 
40  QgsPoint p1 = transform.transform( point );
41 
42  // shift point a tiny bit north
43  QgsPoint p2 = p1;
44  p2.setY( p2.y() + 0.000001 );
45 
46  //transform back
48 
49  // find bearing from point to p3
50  return point.azimuth( p3 );
51 }
QgsPoint transform(const QgsPoint &p, TransformDirection direction=ForwardTransform) const
Transform the point from Source Coordinate System to Destination Coordinate System If the direction i...
double azimuth(const QgsPoint &other) const
Calculates azimuth between this point and other one (clockwise in degree, starting from north) ...
Definition: qgspoint.cpp:373
bool createFromOgcWmsCrs(const QString &theCrs)
Sets this CRS to the given OGC WMS-format Coordinate Reference Systems.
QString tr(const char *sourceText, const char *disambiguation, int n)
double y() const
Get the y value of the point.
Definition: qgspoint.h:193
bool isShortCircuited() const
See if the transform short circuits because src and dest are equivalent.
A class to represent a point.
Definition: qgspoint.h:117
void setY(double y)
Sets the y value of the point.
Definition: qgspoint.h:170
Class for storing a coordinate reference system (CRS)
Class for doing transforms between two map coordinate systems.
static double bearingTrueNorth(const QgsCoordinateReferenceSystem &crs, const QgsPoint &point)
Returns the direction to true north from a specified point and for a specified coordinate reference s...
Defines a qgis exception class.
Definition: qgsexception.h:25