diff --git a/src/core/proj/qgscoordinatetransform.cpp b/src/core/proj/qgscoordinatetransform.cpp index 2304181cc5c4..cd934cb3f753 100644 --- a/src/core/proj/qgscoordinatetransform.cpp +++ b/src/core/proj/qgscoordinatetransform.cpp @@ -676,9 +676,27 @@ QgsRectangle QgsCoordinateTransform::transformBoundingBox( const QgsRectangle &r proj_errno_reset( projData ); // proj documentation recommends 21 points for densification constexpr int DENSIFY_POINTS = 21; - const int projResult = proj_trans_bounds( projContext, projData, ( direction == Qgis::TransformDirection::Forward && !d->mIsReversed ) || ( direction == Qgis::TransformDirection::Reverse && d->mIsReversed ) ? PJ_FWD : PJ_INV, - xMin, yMin, xMax, yMax, - &transXMin, &transYMin, &transXMax, &transYMax, DENSIFY_POINTS ); + int projResult = proj_trans_bounds( projContext, projData, ( direction == Qgis::TransformDirection::Forward && !d->mIsReversed ) || ( direction == Qgis::TransformDirection::Reverse && d->mIsReversed ) ? PJ_FWD : PJ_INV, + xMin, yMin, xMax, yMax, + &transXMin, &transYMin, &transXMax, &transYMax, DENSIFY_POINTS ); + + if ( ( projResult != 1 + || !std::isfinite( transXMin ) + || !std::isfinite( transXMax ) + || !std::isfinite( transYMin ) + || !std::isfinite( transYMax ) ) + && ( d->mAvailableOpCount > 1 || d->mAvailableOpCount == -1 ) // only use fallbacks if more than one operation is possible -- otherwise we've already tried it and it failed + ) + { + // fail #1 -- try with getting proj to auto-pick an appropriate coordinate operation for the points + if ( PJ *transform = d->threadLocalFallbackProjData() ) + { + projResult = proj_trans_bounds( projContext, transform, ( direction == Qgis::TransformDirection::Forward && !d->mIsReversed ) || ( direction == Qgis::TransformDirection::Reverse && d->mIsReversed ) ? PJ_FWD : PJ_INV, + xMin, yMin, xMax, yMax, + &transXMin, &transYMin, &transXMax, &transYMax, DENSIFY_POINTS ); + } + } + if ( projResult != 1 || !std::isfinite( transXMin ) || !std::isfinite( transXMax )