QGIS API Documentation  3.18.1-Zürich (202f1bf7e5)
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 
53 class CORE_EXPORT QgsCoordinateTransform
54 {
55 
56  public:
57 
60  {
62  ReverseTransform
63  };
64 
67 
89  const QgsCoordinateReferenceSystem &destination,
90  const QgsCoordinateTransformContext &context );
91 
113  const QgsCoordinateReferenceSystem &destination,
114  const QgsProject *project );
115 
124  const QgsCoordinateReferenceSystem &destination,
125  int sourceDatumTransformId,
126  int destinationDatumTransformId );
127 
132 
136  QgsCoordinateTransform &operator=( const QgsCoordinateTransform &o );
137 
139 
145  bool isValid() const;
146 
153  void setSourceCrs( const QgsCoordinateReferenceSystem &crs );
154 
161  void setDestinationCrs( const QgsCoordinateReferenceSystem &crs );
162 
169  void setContext( const QgsCoordinateTransformContext &context );
170 
177  QgsCoordinateTransformContext context() const;
178 
185  QgsCoordinateReferenceSystem sourceCrs() const;
186 
193  QgsCoordinateReferenceSystem destinationCrs() const;
194 
203  QgsPointXY transform( const QgsPointXY &point, TransformDirection direction = ForwardTransform ) const SIP_THROW( QgsCsException );
204 
214  QgsPointXY transform( double x, double y, TransformDirection direction = ForwardTransform ) const;
215 
225  QgsVector3D transform( const QgsVector3D &point, TransformDirection direction = ForwardTransform ) const;
226 
240  QgsRectangle transformBoundingBox( const QgsRectangle &rectangle, TransformDirection direction = ForwardTransform, bool handle180Crossover = false ) const SIP_THROW( QgsCsException );
241 
253  void transformInPlace( double &x, double &y, double &z, TransformDirection direction = ForwardTransform ) const SIP_THROW( QgsCsException );
254 
267  void transformInPlace( float &x, float &y, double &z, TransformDirection direction = ForwardTransform ) const SIP_SKIP;
268 
281  void transformInPlace( float &x, float &y, float &z, TransformDirection direction = ForwardTransform ) const SIP_SKIP;
282 
295  void transformInPlace( QVector<float> &x, QVector<float> &y, QVector<float> &z,
296  TransformDirection direction = ForwardTransform ) const SIP_SKIP;
297 
310  void transformInPlace( QVector<double> &x, QVector<double> &y, QVector<double> &z,
311  TransformDirection direction = ForwardTransform ) const SIP_SKIP;
312 
318  void transformPolygon( QPolygonF &polygon, TransformDirection direction = ForwardTransform ) const SIP_THROW( QgsCsException );
319 
328  QgsRectangle transform( const QgsRectangle &rectangle, TransformDirection direction = ForwardTransform ) const SIP_THROW( QgsCsException );
329 
340  void transformCoords( int numPoint, double *x, double *y, double *z, TransformDirection direction = ForwardTransform ) const SIP_THROW( QgsCsException );
341 
345  bool isShortCircuited() const;
346 
363  QString coordinateOperation() const;
364 
378  QgsDatumTransform::TransformDetails instantiatedCoordinateOperationDetails() const;
379 
393  void setCoordinateOperation( const QString &operation ) const;
394 
409  void setAllowFallbackTransforms( bool allowed );
410 
422  bool allowFallbackTransforms() const;
423 
455  void setBallparkTransformsAreAppropriate( bool appropriate );
456 
471  void disableFallbackOperationHandler( bool disabled );
472 
481  bool fallbackOperationOccurred() const;
482 
496  Q_DECL_DEPRECATED int sourceDatumTransformId() const SIP_DEPRECATED;
497 
511  Q_DECL_DEPRECATED void setSourceDatumTransformId( int datumId ) SIP_DEPRECATED;
512 
526  Q_DECL_DEPRECATED int destinationDatumTransformId() const SIP_DEPRECATED;
527 
541  Q_DECL_DEPRECATED void setDestinationDatumTransformId( int datumId ) SIP_DEPRECATED;
542 
543 #ifndef SIP_RUN
544 
555  static void invalidateCache( bool disableCache = false );
556 #else
557 
565  static void invalidateCache( bool disableCache SIP_PYARGREMOVE = false );
566 #endif
567 
577  double scaleFactor( const QgsRectangle &referenceExtent ) const;
578 
579 #ifdef SIP_RUN
580  SIP_PYOBJECT __repr__();
581  % MethodCode
582  QString str = QStringLiteral( "<QgsCoordinateTransform: %1 to %2>" ).arg( sipCpp->sourceCrs().isValid() ? sipCpp->sourceCrs().authid() : QStringLiteral( "NULL" ),
583  sipCpp->destinationCrs().isValid() ? sipCpp->destinationCrs().authid() : QStringLiteral( "NULL" ) );
584  sipRes = PyUnicode_FromString( str.toUtf8().constData() );
585  % End
586 #endif
587 
588 #ifndef SIP_RUN
589 
602  static void setCustomMissingRequiredGridHandler( const std::function< void( const QgsCoordinateReferenceSystem &sourceCrs,
603  const QgsCoordinateReferenceSystem &destinationCrs,
604  const QgsDatumTransform::GridDetails &grid )> &handler );
605 
622  static void setCustomMissingPreferredGridHandler( const std::function< void( const QgsCoordinateReferenceSystem &sourceCrs,
623  const QgsCoordinateReferenceSystem &destinationCrs,
624  const QgsDatumTransform::TransformDetails &preferredOperation,
625  const QgsDatumTransform::TransformDetails &availableOperation )> &handler );
626 
639  static void setCustomCoordinateOperationCreationErrorHandler( const std::function< void( const QgsCoordinateReferenceSystem &sourceCrs,
640  const QgsCoordinateReferenceSystem &destinationCrs,
641  const QString &error )> &handler );
642 
655  static void setCustomMissingGridUsedByContextHandler( const std::function< void( const QgsCoordinateReferenceSystem &sourceCrs,
656  const QgsCoordinateReferenceSystem &destinationCrs,
657  const QgsDatumTransform::TransformDetails &desiredOperation )> &handler );
658 
659 
666  static void setFallbackOperationOccurredHandler( const std::function< void( const QgsCoordinateReferenceSystem &sourceCrs,
667  const QgsCoordinateReferenceSystem &destinationCrs,
668  const QString &desiredOperation )> &handler );
669 
670 #endif
671 
672 #ifndef SIP_RUN
673 #if PROJ_VERSION_MAJOR>=6
674  protected:
675  friend class QgsProjContext;
676 
677  // Only meant to be called by QgsProjContext::~QgsProjContext()
678  static void removeFromCacheObjectsBelongingToCurrentThread( void *pj_context );
679 #endif
680 #endif
681  private:
682 
683  mutable QExplicitlySharedDataPointer<QgsCoordinateTransformPrivate> d;
684 
687 
688 #ifdef QGISDEBUG
689  bool mHasContext = false;
690 #endif
691 
692  mutable QString mLastError;
693  bool mBallparkTransformsAreAppropriate = false;
694  bool mDisableFallbackHandler = false;
695  mutable bool mFallbackOperationOccurred = false;
696 
697 #if PROJ_VERSION_MAJOR>=6
698  bool setFromCache( const QgsCoordinateReferenceSystem &src,
699  const QgsCoordinateReferenceSystem &dest,
700  const QString &coordinateOperationProj, bool allowFallback );
701 #else
702  bool setFromCache( const QgsCoordinateReferenceSystem &src,
703  const QgsCoordinateReferenceSystem &dest,
704  int srcDatumTransform,
705  int destDatumTransform );
706 #endif
707  void addToCache();
708 
709  // cache
710  static QReadWriteLock sCacheLock;
711  static QMultiHash< QPair< QString, QString >, QgsCoordinateTransform > sTransforms; //same auth_id pairs might have different datum transformations
712  static bool sDisableCache;
713 
714 
715  static std::function< void( const QgsCoordinateReferenceSystem &sourceCrs,
716  const QgsCoordinateReferenceSystem &destinationCrs,
717  const QString &desiredOperation )> sFallbackOperationOccurredHandler;
718 
719 };
720 
722 #ifndef SIP_RUN
723 inline std::ostream &operator << ( std::ostream &os, const QgsCoordinateTransform &r )
724 {
725  QString mySummary( QStringLiteral( "\n%%%%%%%%%%%%%%%%%%%%%%%%\nCoordinate Transform def begins:" ) );
726  mySummary += QLatin1String( "\n\tInitialized? : " );
727  //prevent warnings
728  if ( r.isValid() )
729  {
730  //do nothing this is a dummy
731  }
732 
733 #if 0
734  if ( r.isValid() )
735  {
736  mySummary += "Yes";
737  }
738  else
739  {
740  mySummary += "No";
741  }
742  mySummary += "\n\tShort Circuit? : ";
743  if ( r.isShortCircuited() )
744  {
745  mySummary += "Yes";
746  }
747  else
748  {
749  mySummary += "No";
750  }
751 
752  mySummary += "\n\tSource Spatial Ref Sys : ";
753  if ( r.sourceCrs() )
754  {
755  mySummary << r.sourceCrs();
756  }
757  else
758  {
759  mySummary += "Undefined";
760  }
761 
762  mySummary += "\n\tDest Spatial Ref Sys : ";
763  if ( r.destCRS() )
764  {
765  mySummary << r.destCRS();
766  }
767  else
768  {
769  mySummary += "Undefined";
770  }
771 #endif
772 
773  mySummary += QLatin1String( "\nCoordinate Transform def ends \n%%%%%%%%%%%%%%%%%%%%%%%%\n" );
774  return os << mySummary.toLocal8Bit().data() << std::endl;
775 }
776 #endif
777 
778 
779 #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:44
Used to create and store a proj context object, correctly freeing the context upon destruction.
Definition: qgsprojutils.h:162
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 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.