QGIS API Documentation 3.41.0-Master (cea29feecf2)
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#include "moc_qgsquickcoordinatetransformer.cpp"
18#include "qgslogger.h"
19
21 : QObject( parent )
22{
23 mCoordinateTransform.setSourceCrs( QgsCoordinateReferenceSystem::fromEpsgId( 4326 ) );
24}
25
27{
28 return mProjectedPosition;
29}
30
32{
33 return mSourcePosition;
34}
35
37{
38 if ( mSourcePosition == sourcePosition )
39 return;
40
41 mSourcePosition = sourcePosition;
42
44 updatePosition();
45}
46
51
53{
54 if ( destinationCrs == mCoordinateTransform.destinationCrs() )
55 return;
56
57 mCoordinateTransform.setDestinationCrs( destinationCrs );
59 updatePosition();
60}
61
66
68{
69 if ( sourceCrs == mCoordinateTransform.sourceCrs() )
70 return;
71
72 mCoordinateTransform.setSourceCrs( sourceCrs );
73
74 emit sourceCrsChanged();
75 updatePosition();
76}
77
79{
80 mCoordinateTransform.setContext( context );
82}
83
88
89void QgsQuickCoordinateTransformer::updatePosition()
90{
91 double x = mSourcePosition.x();
92 double y = mSourcePosition.y();
93 double z = mSourcePosition.z();
94
95 // If Z is NaN, coordinate transformation (proj4) will
96 // also set X and Y to NaN. But we also want to get projected
97 // coords if we do not have any Z coordinate.
98 if ( std::isnan( z ) )
99 {
100 z = 0;
101 }
102
103 try
104 {
105 mCoordinateTransform.transformInPlace( x, y, z );
106 }
107 catch ( const QgsCsException &exp )
108 {
109 QgsDebugError( exp.what() );
110 }
111
112 mProjectedPosition = QgsPoint( x, y );
113 mProjectedPosition.addZValue( mSourcePosition.z() );
114
116}
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
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.
QString what() const
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:558
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:38