QGIS API Documentation  3.23.0-Master (7c4a6de034)
qgsquickcoordinatetransformer.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsquickcoordinatetransformer.cpp
3  --------------------------------------
4  Date : 1.6.2017
5  Copyright : (C) 2017 by Matthias Kuhn
6  Email : matthias (at) opengis.ch
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 
17 #include "qgslogger.h"
18 
20  : QObject( parent )
21 {
22  mCoordinateTransform.setSourceCrs( QgsCoordinateReferenceSystem::fromEpsgId( 4326 ) );
23 }
24 
26 {
27  return mProjectedPosition;
28 }
29 
31 {
32  return mSourcePosition;
33 }
34 
36 {
37  if ( mSourcePosition == sourcePosition )
38  return;
39 
40  mSourcePosition = sourcePosition;
41 
42  emit sourcePositionChanged();
43  updatePosition();
44 }
45 
47 {
48  return mCoordinateTransform.destinationCrs();
49 }
50 
52 {
53  if ( destinationCrs == mCoordinateTransform.destinationCrs() )
54  return;
55 
56  mCoordinateTransform.setDestinationCrs( destinationCrs );
57  emit destinationCrsChanged();
58  updatePosition();
59 }
60 
62 {
63  return mCoordinateTransform.sourceCrs();
64 }
65 
67 {
68  if ( sourceCrs == mCoordinateTransform.sourceCrs() )
69  return;
70 
71  mCoordinateTransform.setSourceCrs( sourceCrs );
72 
73  emit sourceCrsChanged();
74  updatePosition();
75 }
76 
78 {
79  mCoordinateTransform.setContext( context );
81 }
82 
84 {
85  return mCoordinateTransform.context();
86 }
87 
88 void QgsQuickCoordinateTransformer::updatePosition()
89 {
90  double x = mSourcePosition.x();
91  double y = mSourcePosition.y();
92  double z = mSourcePosition.z();
93 
94  // If Z is NaN, coordinate transformation (proj4) will
95  // also set X and Y to NaN. But we also want to get projected
96  // coords if we do not have any Z coordinate.
97  if ( std::isnan( z ) )
98  {
99  z = 0;
100  }
101 
102  try
103  {
104  mCoordinateTransform.transformInPlace( x, y, z );
105  }
106  catch ( const QgsCsException &exp )
107  {
108  QgsDebugMsg( exp.what() );
109  }
110 
111  mProjectedPosition = QgsPoint( x, y );
112  mProjectedPosition.addZValue( mSourcePosition.z() );
113 
115 }
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.
QgsCoordinateTransformContext context() const
Returns the context in which the coordinate transform will be calculated.
QgsCoordinateReferenceSystem sourceCrs() const
Returns the source coordinate reference system, which the transform will transform coordinates from.
void setContext(const QgsCoordinateTransformContext &context)
Sets the context in which the coordinate transform should be calculated.
void setSourceCrs(const QgsCoordinateReferenceSystem &crs)
Sets the source coordinate reference system.
void setDestinationCrs(const QgsCoordinateReferenceSystem &crs)
Sets the destination coordinate reference system.
void transformInPlace(double &x, double &y, double &z, Qgis::TransformDirection direction=Qgis::TransformDirection::Forward) const SIP_THROW(QgsCsException)
Transforms an array of x, y and z double coordinates in place, from the source CRS to the destination...
QgsCoordinateReferenceSystem destinationCrs() const
Returns the destination coordinate reference system, which the transform will transform coordinates t...
Custom exception class for Coordinate Reference System related exceptions.
Definition: qgsexception.h:66
QString what() const
Definition: qgsexception.h:48
Point geometry type, with support for z-dimension and m-values.
Definition: qgspoint.h:49
bool addZValue(double zValue=0) override
Adds a z-dimension to the geometry, initialized to a preset value.
Definition: qgspoint.cpp:551
Q_GADGET double x
Definition: qgspoint.h:52
double z
Definition: qgspoint.h:54
double y
Definition: qgspoint.h:53
QgsCoordinateReferenceSystem sourceCrs
Source CRS, default 4326.
QgsCoordinateReferenceSystem destinationCrs
Destination CRS.
void transformContextChanged()
Transformation context, can be set from QgsQuickMapSettings::transformContext()
void projectedPositionChanged()
Projected (destination) position (in destination CRS)
void sourcePositionChanged()
Source position (in source CRS)
void setTransformContext(const QgsCoordinateTransformContext &context)
Transformation context, can be set from QgsQuickMapSettings::transformContext()
QgsCoordinateTransformContext transformContext
Transformation context, can be set from QgsQuickMapSettings::transformContext()
QgsQuickCoordinateTransformer(QObject *parent=nullptr)
Creates new coordinate transformer.
void sourceCrsChanged()
Source CRS, default 4326.
void setSourceCrs(const QgsCoordinateReferenceSystem &sourceCrs)
Source CRS, default 4326.
QgsPoint projectedPosition
Projected (destination) position (in destination CRS)
QgsPoint sourcePosition
Source position (in source CRS)
void destinationCrsChanged()
Destination CRS.
void setSourcePosition(const QgsPoint &sourcePosition)
Source position (in source CRS)
void setDestinationCrs(const QgsCoordinateReferenceSystem &destinationCrs)
Destination CRS.
#define QgsDebugMsg(str)
Definition: qgslogger.h:38