QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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 }
QgsPointXY::y
double y
Definition: qgspointxy.h:63
QgsException
Defines a QGIS exception class.
Definition: qgsexception.h:34
QgsCoordinateTransformContext
Contains information about the context in which a coordinate transform is executed.
Definition: qgscoordinatetransformcontext.h:57
QgsBearingUtils::bearingTrueNorth
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...
Definition: qgsbearingutils.cpp:25
qgscoordinatetransformcontext.h
crs
const QgsCoordinateReferenceSystem & crs
Definition: qgswfsgetfeature.cpp:105
QgsPointXY::setY
void setY(double y) SIP_HOLDGIL
Sets the y value of the point.
Definition: qgspointxy.h:132
QgsCoordinateTransform::isValid
bool isValid() const
Returns true if the coordinate transform is valid, ie both the source and destination CRS have been s...
Definition: qgscoordinatetransform.cpp:900
QgsCoordinateTransform::isShortCircuited
bool isShortCircuited() const
Returns true if the transform short circuits because the source and destination are equivalent.
Definition: qgscoordinatetransform.cpp:905
qgscoordinatetransform.h
QgsCoordinateReferenceSystem
This class represents a coordinate reference system (CRS).
Definition: qgscoordinatereferencesystem.h:211
QgsPointXY
A class to represent a 2D point.
Definition: qgspointxy.h:58
QgsPointXY::azimuth
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
qgsbearingutils.h
qgsexception.h
QgsCoordinateReferenceSystem::fromEpsgId
static Q_INVOKABLE QgsCoordinateReferenceSystem fromEpsgId(long epsg)
Creates a CRS from a given EPSG ID.
Definition: qgscoordinatereferencesystem.cpp:202
QgsCoordinateTransform::transform
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.
Definition: qgscoordinatetransform.cpp:272
QgsCoordinateTransform
Class for doing transforms between two map coordinate systems.
Definition: qgscoordinatetransform.h:57
qgscoordinatereferencesystem.h
qgspointxy.h