QGIS API Documentation 4.3.0-Master (0cfde48c85b)
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 QgsBox3D;
34class QgsPointXY;
35class QgsRectangle;
36class QPolygonF;
37class QgsProject;
38class QgsVector3D;
39
62class CORE_EXPORT QgsCoordinateTransform
63{
64 public:
67
95 const QgsCoordinateReferenceSystem &source,
96 const QgsCoordinateReferenceSystem &destination,
99 );
100
126 explicit QgsCoordinateTransform(
127 const QgsCoordinateReferenceSystem &source,
128 const QgsCoordinateReferenceSystem &destination,
129 const QgsProject *project,
131 );
132
142
145
147
148 bool operator==( const QgsCoordinateTransform &other ) const;
149 bool operator!=( const QgsCoordinateTransform &other ) const;
150
163 static bool isTransformationPossible( const QgsCoordinateReferenceSystem &source, const QgsCoordinateReferenceSystem &destination );
164
169 bool isValid() const;
170
178
186
193
201
209
217
228
240
252
268 QgsRectangle transformBoundingBox( const QgsRectangle &rectangle, Qgis::TransformDirection direction = Qgis::TransformDirection::Forward, bool handle180Crossover = false ) const SIP_THROW(
270 );
271
289
303
317 void transformInPlace( float &x, float &y, double &z, Qgis::TransformDirection direction = Qgis::TransformDirection::Forward ) const SIP_SKIP;
318
332 void transformInPlace( float &x, float &y, float &z, Qgis::TransformDirection direction = Qgis::TransformDirection::Forward ) const SIP_SKIP;
333
347 void transformInPlace( QVector<float> &x, QVector<float> &y, QVector<float> &z, Qgis::TransformDirection direction = Qgis::TransformDirection::Forward ) const SIP_SKIP;
348
362 void transformInPlace( QVector<double> &x, QVector<double> &y, QVector<double> &z, Qgis::TransformDirection direction = Qgis::TransformDirection::Forward ) const SIP_SKIP;
363
371
382
394 void transformCoords( int numPoint, double *x, double *y, double *z, Qgis::TransformDirection direction = Qgis::TransformDirection::Forward ) const SIP_THROW( QgsCsException );
395
399 bool isShortCircuited() const;
400
407 bool hasVerticalComponent() const;
408
425 QString coordinateOperation() const;
426
441
455 void setCoordinateOperation( const QString &operation ) const;
456
471 void setAllowFallbackTransforms( bool allowed );
472
484 bool allowFallbackTransforms() const;
485
517 void setBallparkTransformsAreAppropriate( bool appropriate );
518
533 void disableFallbackOperationHandler( bool disabled );
534
543 bool fallbackOperationOccurred() const;
544
558 Q_DECL_DEPRECATED int sourceDatumTransformId() const SIP_DEPRECATED;
559
573 Q_DECL_DEPRECATED void setSourceDatumTransformId( int datumId ) SIP_DEPRECATED;
574
588 Q_DECL_DEPRECATED int destinationDatumTransformId() const SIP_DEPRECATED;
589
603 Q_DECL_DEPRECATED void setDestinationDatumTransformId( int datumId ) SIP_DEPRECATED;
604
605#ifndef SIP_RUN
606
616 static void invalidateCache( bool disableCache = false );
617#else
618
625 static void invalidateCache( bool disableCache SIP_PYARGREMOVE = false );
626#endif
627
637 double scaleFactor( const QgsRectangle &referenceExtent ) const;
638
639#ifdef SIP_RUN
640 // clang-format off
641 SIP_PYOBJECT __repr__();
642 % MethodCode
643 QString str = u"<QgsCoordinateTransform: %1 to %2>"_s.arg( sipCpp->sourceCrs().isValid() ? sipCpp->sourceCrs().authid() : u"NULL"_s,
644 sipCpp->destinationCrs().isValid() ? sipCpp->destinationCrs().authid() : u"NULL"_s );
645 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
646 % End
647// clang-format on
648#endif
649
650#ifndef SIP_RUN
651
665 const std::function< void( const QgsCoordinateReferenceSystem &sourceCrs, const QgsCoordinateReferenceSystem &destinationCrs, const QgsDatumTransform::GridDetails &grid )> &handler
666 );
667
685 const std::function< void(
687 )> &handler
688 );
689
703 const std::function< void( const QgsCoordinateReferenceSystem &sourceCrs, const QgsCoordinateReferenceSystem &destinationCrs, const QString &error )> &handler
704 );
705
719 const std::function< void( const QgsCoordinateReferenceSystem &sourceCrs, const QgsCoordinateReferenceSystem &destinationCrs, const QgsDatumTransform::TransformDetails &desiredOperation )> &handler
720 );
721
722
730 const std::function< void( const QgsCoordinateReferenceSystem &sourceCrs, const QgsCoordinateReferenceSystem &destinationCrs, const QString &desiredOperation )> &handler
731 );
732
739 static void setDynamicCrsToDynamicCrsWarningHandler( const std::function< void( const QgsCoordinateReferenceSystem &sourceCrs, const QgsCoordinateReferenceSystem &destinationCrs )> &handler );
740
741#endif
742
743 private:
744#ifndef SIP_RUN
745 friend class QgsProjContext;
746
747 // Only meant to be called by QgsProjContext::~QgsProjContext()
748 static void removeFromCacheObjectsBelongingToCurrentThread( void *pj_context );
749#endif
750
751 mutable QExplicitlySharedDataPointer<QgsCoordinateTransformPrivate> d;
752
755
756#ifdef QGISDEBUG
757 bool mHasContext = false;
758#endif
759
760 mutable QString mLastError;
761 bool mIgnoreImpossible = false;
762 bool mBallparkTransformsAreAppropriate = false;
763 bool mDisableFallbackHandler = false;
764 mutable bool mFallbackOperationOccurred = false;
765
766 bool setFromCache( const QgsCoordinateReferenceSystem &src, const QgsCoordinateReferenceSystem &dest, const QString &coordinateOperationProj, bool allowFallback );
767
768 void addToCache();
769
770 // cache
771 static QReadWriteLock sCacheLock;
772
780 static QMultiHash< QPair< QString, QString >, QgsCoordinateTransform > sTransforms;
781 static bool sDisableCache;
782
783
784 static std::function< void( const QgsCoordinateReferenceSystem &sourceCrs, const QgsCoordinateReferenceSystem &destinationCrs, const QString &desiredOperation )> sFallbackOperationOccurredHandler;
785
787};
788
790#ifndef SIP_RUN
791inline std::ostream &operator<<( std::ostream &os, const QgsCoordinateTransform &r )
792{
793 QString mySummary( u"\n%%%%%%%%%%%%%%%%%%%%%%%%\nCoordinate Transform def begins:"_s );
794 mySummary += "\n\tInitialized? : "_L1;
795 //prevent warnings
796 if ( r.isValid() )
797 {
798 //do nothing this is a dummy
799 }
800
801#if 0
802 if ( r.isValid() )
803 {
804 mySummary += "Yes";
805 }
806 else
807 {
808 mySummary += "No";
809 }
810 mySummary += "\n\tShort Circuit? : ";
811 if ( r.isShortCircuited() )
812 {
813 mySummary += "Yes";
814 }
815 else
816 {
817 mySummary += "No";
818 }
819
820 mySummary += "\n\tSource Spatial Ref Sys : ";
821 if ( r.sourceCrs() )
822 {
823 mySummary << r.sourceCrs();
824 }
825 else
826 {
827 mySummary += "Undefined";
828 }
829
830 mySummary += "\n\tDest Spatial Ref Sys : ";
831 if ( r.destCRS() )
832 {
833 mySummary << r.destCRS();
834 }
835 else
836 {
837 mySummary += "Undefined";
838 }
839#endif
840
841 mySummary += "\nCoordinate Transform def ends \n%%%%%%%%%%%%%%%%%%%%%%%%\n"_L1;
842 return os << mySummary.toLocal8Bit().data() << std::endl;
843}
844#endif
845
846
847#endif // QGSCOORDINATETRANSFORM_H
QFlags< CoordinateTransformationFlag > CoordinateTransformationFlags
Coordinate transformation flags.
Definition qgis.h:2907
TransformDirection
Indicates the direction (forward or inverse) of a transform.
Definition qgis.h:2882
@ Forward
Forward transform (from source to destination).
Definition qgis.h:2883
A 3-dimensional box composed of x, y, z coordinates.
Definition qgsbox3d.h:45
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.
QgsBox3D transformBox3D(const QgsBox3D &box, Qgis::TransformDirection direction=Qgis::TransformDirection::Forward) const
Transforms a 3D box from the source CRS to the destination CRS.
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:114
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:138
#define SIP_PYARGREMOVE
Definition qgis_sip.h:163
#define SIP_INOUT
Definition qgis_sip.h:78
#define SIP_THROW(name,...)
Definition qgis_sip.h:215
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.