QGIS API Documentation  3.11.0-Master (358e466517)
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 
345  QString coordinateOperation() const;
346 
360  void setCoordinateOperation( const QString &operation ) const;
361 
375  Q_DECL_DEPRECATED int sourceDatumTransformId() const SIP_DEPRECATED;
376 
390  Q_DECL_DEPRECATED void setSourceDatumTransformId( int datumId ) SIP_DEPRECATED;
391 
405  Q_DECL_DEPRECATED int destinationDatumTransformId() const SIP_DEPRECATED;
406 
420  Q_DECL_DEPRECATED void setDestinationDatumTransformId( int datumId ) SIP_DEPRECATED;
421 
422 #ifndef SIP_RUN
423 
434  static void invalidateCache( bool disableCache = false );
435 #else
436 
444  static void invalidateCache( bool disableCache SIP_PYARGREMOVE = false );
445 #endif
446 
456  double scaleFactor( const QgsRectangle &referenceExtent ) const;
457 
458 #ifdef SIP_RUN
459  SIP_PYOBJECT __repr__();
460  % MethodCode
461  QString str = QStringLiteral( "<QgsCoordinateTransform: %1 to %2>" ).arg( sipCpp->sourceCrs().isValid() ? sipCpp->sourceCrs().authid() : QStringLiteral( "NULL" ),
462  sipCpp->destinationCrs().isValid() ? sipCpp->destinationCrs().authid() : QStringLiteral( "NULL" ) );
463  sipRes = PyUnicode_FromString( str.toUtf8().constData() );
464  % End
465 #endif
466 
467 #ifndef SIP_RUN
468 
481  static void setCustomMissingRequiredGridHandler( const std::function< void( const QgsCoordinateReferenceSystem &sourceCrs,
482  const QgsCoordinateReferenceSystem &destinationCrs,
483  const QgsDatumTransform::GridDetails &grid )> &handler );
484 
501  static void setCustomMissingPreferredGridHandler( const std::function< void( const QgsCoordinateReferenceSystem &sourceCrs,
502  const QgsCoordinateReferenceSystem &destinationCrs,
503  const QgsDatumTransform::TransformDetails &preferredOperation,
504  const QgsDatumTransform::TransformDetails &availableOperation )> &handler );
505 
518  static void setCustomCoordinateOperationCreationErrorHandler( const std::function< void( const QgsCoordinateReferenceSystem &sourceCrs,
519  const QgsCoordinateReferenceSystem &destinationCrs,
520  const QString &error )> &handler );
521 
534  static void setCustomMissingGridUsedByContextHandler( const std::function< void( const QgsCoordinateReferenceSystem &sourceCrs,
535  const QgsCoordinateReferenceSystem &destinationCrs,
536  const QgsDatumTransform::TransformDetails &desiredOperation )> &handler );
537 #endif
538 
539 #ifndef SIP_RUN
540 #if PROJ_VERSION_MAJOR>=6
541  protected:
542  friend class QgsProjContext;
543 
544  // Only meant to be called by QgsProjContext::~QgsProjContext()
545  static void removeFromCacheObjectsBelongingToCurrentThread( void *pj_context );
546 #endif
547 #endif
548  private:
549 
550  mutable QExplicitlySharedDataPointer<QgsCoordinateTransformPrivate> d;
551 
554 
555 #ifdef QGISDEBUG
556  bool mHasContext = false;
557 #endif
558 
559 #if PROJ_VERSION_MAJOR>=6
560  bool setFromCache( const QgsCoordinateReferenceSystem &src,
561  const QgsCoordinateReferenceSystem &dest,
562  const QString &coordinateOperationProj );
563 #else
564  bool setFromCache( const QgsCoordinateReferenceSystem &src,
565  const QgsCoordinateReferenceSystem &dest,
566  int srcDatumTransform,
567  int destDatumTransform );
568 #endif
569  void addToCache();
570 
571  // cache
572  static QReadWriteLock sCacheLock;
573  static QMultiHash< QPair< QString, QString >, QgsCoordinateTransform > sTransforms; //same auth_id pairs might have different datum transformations
574  static bool sDisableCache;
575 
576 };
577 
579 #ifndef SIP_RUN
580 inline std::ostream &operator << ( std::ostream &os, const QgsCoordinateTransform &r )
581 {
582  QString mySummary( QStringLiteral( "\n%%%%%%%%%%%%%%%%%%%%%%%%\nCoordinate Transform def begins:" ) );
583  mySummary += QLatin1String( "\n\tInitialized? : " );
584  //prevent warnings
585  if ( r.isValid() )
586  {
587  //do nothing this is a dummy
588  }
589 
590 #if 0
591  if ( r.isValid() )
592  {
593  mySummary += "Yes";
594  }
595  else
596  {
597  mySummary += "No";
598  }
599  mySummary += "\n\tShort Circuit? : ";
600  if ( r.isShortCircuited() )
601  {
602  mySummary += "Yes";
603  }
604  else
605  {
606  mySummary += "No";
607  }
608 
609  mySummary += "\n\tSource Spatial Ref Sys : ";
610  if ( r.sourceCrs() )
611  {
612  mySummary << r.sourceCrs();
613  }
614  else
615  {
616  mySummary += "Undefined";
617  }
618 
619  mySummary += "\n\tDest Spatial Ref Sys : ";
620  if ( r.destCRS() )
621  {
622  mySummary << r.destCRS();
623  }
624  else
625  {
626  mySummary += "Undefined";
627  }
628 #endif
629 
630  mySummary += QLatin1String( "\nCoordinate Transform def ends \n%%%%%%%%%%%%%%%%%%%%%%%%\n" );
631  return os << mySummary.toLocal8Bit().data() << std::endl;
632 }
633 #endif
634 
635 
636 #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:153
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:90
Contains information about the context in which a coordinate transform is executed.
#define SIP_DEPRECATED
Definition: qgis_sip.h:106
#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:184
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.