QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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 
28 class QgsCoordinateTransformPrivate;
29 class QgsPointXY;
30 class QgsRectangle;
31 class QPolygonF;
32 class QgsProject;
33 class QgsVector3D;
34 
57 class CORE_EXPORT QgsCoordinateTransform
58 {
59 
60  public:
61 
64 
93  const QgsCoordinateReferenceSystem &destination,
94  const QgsCoordinateTransformContext &context,
95  Qgis::CoordinateTransformationFlags flags = Qgis::CoordinateTransformationFlags() );
96 
124  const QgsCoordinateReferenceSystem &destination,
125  const QgsProject *project,
126  Qgis::CoordinateTransformationFlags flags = Qgis::CoordinateTransformationFlags() );
127 
136  Q_DECL_DEPRECATED explicit QgsCoordinateTransform( const QgsCoordinateReferenceSystem &source,
137  const QgsCoordinateReferenceSystem &destination,
138  int sourceDatumTransformId,
139  int destinationDatumTransformId ) SIP_DEPRECATED;
140 
145 
149  QgsCoordinateTransform &operator=( const QgsCoordinateTransform &o );
150 
152 
165  static bool isTransformationPossible( const QgsCoordinateReferenceSystem &source, const QgsCoordinateReferenceSystem &destination );
166 
172  bool isValid() const;
173 
180  void setSourceCrs( const QgsCoordinateReferenceSystem &crs );
181 
188  void setDestinationCrs( const QgsCoordinateReferenceSystem &crs );
189 
196  void setContext( const QgsCoordinateTransformContext &context );
197 
204  QgsCoordinateTransformContext context() const;
205 
212  QgsCoordinateReferenceSystem sourceCrs() const;
213 
220  QgsCoordinateReferenceSystem destinationCrs() const;
221 
230  QgsPointXY transform( const QgsPointXY &point, Qgis::TransformDirection direction = Qgis::TransformDirection::Forward ) const SIP_THROW( QgsCsException );
231 
241  QgsPointXY transform( double x, double y, Qgis::TransformDirection direction = Qgis::TransformDirection::Forward ) const;
242 
252  QgsVector3D transform( const QgsVector3D &point, Qgis::TransformDirection direction = Qgis::TransformDirection::Forward ) const;
253 
267  QgsRectangle transformBoundingBox( const QgsRectangle &rectangle, Qgis::TransformDirection direction = Qgis::TransformDirection::Forward, bool handle180Crossover = false ) const SIP_THROW( QgsCsException );
268 
280  void transformInPlace( double &x, double &y, double &z, Qgis::TransformDirection direction = Qgis::TransformDirection::Forward ) const SIP_THROW( QgsCsException );
281 
294  void transformInPlace( float &x, float &y, double &z, Qgis::TransformDirection direction = Qgis::TransformDirection::Forward ) const SIP_SKIP;
295 
308  void transformInPlace( float &x, float &y, float &z, Qgis::TransformDirection direction = Qgis::TransformDirection::Forward ) const SIP_SKIP;
309 
322  void transformInPlace( QVector<float> &x, QVector<float> &y, QVector<float> &z,
323  Qgis::TransformDirection direction = Qgis::TransformDirection::Forward ) const SIP_SKIP;
324 
337  void transformInPlace( QVector<double> &x, QVector<double> &y, QVector<double> &z,
338  Qgis::TransformDirection direction = Qgis::TransformDirection::Forward ) const SIP_SKIP;
339 
345  void transformPolygon( QPolygonF &polygon, Qgis::TransformDirection direction = Qgis::TransformDirection::Forward ) const SIP_THROW( QgsCsException );
346 
355  QgsRectangle transform( const QgsRectangle &rectangle, Qgis::TransformDirection direction = Qgis::TransformDirection::Forward ) const SIP_THROW( QgsCsException );
356 
367  void transformCoords( int numPoint, double *x, double *y, double *z, Qgis::TransformDirection direction = Qgis::TransformDirection::Forward ) const SIP_THROW( QgsCsException );
368 
372  bool isShortCircuited() const;
373 
390  QString coordinateOperation() const;
391 
405  QgsDatumTransform::TransformDetails instantiatedCoordinateOperationDetails() const;
406 
420  void setCoordinateOperation( const QString &operation ) const;
421 
436  void setAllowFallbackTransforms( bool allowed );
437 
449  bool allowFallbackTransforms() const;
450 
482  void setBallparkTransformsAreAppropriate( bool appropriate );
483 
498  void disableFallbackOperationHandler( bool disabled );
499 
508  bool fallbackOperationOccurred() const;
509 
523  Q_DECL_DEPRECATED int sourceDatumTransformId() const SIP_DEPRECATED;
524 
538  Q_DECL_DEPRECATED void setSourceDatumTransformId( int datumId ) SIP_DEPRECATED;
539 
553  Q_DECL_DEPRECATED int destinationDatumTransformId() const SIP_DEPRECATED;
554 
568  Q_DECL_DEPRECATED void setDestinationDatumTransformId( int datumId ) SIP_DEPRECATED;
569 
570 #ifndef SIP_RUN
571 
582  static void invalidateCache( bool disableCache = false );
583 #else
584 
592  static void invalidateCache( bool disableCache SIP_PYARGREMOVE = false );
593 #endif
594 
604  double scaleFactor( const QgsRectangle &referenceExtent ) const;
605 
606 #ifdef SIP_RUN
607  SIP_PYOBJECT __repr__();
608  % MethodCode
609  QString str = QStringLiteral( "<QgsCoordinateTransform: %1 to %2>" ).arg( sipCpp->sourceCrs().isValid() ? sipCpp->sourceCrs().authid() : QStringLiteral( "NULL" ),
610  sipCpp->destinationCrs().isValid() ? sipCpp->destinationCrs().authid() : QStringLiteral( "NULL" ) );
611  sipRes = PyUnicode_FromString( str.toUtf8().constData() );
612  % End
613 #endif
614 
615 #ifndef SIP_RUN
616 
629  static void setCustomMissingRequiredGridHandler( const std::function< void( const QgsCoordinateReferenceSystem &sourceCrs,
630  const QgsCoordinateReferenceSystem &destinationCrs,
631  const QgsDatumTransform::GridDetails &grid )> &handler );
632 
649  static void setCustomMissingPreferredGridHandler( const std::function< void( const QgsCoordinateReferenceSystem &sourceCrs,
650  const QgsCoordinateReferenceSystem &destinationCrs,
651  const QgsDatumTransform::TransformDetails &preferredOperation,
652  const QgsDatumTransform::TransformDetails &availableOperation )> &handler );
653 
666  static void setCustomCoordinateOperationCreationErrorHandler( const std::function< void( const QgsCoordinateReferenceSystem &sourceCrs,
667  const QgsCoordinateReferenceSystem &destinationCrs,
668  const QString &error )> &handler );
669 
682  static void setCustomMissingGridUsedByContextHandler( const std::function< void( const QgsCoordinateReferenceSystem &sourceCrs,
683  const QgsCoordinateReferenceSystem &destinationCrs,
684  const QgsDatumTransform::TransformDetails &desiredOperation )> &handler );
685 
686 
693  static void setFallbackOperationOccurredHandler( const std::function< void( const QgsCoordinateReferenceSystem &sourceCrs,
694  const QgsCoordinateReferenceSystem &destinationCrs,
695  const QString &desiredOperation )> &handler );
696 
703  static void setDynamicCrsToDynamicCrsWarningHandler( const std::function< void( const QgsCoordinateReferenceSystem &sourceCrs,
704  const QgsCoordinateReferenceSystem &destinationCrs )> &handler );
705 
706 #endif
707 
708  private:
709 
710 #ifndef SIP_RUN
711  friend class QgsProjContext;
712 
713  // Only meant to be called by QgsProjContext::~QgsProjContext()
714  static void removeFromCacheObjectsBelongingToCurrentThread( void *pj_context );
715 #endif
716 
717  mutable QExplicitlySharedDataPointer<QgsCoordinateTransformPrivate> d;
718 
721 
722 #ifdef QGISDEBUG
723  bool mHasContext = false;
724 #endif
725 
726  mutable QString mLastError;
727  bool mIgnoreImpossible = false;
728  bool mBallparkTransformsAreAppropriate = false;
729  bool mDisableFallbackHandler = false;
730  mutable bool mFallbackOperationOccurred = false;
731 
732  bool setFromCache( const QgsCoordinateReferenceSystem &src,
733  const QgsCoordinateReferenceSystem &dest,
734  const QString &coordinateOperationProj, bool allowFallback );
735 
736  void addToCache();
737 
738  // cache
739  static QReadWriteLock sCacheLock;
740 
748  static QMultiHash< QPair< QString, QString >, QgsCoordinateTransform > sTransforms;
749  static bool sDisableCache;
750 
751 
752  static std::function< void( const QgsCoordinateReferenceSystem &sourceCrs,
753  const QgsCoordinateReferenceSystem &destinationCrs,
754  const QString &desiredOperation )> sFallbackOperationOccurredHandler;
755 
756  friend class TestQgsCoordinateTransform;
757 
758 };
759 
761 #ifndef SIP_RUN
762 inline std::ostream &operator << ( std::ostream &os, const QgsCoordinateTransform &r )
763 {
764  QString mySummary( QStringLiteral( "\n%%%%%%%%%%%%%%%%%%%%%%%%\nCoordinate Transform def begins:" ) );
765  mySummary += QLatin1String( "\n\tInitialized? : " );
766  //prevent warnings
767  if ( r.isValid() )
768  {
769  //do nothing this is a dummy
770  }
771 
772 #if 0
773  if ( r.isValid() )
774  {
775  mySummary += "Yes";
776  }
777  else
778  {
779  mySummary += "No";
780  }
781  mySummary += "\n\tShort Circuit? : ";
782  if ( r.isShortCircuited() )
783  {
784  mySummary += "Yes";
785  }
786  else
787  {
788  mySummary += "No";
789  }
790 
791  mySummary += "\n\tSource Spatial Ref Sys : ";
792  if ( r.sourceCrs() )
793  {
794  mySummary << r.sourceCrs();
795  }
796  else
797  {
798  mySummary += "Undefined";
799  }
800 
801  mySummary += "\n\tDest Spatial Ref Sys : ";
802  if ( r.destCRS() )
803  {
804  mySummary << r.destCRS();
805  }
806  else
807  {
808  mySummary += "Undefined";
809  }
810 #endif
811 
812  mySummary += QLatin1String( "\nCoordinate Transform def ends \n%%%%%%%%%%%%%%%%%%%%%%%%\n" );
813  return os << mySummary.toLocal8Bit().data() << std::endl;
814 }
815 #endif
816 
817 
818 #endif // QGSCOORDINATETRANSFORM_H
QgsCoordinateTransformContext
Contains information about the context in which a coordinate transform is executed.
Definition: qgscoordinatetransformcontext.h:57
QgsVector3D
Class for storage of 3D vectors similar to QVector3D, with the difference that it uses double precisi...
Definition: qgsvector3d.h:31
qgscoordinatetransformcontext.h
crs
const QgsCoordinateReferenceSystem & crs
Definition: qgswfsgetfeature.cpp:105
SIP_PYARGREMOVE
#define SIP_PYARGREMOVE
Definition: qgis_sip.h:151
QgsCoordinateTransform::isValid
bool isValid() const
Returns true if the coordinate transform is valid, ie both the source and destination CRS have been s...
Definition: qgscoordinatetransform.cpp:900
QgsRectangle
A rectangle specified with double values.
Definition: qgsrectangle.h:41
QgsDatumTransform::GridDetails
Contains information about a projection transformation grid file.
Definition: qgsdatumtransform.h:132
QgsProject
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
Definition: qgsproject.h:103
operator<<
std::ostream & operator<<(std::ostream &os, const QgsCoordinateTransform &r)
Output stream operator.
Definition: qgscoordinatetransform.h:762
SIP_DEPRECATED
#define SIP_DEPRECATED
Definition: qgis_sip.h:106
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
SIP_SKIP
#define SIP_SKIP
Definition: qgis_sip.h:126
SIP_THROW
#define SIP_THROW(name)
Definition: qgis_sip.h:198
QgsCoordinateTransform::isShortCircuited
bool isShortCircuited() const
Returns true if the transform short circuits because the source and destination are equivalent.
Definition: qgscoordinatetransform.cpp:905
qgis_sip.h
QgsDatumTransform::TransformDetails
Contains information about a coordinate transformation operation.
Definition: qgsdatumtransform.h:180
QgsCoordinateReferenceSystem
This class represents a coordinate reference system (CRS).
Definition: qgscoordinatereferencesystem.h:211
QgsPointXY
A class to represent a 2D point.
Definition: qgspointxy.h:58
str
#define str(x)
Definition: qgis.cpp:37
Qgis::TransformDirection
TransformDirection
Indicates the direction (forward or inverse) of a transform.
Definition: qgis.h:1235
QgsCoordinateTransform
Class for doing transforms between two map coordinate systems.
Definition: qgscoordinatetransform.h:57
qgscoordinatereferencesystem.h
QgsProjContext
Used to create and store a proj context object, correctly freeing the context upon destruction.
Definition: qgsprojutils.h:230