QGIS API Documentation  3.14.0-Pi (9f7028fd23)
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
QgsCoordinateTransformContext
Definition: qgscoordinatetransformcontext.h:57
qgscoordinatetransformcontext.h
crs
const QgsCoordinateReferenceSystem & crs
Definition: qgswfsgetfeature.cpp:105
QgsCoordinateTransform::TransformDirection
TransformDirection
Enum used to indicate the direction (forward or inverse) of the transform.
Definition: qgscoordinatetransform.h:58
SIP_PYARGREMOVE
#define SIP_PYARGREMOVE
Definition: qgis_sip.h:146
QgsCoordinateTransform::isValid
bool isValid() const
Returns true if the coordinate transform is valid, ie both the source and destination CRS have been s...
Definition: qgscoordinatetransform.cpp:876
QgsRectangle
Definition: qgsrectangle.h:41
QgsDatumTransform::GridDetails
Contains information about a projection transformation grid file.
Definition: qgsdatumtransform.h:133
QgsProject
Definition: qgsproject.h:92
operator<<
std::ostream & operator<<(std::ostream &os, const QgsCoordinateTransform &r)
Output stream operator.
Definition: qgscoordinatetransform.h:711
SIP_DEPRECATED
#define SIP_DEPRECATED
Definition: qgis_sip.h:106
QgsCoordinateTransform::sourceCrs
QgsCoordinateReferenceSystem sourceCrs() const
Returns the source coordinate reference system, which the transform will transform coordinates from.
Definition: qgscoordinatetransform.cpp:229
QgsCsException
Definition: qgsexception.h:65
SIP_SKIP
#define SIP_SKIP
Definition: qgis_sip.h:126
SIP_THROW
#define SIP_THROW(name)
Definition: qgis_sip.h:184
QgsCoordinateTransform::isShortCircuited
bool isShortCircuited() const
Returns true if the transform short circuits because the source and destination are equivalent.
Definition: qgscoordinatetransform.cpp:881
QgsDatumTransform
Contains methods and classes relating the datum transformations.
Definition: qgsdatumtransform.h:44
qgis_sip.h
QgsDatumTransform::TransformDetails
Contains information about a coordinate transformation operation.
Definition: qgsdatumtransform.h:181
QgsCoordinateReferenceSystem
Definition: qgscoordinatereferencesystem.h:206
QgsCoordinateTransform::ForwardTransform
@ ForwardTransform
Transform from source to destination CRS.
Definition: qgscoordinatetransform.h:60
QgsPointXY
Definition: qgspointxy.h:43
QgsCoordinateTransform
Definition: qgscoordinatetransform.h:52
qgscoordinatereferencesystem.h
QgsProjContext
Definition: qgsprojutils.h:161