26#include <proj_experimental.h>
45 const QString &error )> QgsCoordinateTransformPrivate::sCoordinateOperationCreationErrorHandler =
nullptr;
55QgsCoordinateTransformPrivate::QgsCoordinateTransformPrivate()
64 : mSourceCRS( source )
65 , mDestCRS( destination )
67 if ( mSourceCRS != mDestCRS )
68 calculateTransforms( context );
74 : mSourceCRS( source )
75 , mDestCRS( destination )
76 , mSourceDatumTransform( sourceDatumTransform )
77 , mDestinationDatumTransform( destDatumTransform )
81QgsCoordinateTransformPrivate::QgsCoordinateTransformPrivate(
const QgsCoordinateTransformPrivate &other )
82 : QSharedData( other )
83 , mAvailableOpCount( other.mAvailableOpCount )
84 , mIsValid( other.mIsValid )
85 , mShortCircuit( other.mShortCircuit )
86 , mGeographicToWebMercator( other.mGeographicToWebMercator )
87 , mHasVerticalComponent( other.mHasVerticalComponent )
88 , mSourceCRS( other.mSourceCRS )
89 , mDestCRS( other.mDestCRS )
90 , mSourceDatumTransform( other.mSourceDatumTransform )
91 , mDestinationDatumTransform( other.mDestinationDatumTransform )
92 , mProjCoordinateOperation( other.mProjCoordinateOperation )
93 , mShouldReverseCoordinateOperation( other.mShouldReverseCoordinateOperation )
94 , mAllowFallbackTransforms( other.mAllowFallbackTransforms )
95 , mSourceIsDynamic( other.mSourceIsDynamic )
96 , mDestIsDynamic( other.mDestIsDynamic )
97 , mSourceCoordinateEpoch( other.mSourceCoordinateEpoch )
98 , mDestCoordinateEpoch( other.mDestCoordinateEpoch )
99 , mDefaultTime( other.mDefaultTime )
100 , mIsReversed( other.mIsReversed )
103 , mProjFallbackProjections()
109QgsCoordinateTransformPrivate::~QgsCoordinateTransformPrivate()
116bool QgsCoordinateTransformPrivate::checkValidity()
118 if ( !mSourceCRS.isValid() || !mDestCRS.isValid() )
126void QgsCoordinateTransformPrivate::invalidate()
128 mShortCircuit =
true;
130 mAvailableOpCount = -1;
133bool QgsCoordinateTransformPrivate::initialize()
136 if ( !mSourceCRS.isValid() )
144 if ( !mDestCRS.isValid() )
148 mDestCRS = mSourceCRS;
155 if ( mSourceCRS == mDestCRS )
159 mShortCircuit =
true;
163 mGeographicToWebMercator =
164 mSourceCRS.isGeographic() &&
165 mDestCRS.authid() == QLatin1String(
"EPSG:3857" );
167 mHasVerticalComponent = mSourceCRS.hasVerticalAxis() && mDestCRS.hasVerticalAxis();
169 mSourceIsDynamic = mSourceCRS.isDynamic();
170 mSourceCoordinateEpoch = mSourceCRS.coordinateEpoch();
171 mDestIsDynamic = mDestCRS.isDynamic();
172 mDestCoordinateEpoch = mDestCRS.coordinateEpoch();
179 mDefaultTime = ( mSourceIsDynamic && !std::isnan( mSourceCoordinateEpoch ) && !mDestIsDynamic )
180 ? mSourceCoordinateEpoch
181 : ( mDestIsDynamic && !std::isnan( mDestCoordinateEpoch ) && !mSourceIsDynamic )
182 ? mDestCoordinateEpoch : std::numeric_limits< double >::quiet_NaN();
184 if ( mSourceIsDynamic && mDestIsDynamic && !
qgsNanCompatibleEquals( mSourceCoordinateEpoch, mDestCoordinateEpoch ) )
187 if ( sDynamicCrsToDynamicCrsWarningHandler )
189 sDynamicCrsToDynamicCrsWarningHandler( mSourceCRS, mDestCRS );
197 ProjData res = threadLocalProjData();
199#ifdef COORDINATE_TRANSFORM_VERBOSE
207#ifdef COORDINATE_TRANSFORM_VERBOSE
210 QgsDebugMsgLevel( QStringLiteral(
"------------------------------------------------------------" ), 2 );
211 QgsDebugMsgLevel( QStringLiteral(
"The OGR Coordinate transformation for this layer was set to" ), 2 );
212 QgsLogger::debug<QgsCoordinateReferenceSystem>(
"Input", mSourceCRS, __FILE__, __FUNCTION__, __LINE__ );
213 QgsLogger::debug<QgsCoordinateReferenceSystem>(
"Output", mDestCRS, __FILE__, __FUNCTION__, __LINE__ );
214 QgsDebugMsgLevel( QStringLiteral(
"------------------------------------------------------------" ), 2 );
218 QgsDebugError( QStringLiteral(
"The OGR Coordinate transformation FAILED TO INITIALIZE!" ) );
223 QgsDebugError( QStringLiteral(
"Coordinate transformation failed to initialize!" ) );
228 mShortCircuit =
false;
236 if ( mSourceCRS.isValid() && mDestCRS.isValid() )
244 mProjCoordinateOperation.clear();
245 mShouldReverseCoordinateOperation =
false;
246 mAllowFallbackTransforms =
false;
250ProjData QgsCoordinateTransformPrivate::threadLocalProjData()
255 const QMap < uintptr_t, ProjData >::const_iterator it = mProjProjections.constFind(
reinterpret_cast< uintptr_t
>( context ) );
257 if ( it != mProjProjections.constEnd() )
259 ProjData res = it.value();
272 if ( !mProjCoordinateOperation.isEmpty() )
274 transform.reset( proj_create( context, mProjCoordinateOperation.toUtf8().constData() ) );
284 proj_context_is_network_enabled( context ) &&
285 !proj_coordoperation_is_instantiable( context, transform.get() ) )
288 if ( sMissingGridUsedByContextHandler )
291 desired.
proj = mProjCoordinateOperation;
294 sMissingGridUsedByContextHandler( mSourceCRS, mDestCRS, desired );
298 const QString err = QObject::tr(
"Could not use operation specified in project between %1 and %2. (Wanted to use: %3)." ).arg( mSourceCRS.authid(),
300 mProjCoordinateOperation );
308 mIsReversed = mShouldReverseCoordinateOperation;
312 QString nonAvailableError;
315 if ( !mSourceCRS.projObject() || ! mDestCRS.projObject() )
320 PJ_OPERATION_FACTORY_CONTEXT *operationContext = proj_create_operation_factory_context( context,
nullptr );
323 proj_operation_factory_context_set_grid_availability_use( context, operationContext, PROJ_GRID_AVAILABILITY_IGNORED );
326 proj_operation_factory_context_set_spatial_criterion( context, operationContext, PROJ_SPATIAL_CRITERION_PARTIAL_INTERSECTION );
328 if ( PJ_OBJ_LIST *ops = proj_create_operations( context, mSourceCRS.projObject(), mDestCRS.projObject(), operationContext ) )
330 mAvailableOpCount = proj_list_get_count( ops );
331 if ( mAvailableOpCount < 1 )
334 const int errNo = proj_context_errno( context );
335 if ( errNo && errNo != -61 )
337 nonAvailableError = QString( proj_errno_string( errNo ) );
341 nonAvailableError = QObject::tr(
"No coordinate operations are available between these two reference systems" );
344 else if ( mAvailableOpCount == 1 )
347 transform.reset( proj_list_get( context, ops, 0 ) );
350 if ( !proj_coordoperation_is_instantiable( context, transform.get() ) )
353 for (
int j = 0; j < proj_coordoperation_get_grid_used_count( context, transform.get() ); ++j )
355 const char *shortName =
nullptr;
356 const char *fullName =
nullptr;
357 const char *packageName =
nullptr;
358 const char *url =
nullptr;
359 int directDownload = 0;
362 proj_coordoperation_get_grid_used( context, transform.get(), j, &shortName, &fullName, &packageName, &url, &directDownload, &openLicense, &isAvailable );
366 if ( sMissingRequiredGridHandler )
369 gridDetails.
shortName = QString( shortName );
370 gridDetails.
fullName = QString( fullName );
372 gridDetails.
url = QString( url );
376 sMissingRequiredGridHandler( mSourceCRS, mDestCRS, gridDetails );
380 const QString err = QObject::tr(
"Cannot create transform between %1 and %2, missing required grid %3" ).arg( mSourceCRS.authid(),
393 transform.reset( proj_normalize_for_visualization( context, transform.get() ) );
396 const QString err = QObject::tr(
"Cannot normalize transform between %1 and %2" ).arg( mSourceCRS.authid(),
407 bool missingPreferred =
false;
408 bool stillLookingForPreferred =
true;
409 for (
int i = 0; i < mAvailableOpCount; ++ i )
411 transform.reset( proj_list_get( context, ops, i ) );
412 const bool isInstantiable = transform && proj_coordoperation_is_instantiable( context, transform.get() );
413 if ( stillLookingForPreferred && transform && !isInstantiable )
417 if ( !candidate.
proj.isEmpty() )
419 preferred = candidate;
420 missingPreferred =
true;
421 stillLookingForPreferred =
false;
424 if ( transform && isInstantiable )
432 if ( transform && missingPreferred )
436 if ( sMissingPreferredGridHandler )
438 sMissingPreferredGridHandler( mSourceCRS, mDestCRS, preferred, available );
442 const QString err = QObject::tr(
"Using non-preferred coordinate operation between %1 and %2. Using %3, preferred %4." ).arg( mSourceCRS.authid(),
452 transform.reset( proj_normalize_for_visualization( context, transform.get() ) );
455 const QString err = QObject::tr(
"Cannot normalize transform between %1 and %2" ).arg( mSourceCRS.authid(),
460 proj_list_destroy( ops );
462 proj_operation_factory_context_destroy( operationContext );
465 if ( !transform && nonAvailableError.isEmpty() )
467 const int errNo = proj_context_errno( context );
468 const QStringList projErrors = errorLogger.
errors();
469 if ( errNo && errNo != -61 )
471 nonAvailableError = QString( proj_errno_string( errNo ) );
473 else if ( !projErrors.empty() )
475 nonAvailableError = projErrors.constLast();
478 if ( nonAvailableError.isEmpty() )
480 nonAvailableError = QObject::tr(
"No coordinate operations are available between these two reference systems" );
485 nonAvailableError = nonAvailableError.remove( QStringLiteral(
"internal_proj_create_operations: " ) );
489 if ( !nonAvailableError.isEmpty() )
491 if ( sCoordinateOperationCreationErrorHandler )
493 sCoordinateOperationCreationErrorHandler( mSourceCRS, mDestCRS, nonAvailableError );
497 const QString err = QObject::tr(
"Cannot create transform between %1 and %2: %3" ).arg( mSourceCRS.authid(),
510 ProjData res = transform.release();
511 mProjProjections.insert(
reinterpret_cast< uintptr_t
>( context ), res );
515ProjData QgsCoordinateTransformPrivate::threadLocalFallbackProjData()
520 const QMap < uintptr_t, ProjData >::const_iterator it = mProjFallbackProjections.constFind(
reinterpret_cast< uintptr_t
>( context ) );
522 if ( it != mProjFallbackProjections.constEnd() )
524 ProjData res = it.value();
533 transform.reset( proj_normalize_for_visualization(
QgsProjContext::get(), transform.get() ) );
535 ProjData res = transform.release();
536 mProjFallbackProjections.insert(
reinterpret_cast< uintptr_t
>( context ), res );
542 sMissingRequiredGridHandler = handler;
547 sMissingPreferredGridHandler = handler;
552 sCoordinateOperationCreationErrorHandler = handler;
557 sMissingGridUsedByContextHandler = handler;
562 sDynamicCrsToDynamicCrsWarningHandler = handler;
565void QgsCoordinateTransformPrivate::freeProj()
568 if ( mProjProjections.isEmpty() && mProjFallbackProjections.isEmpty() )
570 QMap < uintptr_t, ProjData >::const_iterator it = mProjProjections.constBegin();
577 PJ_CONTEXT *tmpContext = proj_context_create();
578 for ( ; it != mProjProjections.constEnd(); ++it )
580 proj_assign_context( it.value(), tmpContext );
581 proj_destroy( it.value() );
584 it = mProjFallbackProjections.constBegin();
585 for ( ; it != mProjFallbackProjections.constEnd(); ++it )
587 proj_assign_context( it.value(), tmpContext );
588 proj_destroy( it.value() );
591 proj_context_destroy( tmpContext );
592 mProjProjections.clear();
593 mProjFallbackProjections.clear();
596bool QgsCoordinateTransformPrivate::removeObjectsBelongingToCurrentThread(
void *pj_context )
600 QMap < uintptr_t, ProjData >::iterator it = mProjProjections.find(
reinterpret_cast< uintptr_t
>( pj_context ) );
601 if ( it != mProjProjections.end() )
603 proj_destroy( it.value() );
604 mProjProjections.erase( it );
607 it = mProjFallbackProjections.find(
reinterpret_cast< uintptr_t
>( pj_context ) );
608 if ( it != mProjFallbackProjections.end() )
610 proj_destroy( it.value() );
611 mProjFallbackProjections.erase( it );
614 return mProjProjections.isEmpty();
@ Critical
Critical/error message.
This class represents a coordinate reference system (CRS).
Contains information about the context in which a coordinate transform is executed.
bool allowFallbackTransform(const QgsCoordinateReferenceSystem &source, const QgsCoordinateReferenceSystem &destination) const
Returns true if approximate "ballpark" transforms may be used when transforming between a source and ...
QString calculateCoordinateOperation(const QgsCoordinateReferenceSystem &source, const QgsCoordinateReferenceSystem &destination) const
Returns the Proj coordinate operation string to use when transforming from the specified source CRS t...
bool mustReverseCoordinateOperation(const QgsCoordinateReferenceSystem &source, const QgsCoordinateReferenceSystem &destination) const
Returns true if the coordinate operation returned by calculateCoordinateOperation() for the source to...
static void logMessage(const QString &message, const QString &tag=QString(), Qgis::MessageLevel level=Qgis::MessageLevel::Warning, bool notifyUser=true)
Adds a message to the log instance (and creates it if necessary).
static PJ_CONTEXT * get()
Returns a thread local instance of a proj context, safe for use in the current thread.
static QList< QgsDatumTransform::GridDetails > gridsUsed(const QString &proj)
Returns a list of grids used by the given proj string.
std::unique_ptr< PJ, ProjPJDeleter > proj_pj_unique_ptr
Scoped Proj PJ object.
The QgsReadWriteLocker class is a convenience class that simplifies locking and unlocking QReadWriteL...
Scoped object for temporary swapping to an error-collecting PROJ log function.
QStringList errors() const
Returns the (possibly empty) list of collected errors.
#define Q_NOWARN_DEPRECATED_POP
#define Q_NOWARN_DEPRECATED_PUSH
bool qgsNanCompatibleEquals(double a, double b)
Compare two doubles, treating nan values as equal.
struct projCtx_t PJ_CONTEXT
#define QgsDebugMsgLevel(str, level)
#define QgsDebugError(str)