QGIS API Documentation  3.21.0-Master (56b4176581)
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 
90  const QgsCoordinateReferenceSystem &destination,
91  const QgsCoordinateTransformContext &context );
92 
117  const QgsCoordinateReferenceSystem &destination,
118  const QgsProject *project );
119 
128  Q_DECL_DEPRECATED explicit QgsCoordinateTransform( const QgsCoordinateReferenceSystem &source,
129  const QgsCoordinateReferenceSystem &destination,
130  int sourceDatumTransformId,
131  int destinationDatumTransformId ) SIP_DEPRECATED;
132 
137 
141  QgsCoordinateTransform &operator=( const QgsCoordinateTransform &o );
142 
144 
150  bool isValid() const;
151 
158  void setSourceCrs( const QgsCoordinateReferenceSystem &crs );
159 
166  void setDestinationCrs( const QgsCoordinateReferenceSystem &crs );
167 
174  void setContext( const QgsCoordinateTransformContext &context );
175 
182  QgsCoordinateTransformContext context() const;
183 
190  QgsCoordinateReferenceSystem sourceCrs() const;
191 
198  QgsCoordinateReferenceSystem destinationCrs() const;
199 
208  QgsPointXY transform( const QgsPointXY &point, Qgis::TransformDirection direction = Qgis::TransformDirection::Forward ) const SIP_THROW( QgsCsException );
209 
219  QgsPointXY transform( double x, double y, Qgis::TransformDirection direction = Qgis::TransformDirection::Forward ) const;
220 
230  QgsVector3D transform( const QgsVector3D &point, Qgis::TransformDirection direction = Qgis::TransformDirection::Forward ) const;
231 
245  QgsRectangle transformBoundingBox( const QgsRectangle &rectangle, Qgis::TransformDirection direction = Qgis::TransformDirection::Forward, bool handle180Crossover = false ) const SIP_THROW( QgsCsException );
246 
258  void transformInPlace( double &x, double &y, double &z, Qgis::TransformDirection direction = Qgis::TransformDirection::Forward ) const SIP_THROW( QgsCsException );
259 
272  void transformInPlace( float &x, float &y, double &z, Qgis::TransformDirection direction = Qgis::TransformDirection::Forward ) const SIP_SKIP;
273 
286  void transformInPlace( float &x, float &y, float &z, Qgis::TransformDirection direction = Qgis::TransformDirection::Forward ) const SIP_SKIP;
287 
300  void transformInPlace( QVector<float> &x, QVector<float> &y, QVector<float> &z,
301  Qgis::TransformDirection direction = Qgis::TransformDirection::Forward ) const SIP_SKIP;
302 
315  void transformInPlace( QVector<double> &x, QVector<double> &y, QVector<double> &z,
316  Qgis::TransformDirection direction = Qgis::TransformDirection::Forward ) const SIP_SKIP;
317 
323  void transformPolygon( QPolygonF &polygon, Qgis::TransformDirection direction = Qgis::TransformDirection::Forward ) const SIP_THROW( QgsCsException );
324 
333  QgsRectangle transform( const QgsRectangle &rectangle, Qgis::TransformDirection direction = Qgis::TransformDirection::Forward ) const SIP_THROW( QgsCsException );
334 
345  void transformCoords( int numPoint, double *x, double *y, double *z, Qgis::TransformDirection direction = Qgis::TransformDirection::Forward ) const SIP_THROW( QgsCsException );
346 
350  bool isShortCircuited() const;
351 
368  QString coordinateOperation() const;
369 
383  QgsDatumTransform::TransformDetails instantiatedCoordinateOperationDetails() const;
384 
398  void setCoordinateOperation( const QString &operation ) const;
399 
414  void setAllowFallbackTransforms( bool allowed );
415 
427  bool allowFallbackTransforms() const;
428 
460  void setBallparkTransformsAreAppropriate( bool appropriate );
461 
476  void disableFallbackOperationHandler( bool disabled );
477 
486  bool fallbackOperationOccurred() const;
487 
501  Q_DECL_DEPRECATED int sourceDatumTransformId() const SIP_DEPRECATED;
502 
516  Q_DECL_DEPRECATED void setSourceDatumTransformId( int datumId ) SIP_DEPRECATED;
517 
531  Q_DECL_DEPRECATED int destinationDatumTransformId() const SIP_DEPRECATED;
532 
546  Q_DECL_DEPRECATED void setDestinationDatumTransformId( int datumId ) SIP_DEPRECATED;
547 
548 #ifndef SIP_RUN
549 
560  static void invalidateCache( bool disableCache = false );
561 #else
562 
570  static void invalidateCache( bool disableCache SIP_PYARGREMOVE = false );
571 #endif
572 
582  double scaleFactor( const QgsRectangle &referenceExtent ) const;
583 
584 #ifdef SIP_RUN
585  SIP_PYOBJECT __repr__();
586  % MethodCode
587  QString str = QStringLiteral( "<QgsCoordinateTransform: %1 to %2>" ).arg( sipCpp->sourceCrs().isValid() ? sipCpp->sourceCrs().authid() : QStringLiteral( "NULL" ),
588  sipCpp->destinationCrs().isValid() ? sipCpp->destinationCrs().authid() : QStringLiteral( "NULL" ) );
589  sipRes = PyUnicode_FromString( str.toUtf8().constData() );
590  % End
591 #endif
592 
593 #ifndef SIP_RUN
594 
607  static void setCustomMissingRequiredGridHandler( const std::function< void( const QgsCoordinateReferenceSystem &sourceCrs,
608  const QgsCoordinateReferenceSystem &destinationCrs,
609  const QgsDatumTransform::GridDetails &grid )> &handler );
610 
627  static void setCustomMissingPreferredGridHandler( const std::function< void( const QgsCoordinateReferenceSystem &sourceCrs,
628  const QgsCoordinateReferenceSystem &destinationCrs,
629  const QgsDatumTransform::TransformDetails &preferredOperation,
630  const QgsDatumTransform::TransformDetails &availableOperation )> &handler );
631 
644  static void setCustomCoordinateOperationCreationErrorHandler( const std::function< void( const QgsCoordinateReferenceSystem &sourceCrs,
645  const QgsCoordinateReferenceSystem &destinationCrs,
646  const QString &error )> &handler );
647 
660  static void setCustomMissingGridUsedByContextHandler( const std::function< void( const QgsCoordinateReferenceSystem &sourceCrs,
661  const QgsCoordinateReferenceSystem &destinationCrs,
662  const QgsDatumTransform::TransformDetails &desiredOperation )> &handler );
663 
664 
671  static void setFallbackOperationOccurredHandler( const std::function< void( const QgsCoordinateReferenceSystem &sourceCrs,
672  const QgsCoordinateReferenceSystem &destinationCrs,
673  const QString &desiredOperation )> &handler );
674 
681  static void setDynamicCrsToDynamicCrsWarningHandler( const std::function< void( const QgsCoordinateReferenceSystem &sourceCrs,
682  const QgsCoordinateReferenceSystem &destinationCrs )> &handler );
683 
684 #endif
685 
686  private:
687 
688 #ifndef SIP_RUN
689  friend class QgsProjContext;
690 
691  // Only meant to be called by QgsProjContext::~QgsProjContext()
692  static void removeFromCacheObjectsBelongingToCurrentThread( void *pj_context );
693 #endif
694 
695  mutable QExplicitlySharedDataPointer<QgsCoordinateTransformPrivate> d;
696 
699 
700 #ifdef QGISDEBUG
701  bool mHasContext = false;
702 #endif
703 
704  mutable QString mLastError;
705  bool mBallparkTransformsAreAppropriate = false;
706  bool mDisableFallbackHandler = false;
707  mutable bool mFallbackOperationOccurred = false;
708 
709  bool setFromCache( const QgsCoordinateReferenceSystem &src,
710  const QgsCoordinateReferenceSystem &dest,
711  const QString &coordinateOperationProj, bool allowFallback );
712 
713  void addToCache();
714 
715  // cache
716  static QReadWriteLock sCacheLock;
717 
725  static QMultiHash< QPair< QString, QString >, QgsCoordinateTransform > sTransforms;
726  static bool sDisableCache;
727 
728 
729  static std::function< void( const QgsCoordinateReferenceSystem &sourceCrs,
730  const QgsCoordinateReferenceSystem &destinationCrs,
731  const QString &desiredOperation )> sFallbackOperationOccurredHandler;
732 
733 };
734 
736 #ifndef SIP_RUN
737 inline std::ostream &operator << ( std::ostream &os, const QgsCoordinateTransform &r )
738 {
739  QString mySummary( QStringLiteral( "\n%%%%%%%%%%%%%%%%%%%%%%%%\nCoordinate Transform def begins:" ) );
740  mySummary += QLatin1String( "\n\tInitialized? : " );
741  //prevent warnings
742  if ( r.isValid() )
743  {
744  //do nothing this is a dummy
745  }
746 
747 #if 0
748  if ( r.isValid() )
749  {
750  mySummary += "Yes";
751  }
752  else
753  {
754  mySummary += "No";
755  }
756  mySummary += "\n\tShort Circuit? : ";
757  if ( r.isShortCircuited() )
758  {
759  mySummary += "Yes";
760  }
761  else
762  {
763  mySummary += "No";
764  }
765 
766  mySummary += "\n\tSource Spatial Ref Sys : ";
767  if ( r.sourceCrs() )
768  {
769  mySummary << r.sourceCrs();
770  }
771  else
772  {
773  mySummary += "Undefined";
774  }
775 
776  mySummary += "\n\tDest Spatial Ref Sys : ";
777  if ( r.destCRS() )
778  {
779  mySummary << r.destCRS();
780  }
781  else
782  {
783  mySummary += "Undefined";
784  }
785 #endif
786 
787  mySummary += QLatin1String( "\nCoordinate Transform def ends \n%%%%%%%%%%%%%%%%%%%%%%%%\n" );
788  return os << mySummary.toLocal8Bit().data() << std::endl;
789 }
790 #endif
791 
792 
793 #endif // QGSCOORDINATETRANSFORM_H
TransformDirection
Indicates the direction (forward or inverse) of a transform.
Definition: qgis.h:880
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:101
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:189
#define SIP_DEPRECATED
Definition: qgis_sip.h:106
#define SIP_SKIP
Definition: qgis_sip.h:126
#define SIP_PYARGREMOVE
Definition: qgis_sip.h:146
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.