QGIS API Documentation  3.8.0-Zanzibar (11aff65)
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 }
void setSourcePosition(const QgsPoint &sourcePosition)
Source position (in source CRS)
double y
Definition: qgspoint.h:42
void setContext(const QgsCoordinateTransformContext &context)
Sets the context in which the coordinate transform should be calculated.
void projectedPositionChanged()
Projected (destination) position (in destination CRS)
#define QgsDebugMsg(str)
Definition: qgslogger.h:38
QgsCoordinateTransformContext transformContext() const
Transformation context, can be set from QgsQuickMapSettings::transformContext()
QgsCoordinateReferenceSystem destinationCrs() const
Returns the destination coordinate reference system, which the transform will transform coordinates t...
void setSourceCrs(const QgsCoordinateReferenceSystem &sourceCrs)
Source CRS, default 4326.
void sourcePositionChanged()
Source position (in source CRS)
void transformContextChanged()
Transformation context, can be set from QgsQuickMapSettings::transformContext()
void destinationCrsChanged()
Destination CRS.
QString what() const
Definition: qgsexception.h:48
static Q_INVOKABLE QgsCoordinateReferenceSystem fromEpsgId(long epsg)
Creates a CRS from a given EPSG ID.
QgsCoordinateReferenceSystem sourceCrs() const
Returns the source coordinate reference system, which the transform will transform coordinates from...
void setDestinationCrs(const QgsCoordinateReferenceSystem &crs)
Sets the destination coordinate reference system.
QgsCoordinateTransformContext context() const
Returns the context in which the coordinate transform will be calculated.
QgsPoint sourcePosition() const
Source position (in source CRS)
QgsPoint projectedPosition() const
Projected (destination) position (in destination CRS)
Contains information about the context in which a coordinate transform is executed.
Point geometry type, with support for z-dimension and m-values.
Definition: qgspoint.h:37
void setTransformContext(const QgsCoordinateTransformContext &context)
Transformation context, can be set from QgsQuickMapSettings::transformContext()
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...
QgsCoordinateReferenceSystem destinationCrs() const
Destination CRS.
QgsQuickCoordinateTransformer(QObject *parent=nullptr)
Creates new coordinate transformer.
This class represents a coordinate reference system (CRS).
void sourceCrsChanged()
Source CRS, default 4326.
double z
Definition: qgspoint.h:43
Custom exception class for Coordinate Reference System related exceptions.
Definition: qgsexception.h:65
void setDestinationCrs(const QgsCoordinateReferenceSystem &destinationCrs)
Destination CRS.
void setSourceCrs(const QgsCoordinateReferenceSystem &crs)
Sets the source coordinate reference system.
QgsCoordinateReferenceSystem sourceCrs() const
Source CRS, default 4326.
double x
Definition: qgspoint.h:41