QGIS API Documentation  3.21.0-Master (564c892cfd)
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  {
66  ReverseTransform
67  };
68 
71 
97  const QgsCoordinateReferenceSystem &destination,
98  const QgsCoordinateTransformContext &context );
99 
124  const QgsCoordinateReferenceSystem &destination,
125  const QgsProject *project );
126 
135  Q_DECL_DEPRECATED explicit QgsCoordinateTransform( const QgsCoordinateReferenceSystem &source,
136  const QgsCoordinateReferenceSystem &destination,
137  int sourceDatumTransformId,
138  int destinationDatumTransformId ) SIP_DEPRECATED;
139 
144 
148  QgsCoordinateTransform &operator=( const QgsCoordinateTransform &o );
149 
151 
157  bool isValid() const;
158 
165  void setSourceCrs( const QgsCoordinateReferenceSystem &crs );
166 
173  void setDestinationCrs( const QgsCoordinateReferenceSystem &crs );
174 
181  void setContext( const QgsCoordinateTransformContext &context );
182 
189  QgsCoordinateTransformContext context() const;
190 
197  QgsCoordinateReferenceSystem sourceCrs() const;
198 
205  QgsCoordinateReferenceSystem destinationCrs() const;
206 
215  QgsPointXY transform( const QgsPointXY &point, TransformDirection direction = ForwardTransform ) const SIP_THROW( QgsCsException );
216 
226  QgsPointXY transform( double x, double y, TransformDirection direction = ForwardTransform ) const;
227 
237  QgsVector3D transform( const QgsVector3D &point, TransformDirection direction = ForwardTransform ) const;
238 
252  QgsRectangle transformBoundingBox( const QgsRectangle &rectangle, TransformDirection direction = ForwardTransform, bool handle180Crossover = false ) const SIP_THROW( QgsCsException );
253 
265  void transformInPlace( double &x, double &y, double &z, TransformDirection direction = ForwardTransform ) const SIP_THROW( QgsCsException );
266 
279  void transformInPlace( float &x, float &y, double &z, TransformDirection direction = ForwardTransform ) const SIP_SKIP;
280 
293  void transformInPlace( float &x, float &y, float &z, TransformDirection direction = ForwardTransform ) const SIP_SKIP;
294 
307  void transformInPlace( QVector<float> &x, QVector<float> &y, QVector<float> &z,
308  TransformDirection direction = ForwardTransform ) const SIP_SKIP;
309 
322  void transformInPlace( QVector<double> &x, QVector<double> &y, QVector<double> &z,
323  TransformDirection direction = ForwardTransform ) const SIP_SKIP;
324 
330  void transformPolygon( QPolygonF &polygon, TransformDirection direction = ForwardTransform ) const SIP_THROW( QgsCsException );
331 
340  QgsRectangle transform( const QgsRectangle &rectangle, TransformDirection direction = ForwardTransform ) const SIP_THROW( QgsCsException );
341 
352  void transformCoords( int numPoint, double *x, double *y, double *z, TransformDirection direction = ForwardTransform ) const SIP_THROW( QgsCsException );
353 
357  bool isShortCircuited() const;
358 
375  QString coordinateOperation() const;
376 
390  QgsDatumTransform::TransformDetails instantiatedCoordinateOperationDetails() const;
391 
405  void setCoordinateOperation( const QString &operation ) const;
406 
421  void setAllowFallbackTransforms( bool allowed );
422 
434  bool allowFallbackTransforms() const;
435 
467  void setBallparkTransformsAreAppropriate( bool appropriate );
468 
483  void disableFallbackOperationHandler( bool disabled );
484 
493  bool fallbackOperationOccurred() const;
494 
508  Q_DECL_DEPRECATED int sourceDatumTransformId() const SIP_DEPRECATED;
509 
523  Q_DECL_DEPRECATED void setSourceDatumTransformId( int datumId ) SIP_DEPRECATED;
524 
538  Q_DECL_DEPRECATED int destinationDatumTransformId() const SIP_DEPRECATED;
539 
553  Q_DECL_DEPRECATED void setDestinationDatumTransformId( int datumId ) SIP_DEPRECATED;
554 
555 #ifndef SIP_RUN
556 
567  static void invalidateCache( bool disableCache = false );
568 #else
569 
577  static void invalidateCache( bool disableCache SIP_PYARGREMOVE = false );
578 #endif
579 
589  double scaleFactor( const QgsRectangle &referenceExtent ) const;
590 
591 #ifdef SIP_RUN
592  SIP_PYOBJECT __repr__();
593  % MethodCode
594  QString str = QStringLiteral( "<QgsCoordinateTransform: %1 to %2>" ).arg( sipCpp->sourceCrs().isValid() ? sipCpp->sourceCrs().authid() : QStringLiteral( "NULL" ),
595  sipCpp->destinationCrs().isValid() ? sipCpp->destinationCrs().authid() : QStringLiteral( "NULL" ) );
596  sipRes = PyUnicode_FromString( str.toUtf8().constData() );
597  % End
598 #endif
599 
600 #ifndef SIP_RUN
601 
614  static void setCustomMissingRequiredGridHandler( const std::function< void( const QgsCoordinateReferenceSystem &sourceCrs,
615  const QgsCoordinateReferenceSystem &destinationCrs,
616  const QgsDatumTransform::GridDetails &grid )> &handler );
617 
634  static void setCustomMissingPreferredGridHandler( const std::function< void( const QgsCoordinateReferenceSystem &sourceCrs,
635  const QgsCoordinateReferenceSystem &destinationCrs,
636  const QgsDatumTransform::TransformDetails &preferredOperation,
637  const QgsDatumTransform::TransformDetails &availableOperation )> &handler );
638 
651  static void setCustomCoordinateOperationCreationErrorHandler( const std::function< void( const QgsCoordinateReferenceSystem &sourceCrs,
652  const QgsCoordinateReferenceSystem &destinationCrs,
653  const QString &error )> &handler );
654 
667  static void setCustomMissingGridUsedByContextHandler( const std::function< void( const QgsCoordinateReferenceSystem &sourceCrs,
668  const QgsCoordinateReferenceSystem &destinationCrs,
669  const QgsDatumTransform::TransformDetails &desiredOperation )> &handler );
670 
671 
678  static void setFallbackOperationOccurredHandler( const std::function< void( const QgsCoordinateReferenceSystem &sourceCrs,
679  const QgsCoordinateReferenceSystem &destinationCrs,
680  const QString &desiredOperation )> &handler );
681 
688  static void setDynamicCrsToDynamicCrsWarningHandler( const std::function< void( const QgsCoordinateReferenceSystem &sourceCrs,
689  const QgsCoordinateReferenceSystem &destinationCrs )> &handler );
690 
691 #endif
692 
693  private:
694 
695 #ifndef SIP_RUN
696  friend class QgsProjContext;
697 
698  // Only meant to be called by QgsProjContext::~QgsProjContext()
699  static void removeFromCacheObjectsBelongingToCurrentThread( void *pj_context );
700 #endif
701 
702  mutable QExplicitlySharedDataPointer<QgsCoordinateTransformPrivate> d;
703 
706 
707 #ifdef QGISDEBUG
708  bool mHasContext = false;
709 #endif
710 
711  mutable QString mLastError;
712  bool mBallparkTransformsAreAppropriate = false;
713  bool mDisableFallbackHandler = false;
714  mutable bool mFallbackOperationOccurred = false;
715 
716  bool setFromCache( const QgsCoordinateReferenceSystem &src,
717  const QgsCoordinateReferenceSystem &dest,
718  const QString &coordinateOperationProj, bool allowFallback );
719 
720  void addToCache();
721 
722  // cache
723  static QReadWriteLock sCacheLock;
724 
732  static QMultiHash< QPair< QString, QString >, QgsCoordinateTransform > sTransforms;
733  static bool sDisableCache;
734 
735 
736  static std::function< void( const QgsCoordinateReferenceSystem &sourceCrs,
737  const QgsCoordinateReferenceSystem &destinationCrs,
738  const QString &desiredOperation )> sFallbackOperationOccurredHandler;
739 
740 };
741 
743 #ifndef SIP_RUN
744 inline std::ostream &operator << ( std::ostream &os, const QgsCoordinateTransform &r )
745 {
746  QString mySummary( QStringLiteral( "\n%%%%%%%%%%%%%%%%%%%%%%%%\nCoordinate Transform def begins:" ) );
747  mySummary += QLatin1String( "\n\tInitialized? : " );
748  //prevent warnings
749  if ( r.isValid() )
750  {
751  //do nothing this is a dummy
752  }
753 
754 #if 0
755  if ( r.isValid() )
756  {
757  mySummary += "Yes";
758  }
759  else
760  {
761  mySummary += "No";
762  }
763  mySummary += "\n\tShort Circuit? : ";
764  if ( r.isShortCircuited() )
765  {
766  mySummary += "Yes";
767  }
768  else
769  {
770  mySummary += "No";
771  }
772 
773  mySummary += "\n\tSource Spatial Ref Sys : ";
774  if ( r.sourceCrs() )
775  {
776  mySummary << r.sourceCrs();
777  }
778  else
779  {
780  mySummary += "Undefined";
781  }
782 
783  mySummary += "\n\tDest Spatial Ref Sys : ";
784  if ( r.destCRS() )
785  {
786  mySummary << r.destCRS();
787  }
788  else
789  {
790  mySummary += "Undefined";
791  }
792 #endif
793 
794  mySummary += QLatin1String( "\nCoordinate Transform def ends \n%%%%%%%%%%%%%%%%%%%%%%%%\n" );
795  return os << mySummary.toLocal8Bit().data() << std::endl;
796 }
797 #endif
798 
799 
800 #endif // QGSCOORDINATETRANSFORM_H
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.
TransformDirection
Enum used to indicate the direction (forward or inverse) of the transform.
@ ForwardTransform
Transform from source to destination CRS.
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
Contains methods and classes relating the datum transformations.
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:99
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.