QGIS API Documentation 3.39.0-Master (67e056379ed)
Loading...
Searching...
No Matches
qgscoordinatetransform.h
Go to the documentation of this file.
1/***************************************************************************
2 qgscoordinatetransform.h - Coordinate Transforms
3 ------------------------
4 begin : Dec 2004
5 copyright : (C) 2004 Tim Sutton
6 email : tim at linfiniti.com
7 ***************************************************************************/
8
9/***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17#ifndef QGSCOORDINATETRANSFORM_H
18#define QGSCOORDINATETRANSFORM_H
19
20#include <QExplicitlySharedDataPointer>
21
22#include "qgsconfig.h"
23#include "qgis_core.h"
24#include "qgis_sip.h"
27
28class QgsCoordinateTransformPrivate;
29class QgsPointXY;
30class QgsRectangle;
31class QPolygonF;
32class QgsProject;
33class QgsVector3D;
34
57class CORE_EXPORT QgsCoordinateTransform
58{
59
60 public:
61
64
92 const QgsCoordinateReferenceSystem &destination,
93 const QgsCoordinateTransformContext &context,
95
122 const QgsCoordinateReferenceSystem &destination,
123 const QgsProject *project,
125
133 Q_DECL_DEPRECATED explicit QgsCoordinateTransform( const QgsCoordinateReferenceSystem &source,
134 const QgsCoordinateReferenceSystem &destination,
135 int sourceDatumTransformId,
136 int destinationDatumTransformId ) SIP_DEPRECATED;
137
142
146 QgsCoordinateTransform &operator=( const QgsCoordinateTransform &o );
147
149
162 static bool isTransformationPossible( const QgsCoordinateReferenceSystem &source, const QgsCoordinateReferenceSystem &destination );
163
168 bool isValid() const;
169
176 void setSourceCrs( const QgsCoordinateReferenceSystem &crs );
177
184 void setDestinationCrs( const QgsCoordinateReferenceSystem &crs );
185
191 void setContext( const QgsCoordinateTransformContext &context );
192
199 QgsCoordinateTransformContext context() const;
200
207 QgsCoordinateReferenceSystem sourceCrs() const;
208
215 QgsCoordinateReferenceSystem destinationCrs() const;
216
227
238 QgsPointXY transform( double x, double y, Qgis::TransformDirection direction = Qgis::TransformDirection::Forward ) const SIP_THROW( QgsCsException );
239
251
266 QgsRectangle transformBoundingBox( const QgsRectangle &rectangle, Qgis::TransformDirection direction = Qgis::TransformDirection::Forward, bool handle180Crossover = false ) const SIP_THROW( QgsCsException );
267
280 void transformInPlace( double &x, double &y, double &z, Qgis::TransformDirection direction = Qgis::TransformDirection::Forward ) const SIP_THROW( QgsCsException );
281
295 void transformInPlace( float &x, float &y, double &z, Qgis::TransformDirection direction = Qgis::TransformDirection::Forward ) const SIP_SKIP;
296
310 void transformInPlace( float &x, float &y, float &z, Qgis::TransformDirection direction = Qgis::TransformDirection::Forward ) const SIP_SKIP;
311
325 void transformInPlace( QVector<float> &x, QVector<float> &y, QVector<float> &z,
327
341 void transformInPlace( QVector<double> &x, QVector<double> &y, QVector<double> &z,
343
350 void transformPolygon( QPolygonF &polygon, Qgis::TransformDirection direction = Qgis::TransformDirection::Forward ) const SIP_THROW( QgsCsException );
351
362
374 void transformCoords( int numPoint, double *x, double *y, double *z, Qgis::TransformDirection direction = Qgis::TransformDirection::Forward ) const SIP_THROW( QgsCsException );
375
379 bool isShortCircuited() const;
380
397 QString coordinateOperation() const;
398
412 QgsDatumTransform::TransformDetails instantiatedCoordinateOperationDetails() const;
413
427 void setCoordinateOperation( const QString &operation ) const;
428
443 void setAllowFallbackTransforms( bool allowed );
444
456 bool allowFallbackTransforms() const;
457
489 void setBallparkTransformsAreAppropriate( bool appropriate );
490
505 void disableFallbackOperationHandler( bool disabled );
506
515 bool fallbackOperationOccurred() const;
516
530 Q_DECL_DEPRECATED int sourceDatumTransformId() const SIP_DEPRECATED;
531
545 Q_DECL_DEPRECATED void setSourceDatumTransformId( int datumId ) SIP_DEPRECATED;
546
560 Q_DECL_DEPRECATED int destinationDatumTransformId() const SIP_DEPRECATED;
561
575 Q_DECL_DEPRECATED void setDestinationDatumTransformId( int datumId ) SIP_DEPRECATED;
576
577#ifndef SIP_RUN
578
588 static void invalidateCache( bool disableCache = false );
589#else
590
597 static void invalidateCache( bool disableCache SIP_PYARGREMOVE = false );
598#endif
599
609 double scaleFactor( const QgsRectangle &referenceExtent ) const;
610
611#ifdef SIP_RUN
612 SIP_PYOBJECT __repr__();
613 % MethodCode
614 QString str = QStringLiteral( "<QgsCoordinateTransform: %1 to %2>" ).arg( sipCpp->sourceCrs().isValid() ? sipCpp->sourceCrs().authid() : QStringLiteral( "NULL" ),
615 sipCpp->destinationCrs().isValid() ? sipCpp->destinationCrs().authid() : QStringLiteral( "NULL" ) );
616 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
617 % End
618#endif
619
620#ifndef SIP_RUN
621
634 static void setCustomMissingRequiredGridHandler( const std::function< void( const QgsCoordinateReferenceSystem &sourceCrs,
635 const QgsCoordinateReferenceSystem &destinationCrs,
636 const QgsDatumTransform::GridDetails &grid )> &handler );
637
654 static void setCustomMissingPreferredGridHandler( const std::function< void( const QgsCoordinateReferenceSystem &sourceCrs,
655 const QgsCoordinateReferenceSystem &destinationCrs,
656 const QgsDatumTransform::TransformDetails &preferredOperation,
657 const QgsDatumTransform::TransformDetails &availableOperation )> &handler );
658
671 static void setCustomCoordinateOperationCreationErrorHandler( const std::function< void( const QgsCoordinateReferenceSystem &sourceCrs,
672 const QgsCoordinateReferenceSystem &destinationCrs,
673 const QString &error )> &handler );
674
687 static void setCustomMissingGridUsedByContextHandler( const std::function< void( const QgsCoordinateReferenceSystem &sourceCrs,
688 const QgsCoordinateReferenceSystem &destinationCrs,
689 const QgsDatumTransform::TransformDetails &desiredOperation )> &handler );
690
691
698 static void setFallbackOperationOccurredHandler( const std::function< void( const QgsCoordinateReferenceSystem &sourceCrs,
699 const QgsCoordinateReferenceSystem &destinationCrs,
700 const QString &desiredOperation )> &handler );
701
708 static void setDynamicCrsToDynamicCrsWarningHandler( const std::function< void( const QgsCoordinateReferenceSystem &sourceCrs,
709 const QgsCoordinateReferenceSystem &destinationCrs )> &handler );
710
711#endif
712
713 private:
714
715#ifndef SIP_RUN
716 friend class QgsProjContext;
717
718 // Only meant to be called by QgsProjContext::~QgsProjContext()
719 static void removeFromCacheObjectsBelongingToCurrentThread( void *pj_context );
720#endif
721
722 mutable QExplicitlySharedDataPointer<QgsCoordinateTransformPrivate> d;
723
726
727#ifdef QGISDEBUG
728 bool mHasContext = false;
729#endif
730
731 mutable QString mLastError;
732 bool mIgnoreImpossible = false;
733 bool mBallparkTransformsAreAppropriate = false;
734 bool mDisableFallbackHandler = false;
735 mutable bool mFallbackOperationOccurred = false;
736
737 bool setFromCache( const QgsCoordinateReferenceSystem &src,
739 const QString &coordinateOperationProj, bool allowFallback );
740
741 void addToCache();
742
743 // cache
744 static QReadWriteLock sCacheLock;
745
753 static QMultiHash< QPair< QString, QString >, QgsCoordinateTransform > sTransforms;
754 static bool sDisableCache;
755
756
757 static std::function< void( const QgsCoordinateReferenceSystem &sourceCrs,
758 const QgsCoordinateReferenceSystem &destinationCrs,
759 const QString &desiredOperation )> sFallbackOperationOccurredHandler;
760
761 friend class TestQgsCoordinateTransform;
762
763};
764
766#ifndef SIP_RUN
767inline std::ostream &operator << ( std::ostream &os, const QgsCoordinateTransform &r )
768{
769 QString mySummary( QStringLiteral( "\n%%%%%%%%%%%%%%%%%%%%%%%%\nCoordinate Transform def begins:" ) );
770 mySummary += QLatin1String( "\n\tInitialized? : " );
771 //prevent warnings
772 if ( r.isValid() )
773 {
774 //do nothing this is a dummy
775 }
776
777#if 0
778 if ( r.isValid() )
779 {
780 mySummary += "Yes";
781 }
782 else
783 {
784 mySummary += "No";
785 }
786 mySummary += "\n\tShort Circuit? : ";
787 if ( r.isShortCircuited() )
788 {
789 mySummary += "Yes";
790 }
791 else
792 {
793 mySummary += "No";
794 }
795
796 mySummary += "\n\tSource Spatial Ref Sys : ";
797 if ( r.sourceCrs() )
798 {
799 mySummary << r.sourceCrs();
800 }
801 else
802 {
803 mySummary += "Undefined";
804 }
805
806 mySummary += "\n\tDest Spatial Ref Sys : ";
807 if ( r.destCRS() )
808 {
809 mySummary << r.destCRS();
810 }
811 else
812 {
813 mySummary += "Undefined";
814 }
815#endif
816
817 mySummary += QLatin1String( "\nCoordinate Transform def ends \n%%%%%%%%%%%%%%%%%%%%%%%%\n" );
818 return os << mySummary.toLocal8Bit().data() << std::endl;
819}
820#endif
821
822
823#endif // QGSCOORDINATETRANSFORM_H
QFlags< CoordinateTransformationFlag > CoordinateTransformationFlags
Coordinate transformation flags.
Definition qgis.h:2312
TransformDirection
Indicates the direction (forward or inverse) of a transform.
Definition qgis.h:2289
@ Forward
Forward transform (from source to destination)
This class represents a coordinate reference system (CRS).
Contains information about the context in which a coordinate transform is executed.
Class for doing transforms between two map coordinate systems.
QgsCoordinateReferenceSystem sourceCrs() const
Returns the source coordinate reference system, which the transform will transform coordinates from.
bool isShortCircuited() const
Returns true if the transform short circuits because the source and destination are equivalent.
bool isValid() const
Returns true if the coordinate transform is valid, ie both the source and destination CRS have been s...
Custom exception class for Coordinate Reference System related exceptions.
A class to represent a 2D point.
Definition qgspointxy.h:60
Used to create and store a proj context object, correctly freeing the context upon destruction.
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
Definition qgsproject.h:107
A rectangle specified with double values.
Class for storage of 3D vectors similar to QVector3D, with the difference that it uses double precisi...
Definition qgsvector3d.h:31
#define str(x)
Definition qgis.cpp:38
#define SIP_DEPRECATED
Definition qgis_sip.h:106
#define SIP_SKIP
Definition qgis_sip.h:126
#define SIP_PYARGREMOVE
Definition qgis_sip.h:151
#define SIP_THROW(name,...)
Definition qgis_sip.h:203
std::ostream & operator<<(std::ostream &os, const QgsCoordinateTransform &r)
Output stream operator.
const QgsCoordinateReferenceSystem & crs
Contains information about a projection transformation grid file.
Contains information about a coordinate transformation operation.