QGIS API Documentation 3.99.0-Master (d270888f95f)
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#include <QString>
29
30using namespace Qt::StringLiterals;
31
32class QgsCoordinateTransformPrivate;
33class QgsPointXY;
34class QgsRectangle;
35class QPolygonF;
36class QgsProject;
37class QgsVector3D;
38
61class CORE_EXPORT QgsCoordinateTransform
62{
63
64 public:
65
68
96 const QgsCoordinateReferenceSystem &destination,
99
126 const QgsCoordinateReferenceSystem &destination,
127 const QgsProject *project,
129
137 Q_DECL_DEPRECATED explicit QgsCoordinateTransform( const QgsCoordinateReferenceSystem &source,
138 const QgsCoordinateReferenceSystem &destination,
141
144
146
147 bool operator==( const QgsCoordinateTransform &other ) const;
148 bool operator!=( const QgsCoordinateTransform &other ) const;
149
162 static bool isTransformationPossible( const QgsCoordinateReferenceSystem &source, const QgsCoordinateReferenceSystem &destination );
163
168 bool isValid() const;
169
177
185
192
200
208
216
227
239
251
267 QgsRectangle transformBoundingBox( const QgsRectangle &rectangle, Qgis::TransformDirection direction = Qgis::TransformDirection::Forward, bool handle180Crossover = false ) const SIP_THROW( QgsCsException );
268
281 void transformInPlace( double &x, double &y, double &z, Qgis::TransformDirection direction = Qgis::TransformDirection::Forward ) const SIP_THROW( QgsCsException );
282
296 void transformInPlace( float &x, float &y, double &z, Qgis::TransformDirection direction = Qgis::TransformDirection::Forward ) const SIP_SKIP;
297
311 void transformInPlace( float &x, float &y, float &z, Qgis::TransformDirection direction = Qgis::TransformDirection::Forward ) const SIP_SKIP;
312
326 void transformInPlace( QVector<float> &x, QVector<float> &y, QVector<float> &z,
328
342 void transformInPlace( QVector<double> &x, QVector<double> &y, QVector<double> &z,
344
352
363
375 void transformCoords( int numPoint, double *x, double *y, double *z, Qgis::TransformDirection direction = Qgis::TransformDirection::Forward ) const SIP_THROW( QgsCsException );
376
380 bool isShortCircuited() const;
381
388 bool hasVerticalComponent() const;
389
406 QString coordinateOperation() const;
407
422
436 void setCoordinateOperation( const QString &operation ) const;
437
452 void setAllowFallbackTransforms( bool allowed );
453
465 bool allowFallbackTransforms() const;
466
498 void setBallparkTransformsAreAppropriate( bool appropriate );
499
514 void disableFallbackOperationHandler( bool disabled );
515
524 bool fallbackOperationOccurred() const;
525
539 Q_DECL_DEPRECATED int sourceDatumTransformId() const SIP_DEPRECATED;
540
554 Q_DECL_DEPRECATED void setSourceDatumTransformId( int datumId ) SIP_DEPRECATED;
555
569 Q_DECL_DEPRECATED int destinationDatumTransformId() const SIP_DEPRECATED;
570
584 Q_DECL_DEPRECATED void setDestinationDatumTransformId( int datumId ) SIP_DEPRECATED;
585
586#ifndef SIP_RUN
587
597 static void invalidateCache( bool disableCache = false );
598#else
599
606 static void invalidateCache( bool disableCache SIP_PYARGREMOVE = false );
607#endif
608
618 double scaleFactor( const QgsRectangle &referenceExtent ) const;
619
620#ifdef SIP_RUN
621 SIP_PYOBJECT __repr__();
622 % MethodCode
623 QString str = u"<QgsCoordinateTransform: %1 to %2>"_s.arg( sipCpp->sourceCrs().isValid() ? sipCpp->sourceCrs().authid() : u"NULL"_s,
624 sipCpp->destinationCrs().isValid() ? sipCpp->destinationCrs().authid() : u"NULL"_s );
625 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
626 % End
627#endif
628
629#ifndef SIP_RUN
630
643 static void setCustomMissingRequiredGridHandler( const std::function< void( const QgsCoordinateReferenceSystem &sourceCrs,
645 const QgsDatumTransform::GridDetails &grid )> &handler );
646
663 static void setCustomMissingPreferredGridHandler( const std::function< void( const QgsCoordinateReferenceSystem &sourceCrs,
665 const QgsDatumTransform::TransformDetails &preferredOperation,
666 const QgsDatumTransform::TransformDetails &availableOperation )> &handler );
667
682 const QString &error )> &handler );
683
696 static void setCustomMissingGridUsedByContextHandler( const std::function< void( const QgsCoordinateReferenceSystem &sourceCrs,
698 const QgsDatumTransform::TransformDetails &desiredOperation )> &handler );
699
700
707 static void setFallbackOperationOccurredHandler( const std::function< void( const QgsCoordinateReferenceSystem &sourceCrs,
709 const QString &desiredOperation )> &handler );
710
717 static void setDynamicCrsToDynamicCrsWarningHandler( const std::function< void( const QgsCoordinateReferenceSystem &sourceCrs,
718 const QgsCoordinateReferenceSystem &destinationCrs )> &handler );
719
720#endif
721
722 private:
723
724#ifndef SIP_RUN
725 friend class QgsProjContext;
726
727 // Only meant to be called by QgsProjContext::~QgsProjContext()
728 static void removeFromCacheObjectsBelongingToCurrentThread( void *pj_context );
729#endif
730
731 mutable QExplicitlySharedDataPointer<QgsCoordinateTransformPrivate> d;
732
735
736#ifdef QGISDEBUG
737 bool mHasContext = false;
738#endif
739
740 mutable QString mLastError;
741 bool mIgnoreImpossible = false;
742 bool mBallparkTransformsAreAppropriate = false;
743 bool mDisableFallbackHandler = false;
744 mutable bool mFallbackOperationOccurred = false;
745
746 bool setFromCache( const QgsCoordinateReferenceSystem &src,
748 const QString &coordinateOperationProj, bool allowFallback );
749
750 void addToCache();
751
752 // cache
753 static QReadWriteLock sCacheLock;
754
762 static QMultiHash< QPair< QString, QString >, QgsCoordinateTransform > sTransforms;
763 static bool sDisableCache;
764
765
766 static std::function< void( const QgsCoordinateReferenceSystem &sourceCrs,
768 const QString &desiredOperation )> sFallbackOperationOccurredHandler;
769
771
772};
773
775#ifndef SIP_RUN
776inline std::ostream &operator << ( std::ostream &os, const QgsCoordinateTransform &r )
777{
778 QString mySummary( u"\n%%%%%%%%%%%%%%%%%%%%%%%%\nCoordinate Transform def begins:"_s );
779 mySummary += "\n\tInitialized? : "_L1;
780 //prevent warnings
781 if ( r.isValid() )
782 {
783 //do nothing this is a dummy
784 }
785
786#if 0
787 if ( r.isValid() )
788 {
789 mySummary += "Yes";
790 }
791 else
792 {
793 mySummary += "No";
794 }
795 mySummary += "\n\tShort Circuit? : ";
796 if ( r.isShortCircuited() )
797 {
798 mySummary += "Yes";
799 }
800 else
801 {
802 mySummary += "No";
803 }
804
805 mySummary += "\n\tSource Spatial Ref Sys : ";
806 if ( r.sourceCrs() )
807 {
808 mySummary << r.sourceCrs();
809 }
810 else
811 {
812 mySummary += "Undefined";
813 }
814
815 mySummary += "\n\tDest Spatial Ref Sys : ";
816 if ( r.destCRS() )
817 {
818 mySummary << r.destCRS();
819 }
820 else
821 {
822 mySummary += "Undefined";
823 }
824#endif
825
826 mySummary += "\nCoordinate Transform def ends \n%%%%%%%%%%%%%%%%%%%%%%%%\n"_L1;
827 return os << mySummary.toLocal8Bit().data() << std::endl;
828}
829#endif
830
831
832#endif // QGSCOORDINATETRANSFORM_H
QFlags< CoordinateTransformationFlag > CoordinateTransformationFlags
Coordinate transformation flags.
Definition qgis.h:2752
TransformDirection
Indicates the direction (forward or inverse) of a transform.
Definition qgis.h:2729
@ Forward
Forward transform (from source to destination).
Definition qgis.h:2730
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:62
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
Definition qgsproject.h:112
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:33
#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.