QGIS API Documentation  3.27.0-Master (e113457133)
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
TransformDirection
Flags for raster layer temporal capabilities.
Definition: qgis.h:1255
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.
Definition: qgsexception.h:66
A class to represent a 2D point.
Definition: qgspointxy.h:59
Used to create and store a proj context object, correctly freeing the context upon destruction.
Definition: qgsprojutils.h:231
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
Definition: qgsproject.h:104
A rectangle specified with double values.
Definition: qgsrectangle.h:42
#define str(x)
Definition: qgis.cpp:37
#define SIP_THROW(name)
Definition: qgis_sip.h:198
#define SIP_DEPRECATED
Definition: qgis_sip.h:106
#define SIP_SKIP
Definition: qgis_sip.h:126
#define SIP_PYARGREMOVE
Definition: qgis_sip.h:151
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.