QGIS API Documentation  3.9.0-Master (224899f119)
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  private:
540 
541  mutable QExplicitlySharedDataPointer<QgsCoordinateTransformPrivate> d;
542 
545 
546 #ifdef QGISDEBUG
547  bool mHasContext = false;
548 #endif
549 
550 #if PROJ_VERSION_MAJOR>=6
551  bool setFromCache( const QgsCoordinateReferenceSystem &src,
552  const QgsCoordinateReferenceSystem &dest,
553  const QString &coordinateOperationProj );
554 #else
555  bool setFromCache( const QgsCoordinateReferenceSystem &src,
556  const QgsCoordinateReferenceSystem &dest,
557  int srcDatumTransform,
558  int destDatumTransform );
559 #endif
560  void addToCache();
561 
562  // cache
563  static QReadWriteLock sCacheLock;
564  static QMultiHash< QPair< QString, QString >, QgsCoordinateTransform > sTransforms; //same auth_id pairs might have different datum transformations
565  static bool sDisableCache;
566 
567 };
568 
570 #ifndef SIP_RUN
571 inline std::ostream &operator << ( std::ostream &os, const QgsCoordinateTransform &r )
572 {
573  QString mySummary( QStringLiteral( "\n%%%%%%%%%%%%%%%%%%%%%%%%\nCoordinate Transform def begins:" ) );
574  mySummary += QLatin1String( "\n\tInitialized? : " );
575  //prevent warnings
576  if ( r.isValid() )
577  {
578  //do nothing this is a dummy
579  }
580 
581 #if 0
582  if ( r.isValid() )
583  {
584  mySummary += "Yes";
585  }
586  else
587  {
588  mySummary += "No";
589  }
590  mySummary += "\n\tShort Circuit? : ";
591  if ( r.isShortCircuited() )
592  {
593  mySummary += "Yes";
594  }
595  else
596  {
597  mySummary += "No";
598  }
599 
600  mySummary += "\n\tSource Spatial Ref Sys : ";
601  if ( r.sourceCrs() )
602  {
603  mySummary << r.sourceCrs();
604  }
605  else
606  {
607  mySummary += "Undefined";
608  }
609 
610  mySummary += "\n\tDest Spatial Ref Sys : ";
611  if ( r.destCRS() )
612  {
613  mySummary << r.destCRS();
614  }
615  else
616  {
617  mySummary += "Undefined";
618  }
619 #endif
620 
621  mySummary += QLatin1String( "\nCoordinate Transform def ends \n%%%%%%%%%%%%%%%%%%%%%%%%\n" );
622  return os << mySummary.toLocal8Bit().data() << std::endl;
623 }
624 #endif
625 
626 
627 #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...
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...
Reads and writes project states.
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.