QGIS API Documentation  3.8.0-Zanzibar (11aff65)
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 
428  static void invalidateCache();
429 
439  double scaleFactor( const QgsRectangle &referenceExtent ) const;
440 
441 #ifndef SIP_RUN
442 
455  static void setCustomMissingRequiredGridHandler( const std::function< void( const QgsCoordinateReferenceSystem &sourceCrs,
456  const QgsCoordinateReferenceSystem &destinationCrs,
457  const QgsDatumTransform::GridDetails &grid )> &handler );
458 
475  static void setCustomMissingPreferredGridHandler( const std::function< void( const QgsCoordinateReferenceSystem &sourceCrs,
476  const QgsCoordinateReferenceSystem &destinationCrs,
477  const QgsDatumTransform::TransformDetails &preferredOperation,
478  const QgsDatumTransform::TransformDetails &availableOperation )> &handler );
479 
492  static void setCustomCoordinateOperationCreationErrorHandler( const std::function< void( const QgsCoordinateReferenceSystem &sourceCrs,
493  const QgsCoordinateReferenceSystem &destinationCrs,
494  const QString &error )> &handler );
495 
508  static void setCustomMissingGridUsedByContextHandler( const std::function< void( const QgsCoordinateReferenceSystem &sourceCrs,
509  const QgsCoordinateReferenceSystem &destinationCrs,
510  const QgsDatumTransform::TransformDetails &desiredOperation )> &handler );
511 #endif
512 
513  private:
514 
515  mutable QExplicitlySharedDataPointer<QgsCoordinateTransformPrivate> d;
516 
519 
520 #ifdef QGISDEBUG
521  bool mHasContext = false;
522 #endif
523 
524 #if PROJ_VERSION_MAJOR>=6
525  bool setFromCache( const QgsCoordinateReferenceSystem &src,
526  const QgsCoordinateReferenceSystem &dest,
527  const QString &coordinateOperationProj );
528 #else
529  bool setFromCache( const QgsCoordinateReferenceSystem &src,
530  const QgsCoordinateReferenceSystem &dest,
531  int srcDatumTransform,
532  int destDatumTransform );
533 #endif
534  void addToCache();
535 
536  // cache
537  static QReadWriteLock sCacheLock;
538  static QMultiHash< QPair< QString, QString >, QgsCoordinateTransform > sTransforms; //same auth_id pairs might have different datum transformations
539 
540 };
541 
543 #ifndef SIP_RUN
544 inline std::ostream &operator << ( std::ostream &os, const QgsCoordinateTransform &r )
545 {
546  QString mySummary( QStringLiteral( "\n%%%%%%%%%%%%%%%%%%%%%%%%\nCoordinate Transform def begins:" ) );
547  mySummary += QLatin1String( "\n\tInitialized? : " );
548  //prevent warnings
549  if ( r.isValid() )
550  {
551  //do nothing this is a dummy
552  }
553 
554 #if 0
555  if ( r.isValid() )
556  {
557  mySummary += "Yes";
558  }
559  else
560  {
561  mySummary += "No";
562  }
563  mySummary += "\n\tShort Circuit? : ";
564  if ( r.isShortCircuited() )
565  {
566  mySummary += "Yes";
567  }
568  else
569  {
570  mySummary += "No";
571  }
572 
573  mySummary += "\n\tSource Spatial Ref Sys : ";
574  if ( r.sourceCrs() )
575  {
576  mySummary << r.sourceCrs();
577  }
578  else
579  {
580  mySummary += "Undefined";
581  }
582 
583  mySummary += "\n\tDest Spatial Ref Sys : ";
584  if ( r.destCRS() )
585  {
586  mySummary << r.destCRS();
587  }
588  else
589  {
590  mySummary += "Undefined";
591  }
592 #endif
593 
594  mySummary += QLatin1String( "\nCoordinate Transform def ends \n%%%%%%%%%%%%%%%%%%%%%%%%\n" );
595  return os << mySummary.toLocal8Bit().data() << std::endl;
596 }
597 #endif
598 
599 
600 #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:119
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:89
Contains information about the context in which a coordinate transform is executed.
#define SIP_DEPRECATED
Definition: qgis_sip.h:99
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:177
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.