QGIS API Documentation  3.17.0-Master (a035f434f4)
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 
52 class CORE_EXPORT QgsCoordinateTransform
53 {
54 
55  public:
56 
59  {
61  ReverseTransform
62  };
63 
66 
88  const QgsCoordinateReferenceSystem &destination,
89  const QgsCoordinateTransformContext &context );
90 
112  const QgsCoordinateReferenceSystem &destination,
113  const QgsProject *project );
114 
123  const QgsCoordinateReferenceSystem &destination,
124  int sourceDatumTransformId,
125  int destinationDatumTransformId );
126 
131 
135  QgsCoordinateTransform &operator=( const QgsCoordinateTransform &o );
136 
138 
144  bool isValid() const;
145 
152  void setSourceCrs( const QgsCoordinateReferenceSystem &crs );
153 
160  void setDestinationCrs( const QgsCoordinateReferenceSystem &crs );
161 
168  void setContext( const QgsCoordinateTransformContext &context );
169 
176  QgsCoordinateTransformContext context() const;
177 
184  QgsCoordinateReferenceSystem sourceCrs() const;
185 
192  QgsCoordinateReferenceSystem destinationCrs() const;
193 
202  QgsPointXY transform( const QgsPointXY &point, TransformDirection direction = ForwardTransform ) const SIP_THROW( QgsCsException );
203 
213  QgsPointXY transform( double x, double y, TransformDirection direction = ForwardTransform ) const;
214 
228  QgsRectangle transformBoundingBox( const QgsRectangle &rectangle, TransformDirection direction = ForwardTransform, bool handle180Crossover = false ) const SIP_THROW( QgsCsException );
229 
241  void transformInPlace( double &x, double &y, double &z, TransformDirection direction = ForwardTransform ) const SIP_THROW( QgsCsException );
242 
255  void transformInPlace( float &x, float &y, double &z, TransformDirection direction = ForwardTransform ) const SIP_SKIP;
256 
269  void transformInPlace( float &x, float &y, float &z, TransformDirection direction = ForwardTransform ) const SIP_SKIP;
270 
283  void transformInPlace( QVector<float> &x, QVector<float> &y, QVector<float> &z,
284  TransformDirection direction = ForwardTransform ) const SIP_SKIP;
285 
298  void transformInPlace( QVector<double> &x, QVector<double> &y, QVector<double> &z,
299  TransformDirection direction = ForwardTransform ) const SIP_SKIP;
300 
306  void transformPolygon( QPolygonF &polygon, TransformDirection direction = ForwardTransform ) const SIP_THROW( QgsCsException );
307 
316  QgsRectangle transform( const QgsRectangle &rectangle, TransformDirection direction = ForwardTransform ) const SIP_THROW( QgsCsException );
317 
328  void transformCoords( int numPoint, double *x, double *y, double *z, TransformDirection direction = ForwardTransform ) const SIP_THROW( QgsCsException );
329 
333  bool isShortCircuited() const;
334 
351  QString coordinateOperation() const;
352 
366  QgsDatumTransform::TransformDetails instantiatedCoordinateOperationDetails() const;
367 
381  void setCoordinateOperation( const QString &operation ) const;
382 
397  void setAllowFallbackTransforms( bool allowed );
398 
410  bool allowFallbackTransforms() const;
411 
443  void setBallparkTransformsAreAppropriate( bool appropriate );
444 
459  void disableFallbackOperationHandler( bool disabled );
460 
469  bool fallbackOperationOccurred() const;
470 
484  Q_DECL_DEPRECATED int sourceDatumTransformId() const SIP_DEPRECATED;
485 
499  Q_DECL_DEPRECATED void setSourceDatumTransformId( int datumId ) SIP_DEPRECATED;
500 
514  Q_DECL_DEPRECATED int destinationDatumTransformId() const SIP_DEPRECATED;
515 
529  Q_DECL_DEPRECATED void setDestinationDatumTransformId( int datumId ) SIP_DEPRECATED;
530 
531 #ifndef SIP_RUN
532 
543  static void invalidateCache( bool disableCache = false );
544 #else
545 
553  static void invalidateCache( bool disableCache SIP_PYARGREMOVE = false );
554 #endif
555 
565  double scaleFactor( const QgsRectangle &referenceExtent ) const;
566 
567 #ifdef SIP_RUN
568  SIP_PYOBJECT __repr__();
569  % MethodCode
570  QString str = QStringLiteral( "<QgsCoordinateTransform: %1 to %2>" ).arg( sipCpp->sourceCrs().isValid() ? sipCpp->sourceCrs().authid() : QStringLiteral( "NULL" ),
571  sipCpp->destinationCrs().isValid() ? sipCpp->destinationCrs().authid() : QStringLiteral( "NULL" ) );
572  sipRes = PyUnicode_FromString( str.toUtf8().constData() );
573  % End
574 #endif
575 
576 #ifndef SIP_RUN
577 
590  static void setCustomMissingRequiredGridHandler( const std::function< void( const QgsCoordinateReferenceSystem &sourceCrs,
591  const QgsCoordinateReferenceSystem &destinationCrs,
592  const QgsDatumTransform::GridDetails &grid )> &handler );
593 
610  static void setCustomMissingPreferredGridHandler( const std::function< void( const QgsCoordinateReferenceSystem &sourceCrs,
611  const QgsCoordinateReferenceSystem &destinationCrs,
612  const QgsDatumTransform::TransformDetails &preferredOperation,
613  const QgsDatumTransform::TransformDetails &availableOperation )> &handler );
614 
627  static void setCustomCoordinateOperationCreationErrorHandler( const std::function< void( const QgsCoordinateReferenceSystem &sourceCrs,
628  const QgsCoordinateReferenceSystem &destinationCrs,
629  const QString &error )> &handler );
630 
643  static void setCustomMissingGridUsedByContextHandler( const std::function< void( const QgsCoordinateReferenceSystem &sourceCrs,
644  const QgsCoordinateReferenceSystem &destinationCrs,
645  const QgsDatumTransform::TransformDetails &desiredOperation )> &handler );
646 
647 
654  static void setFallbackOperationOccurredHandler( const std::function< void( const QgsCoordinateReferenceSystem &sourceCrs,
655  const QgsCoordinateReferenceSystem &destinationCrs,
656  const QString &desiredOperation )> &handler );
657 
658 #endif
659 
660 #ifndef SIP_RUN
661 #if PROJ_VERSION_MAJOR>=6
662  protected:
663  friend class QgsProjContext;
664 
665  // Only meant to be called by QgsProjContext::~QgsProjContext()
666  static void removeFromCacheObjectsBelongingToCurrentThread( void *pj_context );
667 #endif
668 #endif
669  private:
670 
671  mutable QExplicitlySharedDataPointer<QgsCoordinateTransformPrivate> d;
672 
675 
676 #ifdef QGISDEBUG
677  bool mHasContext = false;
678 #endif
679 
680  mutable QString mLastError;
681  bool mBallparkTransformsAreAppropriate = false;
682  bool mDisableFallbackHandler = false;
683  mutable bool mFallbackOperationOccurred = false;
684 
685 #if PROJ_VERSION_MAJOR>=6
686  bool setFromCache( const QgsCoordinateReferenceSystem &src,
687  const QgsCoordinateReferenceSystem &dest,
688  const QString &coordinateOperationProj, bool allowFallback );
689 #else
690  bool setFromCache( const QgsCoordinateReferenceSystem &src,
691  const QgsCoordinateReferenceSystem &dest,
692  int srcDatumTransform,
693  int destDatumTransform );
694 #endif
695  void addToCache();
696 
697  // cache
698  static QReadWriteLock sCacheLock;
699  static QMultiHash< QPair< QString, QString >, QgsCoordinateTransform > sTransforms; //same auth_id pairs might have different datum transformations
700  static bool sDisableCache;
701 
702 
703  static std::function< void( const QgsCoordinateReferenceSystem &sourceCrs,
704  const QgsCoordinateReferenceSystem &destinationCrs,
705  const QString &desiredOperation )> sFallbackOperationOccurredHandler;
706 
707 };
708 
710 #ifndef SIP_RUN
711 inline std::ostream &operator << ( std::ostream &os, const QgsCoordinateTransform &r )
712 {
713  QString mySummary( QStringLiteral( "\n%%%%%%%%%%%%%%%%%%%%%%%%\nCoordinate Transform def begins:" ) );
714  mySummary += QLatin1String( "\n\tInitialized? : " );
715  //prevent warnings
716  if ( r.isValid() )
717  {
718  //do nothing this is a dummy
719  }
720 
721 #if 0
722  if ( r.isValid() )
723  {
724  mySummary += "Yes";
725  }
726  else
727  {
728  mySummary += "No";
729  }
730  mySummary += "\n\tShort Circuit? : ";
731  if ( r.isShortCircuited() )
732  {
733  mySummary += "Yes";
734  }
735  else
736  {
737  mySummary += "No";
738  }
739 
740  mySummary += "\n\tSource Spatial Ref Sys : ";
741  if ( r.sourceCrs() )
742  {
743  mySummary << r.sourceCrs();
744  }
745  else
746  {
747  mySummary += "Undefined";
748  }
749 
750  mySummary += "\n\tDest Spatial Ref Sys : ";
751  if ( r.destCRS() )
752  {
753  mySummary << r.destCRS();
754  }
755  else
756  {
757  mySummary += "Undefined";
758  }
759 #endif
760 
761  mySummary += QLatin1String( "\nCoordinate Transform def ends \n%%%%%%%%%%%%%%%%%%%%%%%%\n" );
762  return os << mySummary.toLocal8Bit().data() << std::endl;
763 }
764 #endif
765 
766 
767 #endif // QGSCOORDINATETRANSFORM_H
A rectangle specified with double values.
Definition: qgsrectangle.h:41
A class to represent a 2D point.
Definition: qgspointxy.h:43
TransformDirection
Enum used to indicate the direction (forward or inverse) of the transform.
Contains information about a projection transformation grid file.
const QgsCoordinateReferenceSystem & crs
bool isValid() const
Returns true if the coordinate transform is valid, ie both the source and destination CRS have been s...
Used to create and store a proj context object, correctly freeing the context upon destruction...
Definition: qgsprojutils.h:161
QgsCoordinateReferenceSystem sourceCrs() const
Returns the source coordinate reference system, which the transform will transform coordinates from...
#define SIP_SKIP
Definition: qgis_sip.h:126
bool isShortCircuited() const
Returns true if the transform short circuits because the source and destination are equivalent...
Encapsulates a QGIS project, including sets of map layers and their styles, layouts, annotations, canvases, etc.
Definition: qgsproject.h:94
Contains information about the context in which a coordinate transform is executed.
#define SIP_DEPRECATED
Definition: qgis_sip.h:106
Contains methods and classes relating the datum transformations.
#define SIP_PYARGREMOVE
Definition: qgis_sip.h:146
This class represents a coordinate reference system (CRS).
Class for doing transforms between two map coordinate systems.
#define SIP_THROW(name)
Definition: qgis_sip.h:189
Transform from source to destination CRS.
Custom exception class for Coordinate Reference System related exceptions.
Definition: qgsexception.h:65
Contains information about a coordinate transformation operation.
std::ostream & operator<<(std::ostream &os, const QgsCoordinateTransform &r)
Output stream operator.