QGIS API Documentation  3.22.4-Białowieża (ce8e65e95e)
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  const 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  const 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
46  const QgsPointXY p3 = transform.transform( p2, Qgis::TransformDirection::Reverse );
47 
48  // find bearing from point to p3
49  return point.azimuth( p3 );
50 }
static Q_INVOKABLE 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...
This class represents a coordinate reference system (CRS).
static Q_INVOKABLE QgsCoordinateReferenceSystem fromEpsgId(long epsg)
Creates a CRS from a given EPSG ID.
Contains information about the context in which a coordinate transform is executed.
Class for doing transforms between two map coordinate systems.
bool isShortCircuited() const
Returns true if the transform short circuits because the source and destination are equivalent.
bool isValid() const
Returns true if the coordinate transform is valid, ie both the source and destination CRS have been s...
QgsPointXY transform(const QgsPointXY &point, Qgis::TransformDirection direction=Qgis::TransformDirection::Forward) const SIP_THROW(QgsCsException)
Transform the point from the source CRS to the destination CRS.
Defines a QGIS exception class.
Definition: qgsexception.h:35
A class to represent a 2D point.
Definition: qgspointxy.h:59
double azimuth(const QgsPointXY &other) const SIP_HOLDGIL
Calculates azimuth between this point and other one (clockwise in degree, starting from north)
Definition: qgspointxy.cpp:80
double y
Definition: qgspointxy.h:63
void setY(double y) SIP_HOLDGIL
Sets the y value of the point.
Definition: qgspointxy.h:132
const QgsCoordinateReferenceSystem & crs