QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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)
QgsCoordinateTransform::transformInPlace
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...
Definition: qgscoordinatetransform.cpp:364
QgsCoordinateTransformContext
Contains information about the context in which a coordinate transform is executed.
Definition: qgscoordinatetransformcontext.h:57
QgsPoint
Point geometry type, with support for z-dimension and m-values.
Definition: qgspoint.h:48
QgsPoint::addZValue
bool addZValue(double zValue=0) override
Adds a z-dimension to the geometry, initialized to a preset value.
Definition: qgspoint.cpp:551
QgsCoordinateTransform::setContext
void setContext(const QgsCoordinateTransformContext &context)
Sets the context in which the coordinate transform should be calculated.
Definition: qgscoordinatetransform.cpp:230
QgsCoordinateTransform::setSourceCrs
void setSourceCrs(const QgsCoordinateReferenceSystem &crs)
Sets the source coordinate reference system.
Definition: qgscoordinatetransform.cpp:183
QgsCoordinateTransform::context
QgsCoordinateTransformContext context() const
Returns the context in which the coordinate transform will be calculated.
Definition: qgscoordinatetransform.cpp:257
QgsPoint::z
double z
Definition: qgspoint.h:71
QgsQuickCoordinateTransformer::destinationCrsChanged
void destinationCrsChanged()
Destination CRS.
QgsQuickCoordinateTransformer::sourcePositionChanged
void sourcePositionChanged()
Source position (in source CRS)
QgsQuickCoordinateTransformer::destinationCrs
QgsCoordinateReferenceSystem destinationCrs
Destination CRS.
Definition: qgsquickcoordinatetransformer.h:64
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:267
QgsPoint::y
double y
Definition: qgspoint.h:70
QgsCoordinateTransform::sourceCrs
QgsCoordinateReferenceSystem sourceCrs() const
Returns the source coordinate reference system, which the transform will transform coordinates from.
Definition: qgscoordinatetransform.cpp:262
QgsCsException
Custom exception class for Coordinate Reference System related exceptions.
Definition: qgsexception.h:65
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:206
QgsException::what
QString what() const
Definition: qgsexception.h:48
QgsCoordinateReferenceSystem
This class represents a coordinate reference system (CRS).
Definition: qgscoordinatereferencesystem.h:211
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:61
QgsQuickCoordinateTransformer::transformContext
QgsCoordinateTransformContext transformContext
Transformation context, can be set from QgsQuickMapSettings::transformContext()
Definition: qgsquickcoordinatetransformer.h:70
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:202
QgsQuickCoordinateTransformer::sourceCrs
QgsCoordinateReferenceSystem sourceCrs
Source CRS, default 4326.
Definition: qgsquickcoordinatetransformer.h:67
QgsQuickCoordinateTransformer::projectedPosition
QgsPoint projectedPosition
Projected (destination) position (in destination CRS)
Definition: qgsquickcoordinatetransformer.h:58
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
QgsPoint::x
double x
Definition: qgspoint.h:69
QgsQuickCoordinateTransformer::setSourceCrs
void setSourceCrs(const QgsCoordinateReferenceSystem &sourceCrs)
Source CRS, default 4326.
Definition: qgsquickcoordinatetransformer.cpp:66