QGIS API Documentation 3.99.0-Master (26c88405ac0)
Loading...
Searching...
No Matches
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 "qgsconfig.h"
21
22#include "qgis_core.h"
23#include "qgis_sip.h"
26
27#include <QExplicitlySharedDataPointer>
28
29class QgsCoordinateTransformPrivate;
30class QgsPointXY;
31class QgsRectangle;
32class QPolygonF;
33class QgsProject;
34class QgsVector3D;
35
58class CORE_EXPORT QgsCoordinateTransform
59{
60
61 public:
62
65
93 const QgsCoordinateReferenceSystem &destination,
96
123 const QgsCoordinateReferenceSystem &destination,
124 const QgsProject *project,
126
134 Q_DECL_DEPRECATED explicit QgsCoordinateTransform( const QgsCoordinateReferenceSystem &source,
135 const QgsCoordinateReferenceSystem &destination,
138
141
143
144 bool operator==( const QgsCoordinateTransform &other ) const;
145 bool operator!=( const QgsCoordinateTransform &other ) const;
146
159 static bool isTransformationPossible( const QgsCoordinateReferenceSystem &source, const QgsCoordinateReferenceSystem &destination );
160
165 bool isValid() const;
166
174
182
189
197
205
213
224
236
248
264 QgsRectangle transformBoundingBox( const QgsRectangle &rectangle, Qgis::TransformDirection direction = Qgis::TransformDirection::Forward, bool handle180Crossover = false ) const SIP_THROW( QgsCsException );
265
278 void transformInPlace( double &x, double &y, double &z, Qgis::TransformDirection direction = Qgis::TransformDirection::Forward ) const SIP_THROW( QgsCsException );
279
293 void transformInPlace( float &x, float &y, double &z, Qgis::TransformDirection direction = Qgis::TransformDirection::Forward ) const SIP_SKIP;
294
308 void transformInPlace( float &x, float &y, float &z, Qgis::TransformDirection direction = Qgis::TransformDirection::Forward ) const SIP_SKIP;
309
323 void transformInPlace( QVector<float> &x, QVector<float> &y, QVector<float> &z,
325
339 void transformInPlace( QVector<double> &x, QVector<double> &y, QVector<double> &z,
341
349
360
372 void transformCoords( int numPoint, double *x, double *y, double *z, Qgis::TransformDirection direction = Qgis::TransformDirection::Forward ) const SIP_THROW( QgsCsException );
373
377 bool isShortCircuited() const;
378
385 bool hasVerticalComponent() const;
386
403 QString coordinateOperation() const;
404
419
433 void setCoordinateOperation( const QString &operation ) const;
434
449 void setAllowFallbackTransforms( bool allowed );
450
462 bool allowFallbackTransforms() const;
463
495 void setBallparkTransformsAreAppropriate( bool appropriate );
496
511 void disableFallbackOperationHandler( bool disabled );
512
521 bool fallbackOperationOccurred() const;
522
536 Q_DECL_DEPRECATED int sourceDatumTransformId() const SIP_DEPRECATED;
537
551 Q_DECL_DEPRECATED void setSourceDatumTransformId( int datumId ) SIP_DEPRECATED;
552
566 Q_DECL_DEPRECATED int destinationDatumTransformId() const SIP_DEPRECATED;
567
581 Q_DECL_DEPRECATED void setDestinationDatumTransformId( int datumId ) SIP_DEPRECATED;
582
583#ifndef SIP_RUN
584
594 static void invalidateCache( bool disableCache = false );
595#else
596
603 static void invalidateCache( bool disableCache SIP_PYARGREMOVE = false );
604#endif
605
615 double scaleFactor( const QgsRectangle &referenceExtent ) const;
616
617#ifdef SIP_RUN
618 SIP_PYOBJECT __repr__();
619 % MethodCode
620 QString str = QStringLiteral( "<QgsCoordinateTransform: %1 to %2>" ).arg( sipCpp->sourceCrs().isValid() ? sipCpp->sourceCrs().authid() : QStringLiteral( "NULL" ),
621 sipCpp->destinationCrs().isValid() ? sipCpp->destinationCrs().authid() : QStringLiteral( "NULL" ) );
622 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
623 % End
624#endif
625
626#ifndef SIP_RUN
627
640 static void setCustomMissingRequiredGridHandler( const std::function< void( const QgsCoordinateReferenceSystem &sourceCrs,
642 const QgsDatumTransform::GridDetails &grid )> &handler );
643
660 static void setCustomMissingPreferredGridHandler( const std::function< void( const QgsCoordinateReferenceSystem &sourceCrs,
662 const QgsDatumTransform::TransformDetails &preferredOperation,
663 const QgsDatumTransform::TransformDetails &availableOperation )> &handler );
664
679 const QString &error )> &handler );
680
693 static void setCustomMissingGridUsedByContextHandler( const std::function< void( const QgsCoordinateReferenceSystem &sourceCrs,
695 const QgsDatumTransform::TransformDetails &desiredOperation )> &handler );
696
697
704 static void setFallbackOperationOccurredHandler( const std::function< void( const QgsCoordinateReferenceSystem &sourceCrs,
706 const QString &desiredOperation )> &handler );
707
714 static void setDynamicCrsToDynamicCrsWarningHandler( const std::function< void( const QgsCoordinateReferenceSystem &sourceCrs,
715 const QgsCoordinateReferenceSystem &destinationCrs )> &handler );
716
717#endif
718
719 private:
720
721#ifndef SIP_RUN
722 friend class QgsProjContext;
723
724 // Only meant to be called by QgsProjContext::~QgsProjContext()
725 static void removeFromCacheObjectsBelongingToCurrentThread( void *pj_context );
726#endif
727
728 mutable QExplicitlySharedDataPointer<QgsCoordinateTransformPrivate> d;
729
732
733#ifdef QGISDEBUG
734 bool mHasContext = false;
735#endif
736
737 mutable QString mLastError;
738 bool mIgnoreImpossible = false;
739 bool mBallparkTransformsAreAppropriate = false;
740 bool mDisableFallbackHandler = false;
741 mutable bool mFallbackOperationOccurred = false;
742
743 bool setFromCache( const QgsCoordinateReferenceSystem &src,
745 const QString &coordinateOperationProj, bool allowFallback );
746
747 void addToCache();
748
749 // cache
750 static QReadWriteLock sCacheLock;
751
759 static QMultiHash< QPair< QString, QString >, QgsCoordinateTransform > sTransforms;
760 static bool sDisableCache;
761
762
763 static std::function< void( const QgsCoordinateReferenceSystem &sourceCrs,
765 const QString &desiredOperation )> sFallbackOperationOccurredHandler;
766
768
769};
770
772#ifndef SIP_RUN
773inline std::ostream &operator << ( std::ostream &os, const QgsCoordinateTransform &r )
774{
775 QString mySummary( QStringLiteral( "\n%%%%%%%%%%%%%%%%%%%%%%%%\nCoordinate Transform def begins:" ) );
776 mySummary += QLatin1String( "\n\tInitialized? : " );
777 //prevent warnings
778 if ( r.isValid() )
779 {
780 //do nothing this is a dummy
781 }
782
783#if 0
784 if ( r.isValid() )
785 {
786 mySummary += "Yes";
787 }
788 else
789 {
790 mySummary += "No";
791 }
792 mySummary += "\n\tShort Circuit? : ";
793 if ( r.isShortCircuited() )
794 {
795 mySummary += "Yes";
796 }
797 else
798 {
799 mySummary += "No";
800 }
801
802 mySummary += "\n\tSource Spatial Ref Sys : ";
803 if ( r.sourceCrs() )
804 {
805 mySummary << r.sourceCrs();
806 }
807 else
808 {
809 mySummary += "Undefined";
810 }
811
812 mySummary += "\n\tDest Spatial Ref Sys : ";
813 if ( r.destCRS() )
814 {
815 mySummary << r.destCRS();
816 }
817 else
818 {
819 mySummary += "Undefined";
820 }
821#endif
822
823 mySummary += QLatin1String( "\nCoordinate Transform def ends \n%%%%%%%%%%%%%%%%%%%%%%%%\n" );
824 return os << mySummary.toLocal8Bit().data() << std::endl;
825}
826#endif
827
828
829#endif // QGSCOORDINATETRANSFORM_H
QFlags< CoordinateTransformationFlag > CoordinateTransformationFlags
Coordinate transformation flags.
Definition qgis.h:2694
TransformDirection
Indicates the direction (forward or inverse) of a transform.
Definition qgis.h:2671
@ Forward
Forward transform (from source to destination).
Definition qgis.h:2672
Represents a coordinate reference system (CRS).
Contains information about the context in which a coordinate transform is executed.
Handles coordinate transforms between two coordinate systems.
QgsCoordinateTransformContext context() const
Returns the context in which the coordinate transform will be calculated.
QgsCoordinateTransform()
Default constructor, creates an invalid QgsCoordinateTransform.
static bool isTransformationPossible(const QgsCoordinateReferenceSystem &source, const QgsCoordinateReferenceSystem &destination)
Returns true if it is theoretically possible to transform between source and destination CRSes.
QgsCoordinateReferenceSystem sourceCrs() const
Returns the source coordinate reference system, which the transform will transform coordinates from.
bool allowFallbackTransforms() const
Returns whether "ballpark" fallback transformations will be used in the case that the specified coord...
static void setCustomMissingRequiredGridHandler(const std::function< void(const QgsCoordinateReferenceSystem &sourceCrs, const QgsCoordinateReferenceSystem &destinationCrs, const QgsDatumTransform::GridDetails &grid)> &handler)
Sets a custom handler to use when a coordinate transform is created between sourceCrs and destination...
QgsDatumTransform::TransformDetails instantiatedCoordinateOperationDetails() const
Returns the transform details representing the coordinate operation which is being used to transform ...
Q_DECL_DEPRECATED void setDestinationDatumTransformId(int datumId)
Sets the datumId ID of the datum transform to use when projecting to the destination CRS.
void setContext(const QgsCoordinateTransformContext &context)
Sets the context in which the coordinate transform should be calculated.
QString coordinateOperation() const
Returns a Proj string representing the coordinate operation which will be used to transform coordinat...
void setSourceCrs(const QgsCoordinateReferenceSystem &crs)
Sets the source coordinate reference system.
void setBallparkTransformsAreAppropriate(bool appropriate)
Sets whether approximate "ballpark" results are appropriate for this coordinate transform.
double scaleFactor(const QgsRectangle &referenceExtent) const
Computes an estimated conversion factor between source and destination units:
static void setCustomCoordinateOperationCreationErrorHandler(const std::function< void(const QgsCoordinateReferenceSystem &sourceCrs, const QgsCoordinateReferenceSystem &destinationCrs, const QString &error)> &handler)
Sets a custom handler to use when a coordinate transform was required between sourceCrs and destinati...
void transformCoords(int numPoint, double *x, double *y, double *z, Qgis::TransformDirection direction=Qgis::TransformDirection::Forward) const
Transform an array of coordinates to the destination CRS.
void setDestinationCrs(const QgsCoordinateReferenceSystem &crs)
Sets the destination coordinate reference system.
QgsPointXY transform(const QgsPointXY &point, Qgis::TransformDirection direction=Qgis::TransformDirection::Forward) const
Transform the point from the source CRS to the destination CRS.
void transformInPlace(double &x, double &y, double &z, Qgis::TransformDirection direction=Qgis::TransformDirection::Forward) const
Transforms an array of x, y and z double coordinates in place, from the source CRS to the destination...
bool isShortCircuited() const
Returns true if the transform short circuits because the source and destination are equivalent.
Q_DECL_DEPRECATED void setSourceDatumTransformId(int datumId)
Sets the datumId ID of the datum transform to use when projecting from the source CRS.
bool fallbackOperationOccurred() const
Returns true if a fallback operation occurred for the most recent transform.
void transformPolygon(QPolygonF &polygon, Qgis::TransformDirection direction=Qgis::TransformDirection::Forward) const
Transforms a polygon to the destination coordinate system.
QgsCoordinateTransform & operator=(const QgsCoordinateTransform &o)
QgsRectangle transformBoundingBox(const QgsRectangle &rectangle, Qgis::TransformDirection direction=Qgis::TransformDirection::Forward, bool handle180Crossover=false) const
Transforms a rectangle from the source CRS to the destination CRS.
bool hasVerticalComponent() const
Returns true if the transform includes a vertical component, i.e.
void setAllowFallbackTransforms(bool allowed)
Sets whether "ballpark" fallback transformations can be used in the case that the specified coordinat...
bool isValid() const
Returns true if the coordinate transform is valid, ie both the source and destination CRS have been s...
static void invalidateCache(bool disableCache=false)
Clears the internal cache used to initialize QgsCoordinateTransform objects.
static void setCustomMissingPreferredGridHandler(const std::function< void(const QgsCoordinateReferenceSystem &sourceCrs, const QgsCoordinateReferenceSystem &destinationCrs, const QgsDatumTransform::TransformDetails &preferredOperation, const QgsDatumTransform::TransformDetails &availableOperation)> &handler)
Sets a custom handler to use when a coordinate transform is created between sourceCrs and destination...
QgsCoordinateReferenceSystem destinationCrs() const
Returns the destination coordinate reference system, which the transform will transform coordinates t...
Q_DECL_DEPRECATED int destinationDatumTransformId() const
Returns the ID of the datum transform to use when projecting to the destination CRS.
void disableFallbackOperationHandler(bool disabled)
Sets whether the default fallback operation handler is disabled for this transform instance.
void setCoordinateOperation(const QString &operation) const
Sets a Proj string representing the coordinate operation which will be used to transform coordinates.
static void setDynamicCrsToDynamicCrsWarningHandler(const std::function< void(const QgsCoordinateReferenceSystem &sourceCrs, const QgsCoordinateReferenceSystem &destinationCrs)> &handler)
Sets a custom handler to use when the desired coordinate operation for use between sourceCrs and dest...
Q_DECL_DEPRECATED int sourceDatumTransformId() const
Returns the ID of the datum transform to use when projecting from the source CRS.
static void setCustomMissingGridUsedByContextHandler(const std::function< void(const QgsCoordinateReferenceSystem &sourceCrs, const QgsCoordinateReferenceSystem &destinationCrs, const QgsDatumTransform::TransformDetails &desiredOperation)> &handler)
Sets a custom handler to use when a coordinate operation was specified for use between sourceCrs and ...
static void setFallbackOperationOccurredHandler(const std::function< void(const QgsCoordinateReferenceSystem &sourceCrs, const QgsCoordinateReferenceSystem &destinationCrs, const QString &desiredOperation)> &handler)
Sets a custom handler to use when the desired coordinate operation for use between sourceCrs and dest...
Custom exception class for Coordinate Reference System related exceptions.
Represents a 2D point.
Definition qgspointxy.h:60
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
Definition qgsproject.h:109
A rectangle specified with double values.
A 3D vector (similar to QVector3D) with the difference that it uses double precision instead of singl...
Definition qgsvector3d.h:30
#define SIP_DEPRECATED
Definition qgis_sip.h:114
#define SIP_SKIP
Definition qgis_sip.h:134
#define SIP_PYARGREMOVE
Definition qgis_sip.h:159
#define SIP_THROW(name,...)
Definition qgis_sip.h:211
std::ostream & operator<<(std::ostream &os, const QgsCoordinateTransform &r)
Output stream operator.
bool operator==(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
bool operator!=(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
Contains information about a projection transformation grid file.
Contains information about a coordinate transformation operation.