QGIS API Documentation 4.0.0-Norrköping (1ddcee3d0e4)
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 public:
66
94 const QgsCoordinateReferenceSystem &source,
95 const QgsCoordinateReferenceSystem &destination,
98 );
99
125 explicit QgsCoordinateTransform(
126 const QgsCoordinateReferenceSystem &source,
127 const QgsCoordinateReferenceSystem &destination,
128 const QgsProject *project,
130 );
131
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(
269 );
270
283 void transformInPlace( double &x, double &y, double &z, Qgis::TransformDirection direction = Qgis::TransformDirection::Forward ) const SIP_THROW( QgsCsException );
284
298 void transformInPlace( float &x, float &y, double &z, Qgis::TransformDirection direction = Qgis::TransformDirection::Forward ) const SIP_SKIP;
299
313 void transformInPlace( float &x, float &y, float &z, Qgis::TransformDirection direction = Qgis::TransformDirection::Forward ) const SIP_SKIP;
314
328 void transformInPlace( QVector<float> &x, QVector<float> &y, QVector<float> &z, Qgis::TransformDirection direction = Qgis::TransformDirection::Forward ) const SIP_SKIP;
329
343 void transformInPlace( QVector<double> &x, QVector<double> &y, QVector<double> &z, Qgis::TransformDirection direction = Qgis::TransformDirection::Forward ) const SIP_SKIP;
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 // clang-format off
622 SIP_PYOBJECT __repr__();
623 % MethodCode
624 QString str = u"<QgsCoordinateTransform: %1 to %2>"_s.arg( sipCpp->sourceCrs().isValid() ? sipCpp->sourceCrs().authid() : u"NULL"_s,
625 sipCpp->destinationCrs().isValid() ? sipCpp->destinationCrs().authid() : u"NULL"_s );
626 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
627 % End
628// clang-format on
629#endif
630
631#ifndef SIP_RUN
632
646 const std::function< void( const QgsCoordinateReferenceSystem &sourceCrs, const QgsCoordinateReferenceSystem &destinationCrs, const QgsDatumTransform::GridDetails &grid )> &handler
647 );
648
666 const std::function< void(
668 )> &handler
669 );
670
684 const std::function< void( const QgsCoordinateReferenceSystem &sourceCrs, const QgsCoordinateReferenceSystem &destinationCrs, const QString &error )> &handler
685 );
686
700 const std::function< void( const QgsCoordinateReferenceSystem &sourceCrs, const QgsCoordinateReferenceSystem &destinationCrs, const QgsDatumTransform::TransformDetails &desiredOperation )> &handler
701 );
702
703
711 const std::function< void( const QgsCoordinateReferenceSystem &sourceCrs, const QgsCoordinateReferenceSystem &destinationCrs, const QString &desiredOperation )> &handler
712 );
713
720 static void setDynamicCrsToDynamicCrsWarningHandler( const std::function< void( const QgsCoordinateReferenceSystem &sourceCrs, const QgsCoordinateReferenceSystem &destinationCrs )> &handler );
721
722#endif
723
724 private:
725#ifndef SIP_RUN
726 friend class QgsProjContext;
727
728 // Only meant to be called by QgsProjContext::~QgsProjContext()
729 static void removeFromCacheObjectsBelongingToCurrentThread( void *pj_context );
730#endif
731
732 mutable QExplicitlySharedDataPointer<QgsCoordinateTransformPrivate> d;
733
736
737#ifdef QGISDEBUG
738 bool mHasContext = false;
739#endif
740
741 mutable QString mLastError;
742 bool mIgnoreImpossible = false;
743 bool mBallparkTransformsAreAppropriate = false;
744 bool mDisableFallbackHandler = false;
745 mutable bool mFallbackOperationOccurred = false;
746
747 bool setFromCache( const QgsCoordinateReferenceSystem &src, const QgsCoordinateReferenceSystem &dest, const QString &coordinateOperationProj, bool allowFallback );
748
749 void addToCache();
750
751 // cache
752 static QReadWriteLock sCacheLock;
753
761 static QMultiHash< QPair< QString, QString >, QgsCoordinateTransform > sTransforms;
762 static bool sDisableCache;
763
764
765 static std::function< void( const QgsCoordinateReferenceSystem &sourceCrs, const QgsCoordinateReferenceSystem &destinationCrs, const QString &desiredOperation )> sFallbackOperationOccurredHandler;
766
768};
769
771#ifndef SIP_RUN
772inline std::ostream &operator<<( std::ostream &os, const QgsCoordinateTransform &r )
773{
774 QString mySummary( u"\n%%%%%%%%%%%%%%%%%%%%%%%%\nCoordinate Transform def begins:"_s );
775 mySummary += "\n\tInitialized? : "_L1;
776 //prevent warnings
777 if ( r.isValid() )
778 {
779 //do nothing this is a dummy
780 }
781
782#if 0
783 if ( r.isValid() )
784 {
785 mySummary += "Yes";
786 }
787 else
788 {
789 mySummary += "No";
790 }
791 mySummary += "\n\tShort Circuit? : ";
792 if ( r.isShortCircuited() )
793 {
794 mySummary += "Yes";
795 }
796 else
797 {
798 mySummary += "No";
799 }
800
801 mySummary += "\n\tSource Spatial Ref Sys : ";
802 if ( r.sourceCrs() )
803 {
804 mySummary << r.sourceCrs();
805 }
806 else
807 {
808 mySummary += "Undefined";
809 }
810
811 mySummary += "\n\tDest Spatial Ref Sys : ";
812 if ( r.destCRS() )
813 {
814 mySummary << r.destCRS();
815 }
816 else
817 {
818 mySummary += "Undefined";
819 }
820#endif
821
822 mySummary += "\nCoordinate Transform def ends \n%%%%%%%%%%%%%%%%%%%%%%%%\n"_L1;
823 return os << mySummary.toLocal8Bit().data() << std::endl;
824}
825#endif
826
827
828#endif // QGSCOORDINATETRANSFORM_H
QFlags< CoordinateTransformationFlag > CoordinateTransformationFlags
Coordinate transformation flags.
Definition qgis.h:2789
TransformDirection
Indicates the direction (forward or inverse) of a transform.
Definition qgis.h:2764
@ Forward
Forward transform (from source to destination).
Definition qgis.h:2765
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:113
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:113
#define SIP_SKIP
Definition qgis_sip.h:133
#define SIP_PYARGREMOVE
Definition qgis_sip.h:158
#define SIP_THROW(name,...)
Definition qgis_sip.h:210
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.