33using namespace Qt::StringLiterals;
38#include <QApplication>
54QReadWriteLock QgsCoordinateTransform::sCacheLock;
56bool QgsCoordinateTransform::sDisableCache =
false;
59 QgsCoordinateTransform::sFallbackOperationOccurredHandler =
nullptr;
63 d =
new QgsCoordinateTransformPrivate();
71 d =
new QgsCoordinateTransformPrivate( source, destination, mContext );
74 mIgnoreImpossible =
true;
86 if ( !d->checkValidity() )
90 if ( !setFromCache( d->mSourceCRS, d->mDestCRS, d->mProjCoordinateOperation, d->mAllowFallbackTransforms ) )
98 mBallparkTransformsAreAppropriate =
true;
106 d =
new QgsCoordinateTransformPrivate( source, destination, mContext );
113 mIgnoreImpossible =
true;
121 if ( !d->checkValidity() )
125 if ( !setFromCache( d->mSourceCRS, d->mDestCRS, d->mProjCoordinateOperation, d->mAllowFallbackTransforms ) )
133 mBallparkTransformsAreAppropriate =
true;
138 d =
new QgsCoordinateTransformPrivate( source, destination, sourceDatumTransform, destinationDatumTransform );
143 if ( !d->checkValidity() )
147 if ( !setFromCache( d->mSourceCRS, d->mDestCRS, d->mProjCoordinateOperation, d->mAllowFallbackTransforms ) )
156 : mContext( o.mContext )
158 , mHasContext( o.mHasContext )
164 , mIgnoreImpossible( false )
165 , mBallparkTransformsAreAppropriate( false )
166 , mDisableFallbackHandler( false )
167 , mFallbackOperationOccurred( false )
179 mHasContext = o.mHasContext;
181 mContext = o.mContext;
182 mLastError = QString();
191 return d->mSourceCRS == other.d->mSourceCRS
192 && d->mDestCRS == other.d->mDestCRS
193 && mBallparkTransformsAreAppropriate == other.mBallparkTransformsAreAppropriate
194 && d->mProjCoordinateOperation == other.d->mProjCoordinateOperation
200 return !( *
this == other );
225 if ( !d->checkValidity() )
228 d->calculateTransforms( mContext );
230 if ( !setFromCache( d->mSourceCRS, d->mDestCRS, d->mProjCoordinateOperation, d->mAllowFallbackTransforms ) )
248 if ( !d->checkValidity() )
251 d->calculateTransforms( mContext );
253 if ( !setFromCache( d->mSourceCRS, d->mDestCRS, d->mProjCoordinateOperation, d->mAllowFallbackTransforms ) )
275 if ( !d->checkValidity() )
278 d->calculateTransforms( mContext );
280 if ( !setFromCache( d->mSourceCRS, d->mDestCRS, d->mProjCoordinateOperation, d->mAllowFallbackTransforms ) )
295 return d->mSourceCRS;
305 if ( !d->mIsValid || d->mShortCircuit )
309 double x = point.
x();
310 double y = point.
y();
343 if ( !d->mIsValid || d->mShortCircuit )
367#ifdef COORDINATE_TRANSFORM_VERBOSE
379 double x = point.
x();
380 double y = point.
y();
381 double z = point.
z();
397 if ( !d->mIsValid || d->mShortCircuit )
417 double xd =
static_cast< double >( x ), yd =
static_cast< double >( y );
425 if ( !d->mIsValid || d->mShortCircuit )
451 if ( !d->mIsValid || d->mShortCircuit )
457 const int nVertices = poly.size();
459 QVector<double> x( nVertices );
460 QVector<double> y( nVertices );
461 QVector<double> z( nVertices );
462 double *destX = x.data();
463 double *destY = y.data();
464 double *destZ = z.data();
466 const QPointF *polyData = poly.constData();
467 for (
int i = 0; i < nVertices; ++i )
469 *destX++ = polyData->x();
470 *destY++ = polyData->y();
486 QPointF *destPoint = poly.data();
487 const double *srcX = x.constData();
488 const double *srcY = y.constData();
489 for (
int i = 0; i < nVertices; ++i )
491 destPoint->rx() = *srcX++;
492 destPoint->ry() = *srcY++;
497 if ( !err.isEmpty() )
503 if ( !d->mIsValid || d->mShortCircuit )
506 Q_ASSERT( x.size() == y.size() );
528 if ( !d->mIsValid || d->mShortCircuit )
531 Q_ASSERT( x.size() == y.size() );
541 const int vectorSize = x.size();
542 QVector<double> xd( x.size() );
543 QVector<double> yd( y.size() );
544 QVector<double> zd( z.size() );
546 double *destX = xd.data();
547 double *destY = yd.data();
548 double *destZ = zd.data();
550 const float *srcX = x.constData();
551 const float *srcY = y.constData();
552 const float *srcZ = z.constData();
554 for (
int i = 0; i < vectorSize; ++i )
556 *destX++ =
static_cast< double >( *srcX++ );
557 *destY++ =
static_cast< double >( *srcY++ );
558 *destZ++ =
static_cast< double >( *srcZ++ );
564 float *destFX = x.data();
565 float *destFY = y.data();
566 float *destFZ = z.data();
567 const double *srcXD = xd.constData();
568 const double *srcYD = yd.constData();
569 const double *srcZD = zd.constData();
570 for (
int i = 0; i < vectorSize; ++i )
572 *destFX++ =
static_cast< float >( *srcXD++ );
573 *destFY++ =
static_cast< float >( *srcYD++ );
574 *destFZ++ =
static_cast< float >( *srcZD++ );
592 if ( !d->mIsValid || d->mShortCircuit )
604 QgsDebugMsgLevel( u
"No QgsCoordinateTransformContext context set for transform"_s, 4 );
612 throw QgsCsException( QObject::tr(
"Could not transform bounding box for geocentric CRS %1" ).arg( d->mSourceCRS.authid() ) );
616 throw QgsCsException( QObject::tr(
"Could not transform bounding box for geocentric CRS %1" ).arg( d->mDestCRS.authid() ) );
619 const double xMin = rect.
xMinimum();
620 const double xMax = rect.
xMaximum();
630 constexpr double EPS = 1e-1;
631 if ( yMin < -90 + EPS )
633 if ( yMax < -90 + EPS )
634 throw QgsCsException( QObject::tr(
"Could not transform bounding box to target CRS" ) );
637 if ( yMax > 90 - EPS )
639 if ( yMin > 90 - EPS )
640 throw QgsCsException( QObject::tr(
"Could not transform bounding box to target CRS" ) );
649#if PROJ_VERSION_MAJOR < 9 || ( PROJ_VERSION_MAJOR == 9 && PROJ_VERSION_MINOR < 7 )
650 const auto legacyImplementation = [
this, &rect, xMin, yMin, yMax, direction, handle180Crossover]() {
657 const int nPoints = 1000;
658 const double dst = std::sqrt( ( rect.
width() * ( yMax - yMin ) ) / std::pow( std::sqrt(
static_cast< double >( nPoints ) ) - 1, 2.0 ) );
659 const int nXPoints =
static_cast<int>( std::clamp( std::ceil( rect.
width() / dst ) + 1, 3.0, 1000.0 ) );
660 const int nYPoints =
static_cast<int>( std::clamp( std::ceil( ( yMax - yMin ) / dst ) + 1, 3.0, 1000.0 ) );
665 std::vector<double> x( nXPoints *
static_cast< std::size_t
>( nYPoints ) );
666 std::vector<double> y( nXPoints *
static_cast< std::size_t
>( nYPoints ) );
667 std::vector<double> z( nXPoints *
static_cast< std::size_t
>( nYPoints ) );
671 const double dx = rect.
width() /
static_cast< double >( nXPoints - 1 );
672 const double dy = ( yMax - yMin ) /
static_cast< double >( nYPoints - 1 );
674 double pointY = yMin;
676 for (
int i = 0; i < nYPoints; i++ )
679 double pointX = xMin;
681 for (
int j = 0; j < nXPoints; j++ )
683 x[( i * nXPoints ) + j] = pointX;
684 y[( i * nXPoints ) + j] = pointY;
686 z[( i * nXPoints ) + j] = 0.0;
697 transformCoords( nXPoints * nYPoints, x.data(), y.data(), z.data(), direction );
707 bool doHandle180Crossover =
false;
710 const double xMin = std::fmod( x[0], 180.0 );
711 const double xMax = std::fmod( x[nXPoints - 1], 180.0 );
712 if ( handle180Crossover
719 doHandle180Crossover =
true;
724 for (
int i = 0; i < nXPoints * nYPoints; i++ )
726 if ( !std::isfinite( x[i] ) || !std::isfinite( y[i] ) )
731 if ( doHandle180Crossover )
745 throw QgsCsException( QObject::tr(
"Could not transform bounding box to target CRS" ) );
748 if ( doHandle180Crossover )
769#if PROJ_VERSION_MAJOR > 8 || ( PROJ_VERSION_MAJOR == 8 && PROJ_VERSION_MINOR >= 2 )
771#if PROJ_VERSION_MAJOR < 9 || ( PROJ_VERSION_MAJOR == 9 && PROJ_VERSION_MINOR < 7 )
778 return legacyImplementation();
782 ProjData projData = d->threadLocalProjData();
785#if PROJ_VERSION_MAJOR < 9 || ( PROJ_VERSION_MAJOR == 9 && PROJ_VERSION_MINOR < 6 )
799 transform2D.reset( proj_create_crs_to_crs_from_pj( projContext, srcCrsHorizontal.get(), destCrsHorizontal.get(),
nullptr,
nullptr ) );
802 const QString err = u
"proj_create_crs_to_crs_from_pj(horizontalCrs(%1), horizontalCrs(%2)) failed"_s.arg( d->mSourceCRS.authid(), d->mSourceCRS.authid() );
803 throw QgsCsException( QObject::tr(
"Could not transform bounding box to target CRS: %1" ).arg( err ) );
805 transform2D.reset( proj_normalize_for_visualization( projContext, transform2D.get() ) );
808 const QString err = u
"Cannot normalize transform between horizontalCrs(%1) and horizontalCrs(%2)"_s.arg( d->mSourceCRS.authid(), d->mDestCRS.authid() );
809 throw QgsCsException( QObject::tr(
"Could not transform bounding box to target CRS: %1" ).arg( err ) );
811 projData = transform2D.get();
815 double transXMin = 0;
816 double transYMin = 0;
817 double transXMax = 0;
818 double transYMax = 0;
820 proj_errno_reset( projData );
823 constexpr int DENSIFY_POINTS = 30;
824 int projResult = proj_trans_bounds(
840 ( projResult != 1 || !std::isfinite( transXMin ) || !std::isfinite( transXMax ) || !std::isfinite( transYMin ) || !std::isfinite( transYMax ) )
841 && ( d->mAvailableOpCount > 1 || d->mAvailableOpCount == -1 )
845 if (
PJ *
transform = d->threadLocalFallbackProjData() )
847 projResult = proj_trans_bounds(
864 if ( projResult != 1 || !std::isfinite( transXMin ) || !std::isfinite( transXMax ) || !std::isfinite( transYMin ) || !std::isfinite( transYMax ) )
866#if PROJ_VERSION_MAJOR < 9 || ( PROJ_VERSION_MAJOR == 9 && PROJ_VERSION_MINOR < 7 )
870 return legacyImplementation();
872 const QString projErr = QString::fromUtf8( proj_context_errno_string( projContext, proj_errno( projData ) ) );
874 const QString msg = QObject::tr(
"%1 (%2 to %3) of bounding box failed: %4" )
887 bool doHandle180Crossover =
false;
889 if ( handle180Crossover
891 && ( transXMax < transXMin ) )
894 std::swap( transXMax, transXMin );
899 doHandle180Crossover =
true;
902 QgsRectangle boundingBoxRect { transXMin, transYMin, transXMax, transYMax };
903 if ( boundingBoxRect.
isNull() )
906 throw QgsCsException( QObject::tr(
"Could not transform bounding box to target CRS" ) );
909 if ( doHandle180Crossover )
912 if ( boundingBoxRect.
xMinimum() > 180.0 )
914 if ( boundingBoxRect.
xMaximum() > 180.0 )
920 if ( boundingBoxRect.
isEmpty() )
925 return boundingBoxRect;
927#if PROJ_VERSION_MAJOR < 9 || ( PROJ_VERSION_MAJOR == 9 && PROJ_VERSION_MINOR < 7 )
928 return legacyImplementation();
934 if ( !d->mIsValid || d->mShortCircuit )
937 if ( !d->mSourceCRS.isValid() )
941 "The source spatial reference system (CRS) is not valid. "
942 "The coordinates can not be reprojected. The CRS is: %1"
944 .arg( d->mSourceCRS.toProj() ),
949 if ( !d->mDestCRS.isValid() )
953 "The destination spatial reference system (CRS) is not valid. "
954 "The coordinates can not be reprojected. The CRS is: %1"
956 .arg( d->mDestCRS.toProj() ),
962 std::vector< int > zNanPositions;
963 for (
int i = 0; i < numPoints; i++ )
965 if ( std::isnan( z[i] ) )
967 zNanPositions.push_back( i );
972 std::vector< double > xprev( numPoints );
973 memcpy( xprev.data(), x,
sizeof(
double ) * numPoints );
974 std::vector< double > yprev( numPoints );
975 memcpy( yprev.data(), y,
sizeof(
double ) * numPoints );
976 std::vector< double > zprev( numPoints );
977 memcpy( zprev.data(), z,
sizeof(
double ) * numPoints );
979 const bool useTime = !std::isnan( d->mDefaultTime );
980 std::vector< double > t( useTime ? numPoints : 0, d->mDefaultTime );
982#ifdef COORDINATE_TRANSFORM_VERBOSE
985 QgsDebugMsgLevel( u
"[[[[[[ Number of points to transform: %1 ]]]]]]"_s.arg( numPoints ), 2 );
991 QgsDebugMsgLevel( u
"No QgsCoordinateTransformContext context set for transform"_s, 4 );
996 ProjData projData = d->threadLocalProjData();
1000 proj_errno_reset( projData );
1013 useTime ? t.data() :
nullptr,
1015 useTime ? numPoints : 0
1026 if ( numPoints == 1 )
1028 projResult = proj_errno( projData );
1029 actualRes = projResult;
1033 actualRes = proj_errno( projData );
1035 if ( actualRes == 0 )
1039 if ( std::any_of( x, x + numPoints, [](
double v ) {
return std::isinf( v ); } )
1040 || std::any_of( y, y + numPoints, [](
double v ) {
return std::isinf( v ); } )
1041 || std::any_of( z, z + numPoints, [](
double v ) {
return std::isinf( v ); } ) )
1047 mFallbackOperationOccurred =
false;
1048 bool errorOccurredDuringFallbackOperation =
false;
1050 && ( d->mAvailableOpCount > 1 || d->mAvailableOpCount == -1 )
1051 && ( d->mAllowFallbackTransforms || mBallparkTransformsAreAppropriate ) )
1054 if (
PJ *
transform = d->threadLocalFallbackProjData() )
1058 memcpy( x, xprev.data(),
sizeof(
double ) * numPoints );
1059 memcpy( y, yprev.data(),
sizeof(
double ) * numPoints );
1060 memcpy( z, zprev.data(),
sizeof(
double ) * numPoints );
1073 useTime ? t.data() :
nullptr,
1075 useTime ? numPoints : 0
1085 if ( numPoints == 1 )
1091 errorOccurredDuringFallbackOperation = std::isinf( x[0] ) || std::isinf( y[0] ) || std::isinf( z[0] );
1094 if ( !errorOccurredDuringFallbackOperation )
1096 mFallbackOperationOccurred =
true;
1099 if ( !mBallparkTransformsAreAppropriate && !mDisableFallbackHandler && sFallbackOperationOccurredHandler )
1101 sFallbackOperationOccurredHandler( d->mSourceCRS, d->mDestCRS, d->mProjCoordinateOperation );
1103 const QString warning = u
"A fallback coordinate operation was used between %1 and %2"_s.arg( d->mSourceCRS.authid(),
1104 d->mDestCRS.authid() );
1105 qWarning(
"%s", warning.toLatin1().constData() );
1111 for (
const int &pos : zNanPositions )
1113 z[pos] = std::numeric_limits<double>::quiet_NaN();
1116 if ( projResult != 0 || errorOccurredDuringFallbackOperation )
1121 const QChar delim = numPoints > 1 ?
'\n' :
' ';
1122 for (
int i = 0; i < numPoints; ++i )
1124 points += u
"(%1, %2)"_s.arg( xprev[i], 0,
'f' ).arg( yprev[i], 0,
'f' ) + delim;
1130 const QString projError = !errorOccurredDuringFallbackOperation ? QString::fromUtf8( proj_context_errno_string( projContext, projResult ) ) : QObject::tr(
"Fallback transform failed" );
1132 const QString msg = QObject::tr(
"%1 (%2 to %3) of%4%5Error: %6" )
1143 if ( msg != mLastError )
1145 QgsDebugError(
"Projection failed emitting invalid transform signal: " + msg );
1153#ifdef COORDINATE_TRANSFORM_VERBOSE
1154 QgsDebugMsgLevel( u
"[[[[[[ Projected %1, %2 to %3, %4 ]]]]]]"_s.arg( xorg, 0,
'g', 15 ).arg( yorg, 0,
'g', 15 ).arg( *x, 0,
'g', 15 ).arg( *y, 0,
'g', 15 ), 2 );
1165 return !d->mIsValid || d->mShortCircuit;
1170 return d->mIsValid && d->mHasVerticalComponent;
1175 return d->mProjCoordinateOperation;
1180 ProjData projData = d->threadLocalProjData();
1187 d->mProjCoordinateOperation = operation;
1188 d->mShouldReverseCoordinateOperation =
false;
1194 d->mAllowFallbackTransforms = allowed;
1199 return d->mAllowFallbackTransforms;
1204 mBallparkTransformsAreAppropriate = appropriate;
1209 mDisableFallbackHandler = disabled;
1214 return mFallbackOperationOccurred;
1221 proj = QApplication::applicationDirPath() +
"/share/proj/" + QString( name );
1225 return proj.toUtf8();
1236 if ( sourceKey.isEmpty() || destKey.isEmpty() )
1240 if ( sDisableCache )
1243 const QList< QgsCoordinateTransform > values = sTransforms.values( qMakePair( sourceKey, destKey ) );
1244 for (
auto valIt = values.constBegin(); valIt != values.constEnd(); ++valIt )
1246 if ( ( *valIt ).coordinateOperation() == coordinateOperationProj
1247 && ( *valIt ).allowFallbackTransforms() == allowFallback
1252 const QgsCoordinateTransformContext
context = mContext;
1254 const bool hasContext = mHasContext;
1261 mHasContext = hasContext;
1270void QgsCoordinateTransform::addToCache()
1272 if ( !d->mSourceCRS.isValid() || !d->mDestCRS.isValid() )
1278 if ( sourceKey.isEmpty() || destKey.isEmpty() )
1282 if ( sDisableCache )
1285 sTransforms.insert( qMakePair( sourceKey, destKey ), *
this );
1291 return d->mSourceDatumTransform;
1299 d->mSourceDatumTransform = dt;
1306 return d->mDestinationDatumTransform;
1314 d->mDestinationDatumTransform = dt;
1321 if ( sDisableCache )
1326 sDisableCache =
true;
1329 sTransforms.clear();
1332void QgsCoordinateTransform::removeFromCacheObjectsBelongingToCurrentThread(
void *pj_context )
1338 if ( sDisableCache )
1343 if ( sDisableCache )
1346 for (
auto it = sTransforms.begin(); it != sTransforms.end(); )
1348 auto &v = it.value();
1349 if ( v.d->removeObjectsBelongingToCurrentThread( pj_context ) )
1350 it = sTransforms.erase( it );
1360 const double distSourceUnits = std::sqrt( source1.
sqrDist( source2 ) );
1363 const double distDestUnits = std::sqrt( dest1.
sqrDist( dest2 ) );
1364 return distDestUnits / distSourceUnits;
1371 QgsCoordinateTransformPrivate::setCustomMissingRequiredGridHandler( handler );
1378 QgsCoordinateTransformPrivate::setCustomMissingPreferredGridHandler( handler );
1383 QgsCoordinateTransformPrivate::setCustomCoordinateOperationCreationErrorHandler( handler );
1390 QgsCoordinateTransformPrivate::setCustomMissingGridUsedByContextHandler( handler );
1395 sFallbackOperationOccurredHandler = handler;
1400 QgsCoordinateTransformPrivate::setDynamicCrsToDynamicCrsWarningHandler( handler );
@ Geocentric
Geocentric CRS.
QFlags< CoordinateTransformationFlag > CoordinateTransformationFlags
Coordinate transformation flags.
@ Preferred
Preferred format, matching the most recent WKT ISO standard. Currently an alias to WKT2_2019,...
@ BallparkTransformsAreAppropriate
Indicates that approximate "ballpark" results are appropriate for this coordinate transform....
@ IgnoreImpossibleTransformations
Indicates that impossible transformations (such as those which attempt to transform between two diffe...
TransformDirection
Indicates the direction (forward or inverse) of a transform.
@ Forward
Forward transform (from source to destination).
@ Reverse
Reverse/inverse transform (from destination to source).
Represents a coordinate reference system (CRS).
bool isValid() const
Returns whether this CRS is correctly initialized and usable.
QString toWkt(Qgis::CrsWktVariant variant=Qgis::CrsWktVariant::Wkt1Gdal, bool multiline=false, int indentationWidth=4) const
Returns a WKT representation of this CRS.
QString celestialBodyName() const
Attempts to retrieve the name of the celestial body associated with the CRS (e.g.
double coordinateEpoch() const
Returns the coordinate epoch, as a decimal year.
Contains information about the context in which a coordinate transform is executed.
Custom exception class for Coordinate Reference System related exceptions.
static void logMessage(const QString &message, const QString &tag=QString(), Qgis::MessageLevel level=Qgis::MessageLevel::Warning, bool notifyUser=true, const char *file=__builtin_FILE(), const char *function=__builtin_FUNCTION(), int line=__builtin_LINE(), Qgis::StringFormat format=Qgis::StringFormat::PlainText)
Adds a message to the log instance (and creates it if necessary).
double sqrDist(double x, double y) const
Returns the squared distance between this point a specified x, y coordinate.
static PJ_CONTEXT * get()
Returns a thread local instance of a proj context, safe for use in the current thread.
static proj_pj_unique_ptr crsToHorizontalCrs(const PJ *crs)
Given a PROJ crs (which may be a compound or bound crs, or some other type), extract the horizontal c...
static bool hasVerticalAxis(const PJ *crs)
Returns true if a PROJ crs has a vertical axis.
std::unique_ptr< PJ, ProjPJDeleter > proj_pj_unique_ptr
Scoped Proj PJ object.
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
QgsCoordinateTransformContext transformContext
A convenience class that simplifies locking and unlocking QReadWriteLocks.
A rectangle specified with double values.
Q_INVOKABLE QString toString(int precision=16) const
Returns a string representation of form xmin,ymin : xmax,ymax Coordinates will be rounded to the spec...
void setXMinimum(double x)
Set the minimum x value.
void setXMaximum(double x)
Set the maximum x value.
void combineExtentWith(const QgsRectangle &rect)
Expands the rectangle so that it covers both the original rectangle and the given rectangle.
void setNull()
Mark a rectangle as being null (holding no spatial information).
Scoped object for temporary suppression of PROJ logging output.
A 3D vector (similar to QVector3D) with the difference that it uses double precision instead of singl...
double y() const
Returns Y coordinate.
double z() const
Returns Z coordinate.
double x() const
Returns X coordinate.
#define Q_NOWARN_DEPRECATED_POP
#define Q_NOWARN_DEPRECATED_PUSH
bool qgsNanCompatibleEquals(double a, double b)
Compare two doubles, treating nan values as equal.
#define QgsDebugMsgLevel(str, level)
#define QgsDebugError(str)