QGIS API Documentation  3.4.15-Madeira (e83d02e274)
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 
78  Q_DECL_DEPRECATED explicit QgsCoordinateTransform( const QgsCoordinateReferenceSystem &source, const QgsCoordinateReferenceSystem &destination ) SIP_SKIP;
79 
101  const QgsCoordinateReferenceSystem &destination,
102  const QgsCoordinateTransformContext &context );
103 
125  const QgsCoordinateReferenceSystem &destination,
126  const QgsProject *project );
127 
136  const QgsCoordinateReferenceSystem &destination,
137  int sourceDatumTransformId,
138  int destinationDatumTransformId );
139 
144 
148  QgsCoordinateTransform &operator=( const QgsCoordinateTransform &o );
149 
151 
157  bool isValid() const;
158 
165  void setSourceCrs( const QgsCoordinateReferenceSystem &crs );
166 
173  void setDestinationCrs( const QgsCoordinateReferenceSystem &crs );
174 
181  void setContext( const QgsCoordinateTransformContext &context );
182 
189  QgsCoordinateTransformContext context() const;
190 
197  QgsCoordinateReferenceSystem sourceCrs() const;
198 
205  QgsCoordinateReferenceSystem destinationCrs() const;
206 
215  QgsPointXY transform( const QgsPointXY &point, TransformDirection direction = ForwardTransform ) const SIP_THROW( QgsCsException );
216 
226  QgsPointXY transform( double x, double y, TransformDirection direction = ForwardTransform ) const;
227 
241  QgsRectangle transformBoundingBox( const QgsRectangle &rectangle, TransformDirection direction = ForwardTransform, bool handle180Crossover = false ) const SIP_THROW( QgsCsException );
242 
254  void transformInPlace( double &x, double &y, double &z, TransformDirection direction = ForwardTransform ) const SIP_THROW( QgsCsException );
255 
268  void transformInPlace( float &x, float &y, double &z, TransformDirection direction = ForwardTransform ) const SIP_SKIP;
269 
282  void transformInPlace( float &x, float &y, float &z, TransformDirection direction = ForwardTransform ) const SIP_SKIP;
283 
296  void transformInPlace( QVector<float> &x, QVector<float> &y, QVector<float> &z,
297  TransformDirection direction = ForwardTransform ) const SIP_SKIP;
298 
311  void transformInPlace( QVector<double> &x, QVector<double> &y, QVector<double> &z,
312  TransformDirection direction = ForwardTransform ) const SIP_SKIP;
313 
319  void transformPolygon( QPolygonF &polygon, TransformDirection direction = ForwardTransform ) const SIP_THROW( QgsCsException );
320 
329  QgsRectangle transform( const QgsRectangle &rectangle, TransformDirection direction = ForwardTransform ) const SIP_THROW( QgsCsException );
330 
341  void transformCoords( int numPoint, double *x, double *y, double *z, TransformDirection direction = ForwardTransform ) const SIP_THROW( QgsCsException );
342 
346  bool isShortCircuited() const;
347 
359  int sourceDatumTransformId() const;
360 
372  void setSourceDatumTransformId( int datumId );
373 
385  int destinationDatumTransformId() const;
386 
398  void setDestinationDatumTransformId( int datumId );
399 
400 #ifndef SIP_RUN
401 
412  static void invalidateCache( bool disableCache = false );
413 #else
414 
422  static void invalidateCache( bool disableCache SIP_PYARGREMOVE = false );
423 #endif
424 
434  double scaleFactor( const QgsRectangle &referenceExtent ) const;
435 
436  private:
437 
438  mutable QExplicitlySharedDataPointer<QgsCoordinateTransformPrivate> d;
439 
442 
443 #ifdef QGISDEBUG
444  bool mHasContext = false;
445 #endif
446 
447  bool setFromCache( const QgsCoordinateReferenceSystem &src,
448  const QgsCoordinateReferenceSystem &dest,
449  int srcDatumTransform,
450  int destDatumTransform );
451  void addToCache();
452 
453  // cache
454  static QReadWriteLock sCacheLock;
455  static QMultiHash< QPair< QString, QString >, QgsCoordinateTransform > sTransforms; //same auth_id pairs might have different datum transformations
456  static bool sDisableCache;
457 
458 };
459 
461 #ifndef SIP_RUN
462 inline std::ostream &operator << ( std::ostream &os, const QgsCoordinateTransform &r )
463 {
464  QString mySummary( QStringLiteral( "\n%%%%%%%%%%%%%%%%%%%%%%%%\nCoordinate Transform def begins:" ) );
465  mySummary += QLatin1String( "\n\tInitialized? : " );
466  //prevent warnings
467  if ( r.isValid() )
468  {
469  //do nothing this is a dummy
470  }
471 
472 #if 0
473  if ( r.isValid() )
474  {
475  mySummary += "Yes";
476  }
477  else
478  {
479  mySummary += "No";
480  }
481  mySummary += "\n\tShort Circuit? : ";
482  if ( r.isShortCircuited() )
483  {
484  mySummary += "Yes";
485  }
486  else
487  {
488  mySummary += "No";
489  }
490 
491  mySummary += "\n\tSource Spatial Ref Sys : ";
492  if ( r.sourceCrs() )
493  {
494  mySummary << r.sourceCrs();
495  }
496  else
497  {
498  mySummary += "Undefined";
499  }
500 
501  mySummary += "\n\tDest Spatial Ref Sys : ";
502  if ( r.destCRS() )
503  {
504  mySummary << r.destCRS();
505  }
506  else
507  {
508  mySummary += "Undefined";
509  }
510 #endif
511 
512  mySummary += QLatin1String( "\nCoordinate Transform def ends \n%%%%%%%%%%%%%%%%%%%%%%%%\n" );
513  return os << mySummary.toLocal8Bit().data() << std::endl;
514 }
515 #endif
516 
517 
518 #endif // QGSCOORDINATETRANSFORM_H
A rectangle specified with double values.
Definition: qgsrectangle.h:40
QgsCoordinateReferenceSystem sourceCrs() const
Returns the source coordinate reference system, which the transform will transform coordinates from...
A class to represent a 2D point.
Definition: qgspointxy.h:43
TransformDirection
Enum used to indicate the direction (forward or inverse) of the transform.
const QgsCoordinateReferenceSystem & crs
bool isShortCircuited() const
Returns true if the transform short circuits because the source and destination are equivalent...
#define SIP_SKIP
Definition: qgis_sip.h:119
Reads and writes project states.
Definition: qgsproject.h:89
Contains information about the context in which a coordinate transform is executed.
#define SIP_PYARGREMOVE
Definition: qgis_sip.h:139
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
std::ostream & operator<<(std::ostream &os, const QgsCoordinateTransform &r)
Output stream operator.
bool isValid() const
Returns true if the coordinate transform is valid, ie both the source and destination CRS have been s...