QGIS API Documentation 3.99.0-Master (2fe06baccd8)
Loading...
Searching...
No Matches
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
18#include "qgslogger.h"
19
20#include "moc_qgsquickcoordinatetransformer.cpp"
21
23 : QObject( parent )
24{
25 mCoordinateTransform.setSourceCrs( QgsCoordinateReferenceSystem::fromEpsgId( 4326 ) );
26}
27
29{
30 return mProjectedPosition;
31}
32
34{
35 return mSourcePosition;
36}
37
39{
40 if ( mSourcePosition == sourcePosition )
41 return;
42
43 mSourcePosition = sourcePosition;
44
46 updatePosition();
47}
48
50{
51 return mCoordinateTransform.destinationCrs();
52}
53
55{
56 if ( destinationCrs == mCoordinateTransform.destinationCrs() )
57 return;
58
59 mCoordinateTransform.setDestinationCrs( destinationCrs );
61 updatePosition();
62}
63
65{
66 return mCoordinateTransform.sourceCrs();
67}
68
70{
71 if ( sourceCrs == mCoordinateTransform.sourceCrs() )
72 return;
73
74 mCoordinateTransform.setSourceCrs( sourceCrs );
75
76 emit sourceCrsChanged();
77 updatePosition();
78}
79
81{
82 mCoordinateTransform.setContext( context );
84}
85
87{
88 return mCoordinateTransform.context();
89}
90
91void QgsQuickCoordinateTransformer::updatePosition()
92{
93 double x = mSourcePosition.x();
94 double y = mSourcePosition.y();
95 double z = mSourcePosition.z();
96
97 // If Z is NaN, coordinate transformation (proj4) will
98 // also set X and Y to NaN. But we also want to get projected
99 // coords if we do not have any Z coordinate.
100 if ( std::isnan( z ) )
101 {
102 z = 0;
103 }
104
105 try
106 {
107 mCoordinateTransform.transformInPlace( x, y, z );
108 }
109 catch ( const QgsCsException &exp )
110 {
111 QgsDebugError( exp.what() );
112 }
113
114 mProjectedPosition = QgsPoint( x, y );
115 mProjectedPosition.addZValue( mSourcePosition.z() );
116
118}
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.
QString what() const
Point geometry type, with support for z-dimension and m-values.
Definition qgspoint.h:49
double z
Definition qgspoint.h:54
double x
Definition qgspoint.h:52
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 QgsDebugError(str)
Definition qgslogger.h:57