QGIS API Documentation  3.6.0-Noosa (5873452)
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"
21 #include "qgspointxy.h"
22 #include "qgscoordinatetransform.h"
23 #include "qgsexception.h"
24 
26 {
27  // step 1 - transform point into WGS84 geographic crs
28  QgsCoordinateTransform transform( crs, QgsCoordinateReferenceSystem::fromEpsgId( 4326 ), transformContext );
29 
30  if ( !transform.isValid() )
31  {
32  //raise
33  throw QgsException( QObject::tr( "Could not create transform to calculate true north" ) );
34  }
35 
36  if ( transform.isShortCircuited() )
37  return 0.0;
38 
39  QgsPointXY p1 = transform.transform( point );
40 
41  // shift point a tiny bit north
42  QgsPointXY p2 = p1;
43  p2.setY( p2.y() + 0.000001 );
44 
45  //transform back
47 
48  // find bearing from point to p3
49  return point.azimuth( p3 );
50 }
static double bearingTrueNorth(const QgsCoordinateReferenceSystem &crs, const QgsCoordinateTransformContext &transformContext, const QgsPointXY &point)
Returns the direction to true north from a specified point and for a specified coordinate reference s...
QgsPointXY transform(const QgsPointXY &point, TransformDirection direction=ForwardTransform) const SIP_THROW(QgsCsException)
Transform the point from the source CRS to the destination CRS.
double y
Definition: qgspointxy.h:48
A class to represent a 2D point.
Definition: qgspointxy.h:43
const QgsCoordinateReferenceSystem & crs
bool isValid() const
Returns true if the coordinate transform is valid, ie both the source and destination CRS have been s...
double azimuth(const QgsPointXY &other) const
Calculates azimuth between this point and other one (clockwise in degree, starting from north) ...
Definition: qgspointxy.cpp:63
static Q_INVOKABLE QgsCoordinateReferenceSystem fromEpsgId(long epsg)
Creates a CRS from a given EPSG ID.
bool isShortCircuited() const
Returns true if the transform short circuits because the source and destination are equivalent...
void setY(double y)
Sets the y value of the point.
Definition: qgspointxy.h:113
Contains information about the context in which a coordinate transform is executed.
Transform from destination to source CRS.
This class represents a coordinate reference system (CRS).
Class for doing transforms between two map coordinate systems.
Defines a QGIS exception class.
Definition: qgsexception.h:34