QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
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 }
QgsQuickCoordinateTransformer::projectedPositionChanged
void projectedPositionChanged()
Projected (destination) position (in destination CRS)
QgsCoordinateTransformContext
Contains information about the context in which a coordinate transform is executed.
Definition: qgscoordinatetransformcontext.h:58
QgsPoint
Point geometry type, with support for z-dimension and m-values.
Definition: qgspoint.h:38
QgsPoint::addZValue
bool addZValue(double zValue=0) override
Adds a z-dimension to the geometry, initialized to a preset value.
Definition: qgspoint.cpp:541
QgsCoordinateTransform::setContext
void setContext(const QgsCoordinateTransformContext &context)
Sets the context in which the coordinate transform should be calculated.
Definition: qgscoordinatetransform.cpp:200
QgsCoordinateTransform::setSourceCrs
void setSourceCrs(const QgsCoordinateReferenceSystem &crs)
Sets the source coordinate reference system.
Definition: qgscoordinatetransform.cpp:159
QgsCoordinateTransform::context
QgsCoordinateTransformContext context() const
Returns the context in which the coordinate transform will be calculated.
Definition: qgscoordinatetransform.cpp:224
QgsPoint::z
double z
Definition: qgspoint.h:43
QgsQuickCoordinateTransformer::destinationCrsChanged
void destinationCrsChanged()
Destination CRS.
QgsQuickCoordinateTransformer::sourcePositionChanged
void sourcePositionChanged()
Source position (in source CRS)
QgsQuickCoordinateTransformer::destinationCrs
QgsCoordinateReferenceSystem destinationCrs
Destination CRS.
Definition: qgsquickcoordinatetransformer.h:50
QgsDebugMsg
#define QgsDebugMsg(str)
Definition: qgslogger.h:38
QgsCoordinateTransform::destinationCrs
QgsCoordinateReferenceSystem destinationCrs() const
Returns the destination coordinate reference system, which the transform will transform coordinates t...
Definition: qgscoordinatetransform.cpp:234
QgsPoint::y
double y
Definition: qgspoint.h:42
QgsCoordinateTransform::sourceCrs
QgsCoordinateReferenceSystem sourceCrs() const
Returns the source coordinate reference system, which the transform will transform coordinates from.
Definition: qgscoordinatetransform.cpp:229
QgsCsException
Custom exception class for Coordinate Reference System related exceptions.
Definition: qgsexception.h:66
QgsQuickCoordinateTransformer::setTransformContext
void setTransformContext(const QgsCoordinateTransformContext &context)
Transformation context, can be set from QgsQuickMapSettings::transformContext()
Definition: qgsquickcoordinatetransformer.cpp:77
QgsCoordinateTransform::setDestinationCrs
void setDestinationCrs(const QgsCoordinateReferenceSystem &crs)
Sets the destination coordinate reference system.
Definition: qgscoordinatetransform.cpp:179
QgsException::what
QString what() const
Definition: qgsexception.h:48
QgsPoint::x
Q_GADGET double x
Definition: qgspoint.h:41
QgsCoordinateReferenceSystem
This class represents a coordinate reference system (CRS).
Definition: qgscoordinatereferencesystem.h:206
qgsquickcoordinatetransformer.h
QgsQuickCoordinateTransformer::setDestinationCrs
void setDestinationCrs(const QgsCoordinateReferenceSystem &destinationCrs)
Destination CRS.
Definition: qgsquickcoordinatetransformer.cpp:51
QgsQuickCoordinateTransformer::sourcePosition
QgsPoint sourcePosition
Source position (in source CRS)
Definition: qgsquickcoordinatetransformer.h:47
QgsQuickCoordinateTransformer::transformContext
QgsCoordinateTransformContext transformContext
Transformation context, can be set from QgsQuickMapSettings::transformContext()
Definition: qgsquickcoordinatetransformer.h:56
QgsQuickCoordinateTransformer::transformContextChanged
void transformContextChanged()
Transformation context, can be set from QgsQuickMapSettings::transformContext()
QgsCoordinateReferenceSystem::fromEpsgId
static Q_INVOKABLE QgsCoordinateReferenceSystem fromEpsgId(long epsg)
Creates a CRS from a given EPSG ID.
Definition: qgscoordinatereferencesystem.cpp:207
QgsQuickCoordinateTransformer::sourceCrs
QgsCoordinateReferenceSystem sourceCrs
Source CRS, default 4326.
Definition: qgsquickcoordinatetransformer.h:53
QgsQuickCoordinateTransformer::projectedPosition
QgsPoint projectedPosition
Projected (destination) position (in destination CRS)
Definition: qgsquickcoordinatetransformer.h:44
qgslogger.h
QgsQuickCoordinateTransformer::sourceCrsChanged
void sourceCrsChanged()
Source CRS, default 4326.
QgsQuickCoordinateTransformer::setSourcePosition
void setSourcePosition(const QgsPoint &sourcePosition)
Source position (in source CRS)
Definition: qgsquickcoordinatetransformer.cpp:35
QgsQuickCoordinateTransformer::QgsQuickCoordinateTransformer
QgsQuickCoordinateTransformer(QObject *parent=nullptr)
Creates new coordinate transformer.
Definition: qgsquickcoordinatetransformer.cpp:19
QgsQuickCoordinateTransformer::setSourceCrs
void setSourceCrs(const QgsCoordinateReferenceSystem &sourceCrs)
Source CRS, default 4326.
Definition: qgsquickcoordinatetransformer.cpp:66
QgsCoordinateTransform::transformInPlace
void transformInPlace(double &x, double &y, double &z, TransformDirection direction=ForwardTransform) const SIP_THROW(QgsCsException)
Transforms an array of x, y and z double coordinates in place, from the source CRS to the destination...
Definition: qgscoordinatetransform.cpp:313