QGIS API Documentation 3.41.0-Master (25ec5511245)
Loading...
Searching...
No Matches
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"
19#include "moc_qgsbearingutils.cpp"
22#include "qgspointxy.h"
24#include "qgsexception.h"
25
27{
28 // step 1 - transform point into WGS84 geographic crs
29 const QgsCoordinateTransform transform( crs, QgsCoordinateReferenceSystem::fromEpsgId( 4326 ), transformContext );
30
31 if ( !transform.isValid() )
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 const QgsPointXY p1 = transform.transform( point );
41
42 // shift point a tiny bit north
43 QgsPointXY p2 = p1;
44 p2.setY( p2.y() + 0.000001 );
45
46 //transform back
47 const QgsPointXY p3 = transform.transform( p2, Qgis::TransformDirection::Reverse );
48
49 // find bearing from point to p3
50 return point.azimuth( p3 );
51}
@ Reverse
Reverse/inverse transform (from destination to source)
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.
QgsPointXY transform(const QgsPointXY &point, Qgis::TransformDirection direction=Qgis::TransformDirection::Forward) const
Transform the point from the source CRS to the destination CRS.
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...
Defines a QGIS exception class.
A class to represent a 2D point.
Definition qgspointxy.h:60
void setY(double y)
Sets the y value of the point.
Definition qgspointxy.h:129
double azimuth(const QgsPointXY &other) const
Calculates azimuth between this point and other one (clockwise in degree, starting from north)
double y
Definition qgspointxy.h:64
const QgsCoordinateReferenceSystem & crs