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 );
337#if PROJ_VERSION_MAJOR>=8
338 nonAvailableError = QString( proj_context_errno_string( context, errNo ) );
340 nonAvailableError = QString( proj_errno_string( errNo ) );
346 nonAvailableError = QObject::tr(
"No coordinate operations are available between these two reference systems" );
349 else if ( mAvailableOpCount == 1 )
352 transform.reset( proj_list_get( context, ops, 0 ) );
355 if ( !proj_coordoperation_is_instantiable( context, transform.get() ) )
358 for (
int j = 0; j < proj_coordoperation_get_grid_used_count( context, transform.get() ); ++j )
360 const char *shortName =
nullptr;
361 const char *fullName =
nullptr;
362 const char *packageName =
nullptr;
363 const char *url =
nullptr;
364 int directDownload = 0;
367 proj_coordoperation_get_grid_used( context, transform.get(), j, &shortName, &fullName, &packageName, &url, &directDownload, &openLicense, &isAvailable );
371 if ( sMissingRequiredGridHandler )
374 gridDetails.
shortName = QString( shortName );
375 gridDetails.
fullName = QString( fullName );
377 gridDetails.
url = QString( url );
381 sMissingRequiredGridHandler( mSourceCRS, mDestCRS, gridDetails );
385 const QString err = QObject::tr(
"Cannot create transform between %1 and %2, missing required grid %3" ).arg( mSourceCRS.authid(),
398 transform.reset( proj_normalize_for_visualization( context, transform.get() ) );
401 const QString err = QObject::tr(
"Cannot normalize transform between %1 and %2" ).arg( mSourceCRS.authid(),
412 bool missingPreferred =
false;
413 bool stillLookingForPreferred =
true;
414 for (
int i = 0; i < mAvailableOpCount; ++ i )
416 transform.reset( proj_list_get( context, ops, i ) );
417 const bool isInstantiable = transform && proj_coordoperation_is_instantiable( context, transform.get() );
418 if ( stillLookingForPreferred && transform && !isInstantiable )
422 if ( !candidate.
proj.isEmpty() )
424 preferred = candidate;
425 missingPreferred =
true;
426 stillLookingForPreferred =
false;
429 if ( transform && isInstantiable )
437 if ( transform && missingPreferred )
441 if ( sMissingPreferredGridHandler )
443 sMissingPreferredGridHandler( mSourceCRS, mDestCRS, preferred, available );
447 const QString err = QObject::tr(
"Using non-preferred coordinate operation between %1 and %2. Using %3, preferred %4." ).arg( mSourceCRS.authid(),
457 transform.reset( proj_normalize_for_visualization( context, transform.get() ) );
460 const QString err = QObject::tr(
"Cannot normalize transform between %1 and %2" ).arg( mSourceCRS.authid(),
465 proj_list_destroy( ops );
467 proj_operation_factory_context_destroy( operationContext );
470 if ( !transform && nonAvailableError.isEmpty() )
472 const int errNo = proj_context_errno( context );
473 const QStringList projErrors = errorLogger.
errors();
476#if PROJ_VERSION_MAJOR>=8
477 nonAvailableError = QString( proj_context_errno_string( context, errNo ) );
479 nonAvailableError = QString( proj_errno_string( errNo ) );
482 else if ( !projErrors.empty() )
484 nonAvailableError = projErrors.constLast();
487 if ( nonAvailableError.isEmpty() )
489 nonAvailableError = QObject::tr(
"No coordinate operations are available between these two reference systems" );
494 nonAvailableError = nonAvailableError.remove( QStringLiteral(
"internal_proj_create_operations: " ) );
498 if ( !nonAvailableError.isEmpty() )
500 if ( sCoordinateOperationCreationErrorHandler )
502 sCoordinateOperationCreationErrorHandler( mSourceCRS, mDestCRS, nonAvailableError );
506 const QString err = QObject::tr(
"Cannot create transform between %1 and %2: %3" ).arg( mSourceCRS.authid(),
519 ProjData res = transform.release();
520 mProjProjections.insert(
reinterpret_cast< uintptr_t
>( context ), res );
524ProjData QgsCoordinateTransformPrivate::threadLocalFallbackProjData()
529 const QMap < uintptr_t, ProjData >::const_iterator it = mProjFallbackProjections.constFind(
reinterpret_cast< uintptr_t
>( context ) );
531 if ( it != mProjFallbackProjections.constEnd() )
533 ProjData res = it.value();
542 transform.reset( proj_normalize_for_visualization(
QgsProjContext::get(), transform.get() ) );
544 ProjData res = transform.release();
545 mProjFallbackProjections.insert(
reinterpret_cast< uintptr_t
>( context ), res );
551 sMissingRequiredGridHandler = handler;
556 sMissingPreferredGridHandler = handler;
561 sCoordinateOperationCreationErrorHandler = handler;
566 sMissingGridUsedByContextHandler = handler;
571 sDynamicCrsToDynamicCrsWarningHandler = handler;
574void QgsCoordinateTransformPrivate::freeProj()
577 if ( mProjProjections.isEmpty() && mProjFallbackProjections.isEmpty() )
579 QMap < uintptr_t, ProjData >::const_iterator it = mProjProjections.constBegin();
586 PJ_CONTEXT *tmpContext = proj_context_create();
587 for ( ; it != mProjProjections.constEnd(); ++it )
589 proj_assign_context( it.value(), tmpContext );
590 proj_destroy( it.value() );
593 it = mProjFallbackProjections.constBegin();
594 for ( ; it != mProjFallbackProjections.constEnd(); ++it )
596 proj_assign_context( it.value(), tmpContext );
597 proj_destroy( it.value() );
600 proj_context_destroy( tmpContext );
601 mProjProjections.clear();
602 mProjFallbackProjections.clear();
605bool QgsCoordinateTransformPrivate::removeObjectsBelongingToCurrentThread(
void *pj_context )
609 QMap < uintptr_t, ProjData >::iterator it = mProjProjections.find(
reinterpret_cast< uintptr_t
>( pj_context ) );
610 if ( it != mProjProjections.end() )
612 proj_destroy( it.value() );
613 mProjProjections.erase( it );
616 it = mProjFallbackProjections.find(
reinterpret_cast< uintptr_t
>( pj_context ) );
617 if ( it != mProjFallbackProjections.end() )
619 proj_destroy( it.value() );
620 mProjFallbackProjections.erase( it );
623 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)