QGIS API Documentation  3.14.0-Pi (9f7028fd23)
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 *
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 }
QgsPointXY::y
double y
Definition: qgspointxy.h:48
QgsException
Definition: qgsexception.h:34
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
QgsCoordinateTransform::transform
QgsPointXY transform(const QgsPointXY &point, TransformDirection direction=ForwardTransform) const SIP_THROW(QgsCsException)
Transform the point from the source CRS to the destination CRS.
Definition: qgscoordinatetransform.cpp:239
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:876
QgsCoordinateTransform::ReverseTransform
@ ReverseTransform
Transform from destination to source CRS.
Definition: qgscoordinatetransform.h:61
QgsCoordinateTransform::isShortCircuited
bool isShortCircuited() const
Returns true if the transform short circuits because the source and destination are equivalent.
Definition: qgscoordinatetransform.cpp:881
QgsPointXY::azimuth
double azimuth(const QgsPointXY &other) const
Calculates azimuth between this point and other one (clockwise in degree, starting from north)
Definition: qgspointxy.cpp:80
QgsPointXY::setY
void setY(double y)
Sets the y value of the point.
Definition: qgspointxy.h:117
qgscoordinatetransform.h
QgsPointXY
Definition: qgspointxy.h:43
qgsbearingutils.h
qgsexception.h
QgsCoordinateReferenceSystem::fromEpsgId
static Q_INVOKABLE QgsCoordinateReferenceSystem fromEpsgId(long epsg)
Creates a CRS from a given EPSG ID.
Definition: qgscoordinatereferencesystem.cpp:207
qgscoordinatereferencesystem.h
qgspointxy.h