26 #include <proj_experimental.h>
30 #include <QStringList>
45 const QString &error )> QgsCoordinateTransformPrivate::sCoordinateOperationCreationErrorHandler =
nullptr;
55 QgsCoordinateTransformPrivate::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 )
81 QgsCoordinateTransformPrivate::QgsCoordinateTransformPrivate(
const QgsCoordinateTransformPrivate &other )
82 : QSharedData( other )
83 , mAvailableOpCount( other.mAvailableOpCount )
84 , mIsValid( other.mIsValid )
85 , mShortCircuit( other.mShortCircuit )
86 , mSourceCRS( other.mSourceCRS )
87 , mDestCRS( other.mDestCRS )
88 , mSourceDatumTransform( other.mSourceDatumTransform )
89 , mDestinationDatumTransform( other.mDestinationDatumTransform )
90 , mProjCoordinateOperation( other.mProjCoordinateOperation )
91 , mShouldReverseCoordinateOperation( other.mShouldReverseCoordinateOperation )
92 , mAllowFallbackTransforms( other.mAllowFallbackTransforms )
93 , mSourceIsDynamic( other.mSourceIsDynamic )
94 , mDestIsDynamic( other.mDestIsDynamic )
95 , mSourceCoordinateEpoch( other.mSourceCoordinateEpoch )
96 , mDestCoordinateEpoch( other.mDestCoordinateEpoch )
97 , mDefaultTime( other.mDefaultTime )
98 , mIsReversed( other.mIsReversed )
101 , mProjFallbackProjections()
107 QgsCoordinateTransformPrivate::~QgsCoordinateTransformPrivate()
114 bool QgsCoordinateTransformPrivate::checkValidity()
116 if ( !mSourceCRS.isValid() || !mDestCRS.isValid() )
124 void QgsCoordinateTransformPrivate::invalidate()
126 mShortCircuit =
true;
128 mAvailableOpCount = -1;
131 bool QgsCoordinateTransformPrivate::initialize()
134 if ( !mSourceCRS.isValid() )
142 if ( !mDestCRS.isValid() )
146 mDestCRS = mSourceCRS;
153 if ( mSourceCRS == mDestCRS )
157 mShortCircuit =
true;
161 mSourceIsDynamic = mSourceCRS.isDynamic();
162 mSourceCoordinateEpoch = mSourceCRS.coordinateEpoch();
163 mDestIsDynamic = mDestCRS.isDynamic();
164 mDestCoordinateEpoch = mDestCRS.coordinateEpoch();
171 mDefaultTime = ( mSourceIsDynamic && !std::isnan( mSourceCoordinateEpoch ) && !mDestIsDynamic )
172 ? mSourceCoordinateEpoch
173 : ( mDestIsDynamic && !std::isnan( mDestCoordinateEpoch ) && !mSourceIsDynamic )
174 ? mDestCoordinateEpoch : std::numeric_limits< double >::quiet_NaN();
176 if ( mSourceIsDynamic && mDestIsDynamic && !
qgsNanCompatibleEquals( mSourceCoordinateEpoch, mDestCoordinateEpoch ) )
179 if ( sDynamicCrsToDynamicCrsWarningHandler )
181 sDynamicCrsToDynamicCrsWarningHandler( mSourceCRS, mDestCRS );
189 ProjData res = threadLocalProjData();
191 #ifdef COORDINATE_TRANSFORM_VERBOSE
192 QgsDebugMsg(
"From proj : " + mSourceCRS.toProj() );
199 #ifdef COORDINATE_TRANSFORM_VERBOSE
202 QgsDebugMsg( QStringLiteral(
"------------------------------------------------------------" ) );
203 QgsDebugMsg( QStringLiteral(
"The OGR Coordinate transformation for this layer was set to" ) );
204 QgsLogger::debug<QgsCoordinateReferenceSystem>(
"Input", mSourceCRS, __FILE__, __FUNCTION__, __LINE__ );
205 QgsLogger::debug<QgsCoordinateReferenceSystem>(
"Output", mDestCRS, __FILE__, __FUNCTION__, __LINE__ );
206 QgsDebugMsg( QStringLiteral(
"------------------------------------------------------------" ) );
210 QgsDebugMsg( QStringLiteral(
"------------------------------------------------------------" ) );
211 QgsDebugMsg( QStringLiteral(
"The OGR Coordinate transformation FAILED TO INITIALIZE!" ) );
212 QgsDebugMsg( QStringLiteral(
"------------------------------------------------------------" ) );
217 QgsDebugMsg( QStringLiteral(
"Coordinate transformation failed to initialize!" ) );
222 mShortCircuit =
false;
230 if ( mSourceCRS.isValid() && mDestCRS.isValid() )
238 mProjCoordinateOperation.clear();
239 mShouldReverseCoordinateOperation =
false;
240 mAllowFallbackTransforms =
false;
244 static void proj_collecting_logger(
void *user_data,
int ,
const char *message )
246 QStringList *dest =
reinterpret_cast< QStringList *
>( user_data );
247 dest->append( QString( message ) );
250 static void proj_logger(
void *,
int level,
const char *message )
255 if ( level == PJ_LOG_ERROR )
259 else if ( level == PJ_LOG_DEBUG )
265 ProjData QgsCoordinateTransformPrivate::threadLocalProjData()
270 const QMap < uintptr_t, ProjData >::const_iterator it = mProjProjections.constFind(
reinterpret_cast< uintptr_t
>( context ) );
272 if ( it != mProjProjections.constEnd() )
274 ProjData res = it.value();
282 QStringList projErrors;
283 proj_log_func( context, &projErrors, proj_collecting_logger );
288 if ( !mProjCoordinateOperation.isEmpty() )
290 transform.reset( proj_create( context, mProjCoordinateOperation.toUtf8().constData() ) );
299 #
if PROJ_VERSION_MAJOR >= 7
301 proj_context_is_network_enabled( context ) &&
302 !proj_coordoperation_is_instantiable( context, transform.get() ) )
306 if ( sMissingGridUsedByContextHandler )
309 desired.
proj = mProjCoordinateOperation;
312 sMissingGridUsedByContextHandler( mSourceCRS, mDestCRS, desired );
316 const QString err = QObject::tr(
"Could not use operation specified in project between %1 and %2. (Wanted to use: %3)." ).arg( mSourceCRS.authid(),
318 mProjCoordinateOperation );
326 mIsReversed = mShouldReverseCoordinateOperation;
330 QString nonAvailableError;
333 if ( !mSourceCRS.projObject() || ! mDestCRS.projObject() )
335 proj_log_func( context,
nullptr,
nullptr );
339 PJ_OPERATION_FACTORY_CONTEXT *operationContext = proj_create_operation_factory_context( context,
nullptr );
342 proj_operation_factory_context_set_grid_availability_use( context, operationContext, PROJ_GRID_AVAILABILITY_IGNORED );
345 proj_operation_factory_context_set_spatial_criterion( context, operationContext, PROJ_SPATIAL_CRITERION_PARTIAL_INTERSECTION );
347 if ( PJ_OBJ_LIST *ops = proj_create_operations( context, mSourceCRS.projObject(), mDestCRS.projObject(), operationContext ) )
349 mAvailableOpCount = proj_list_get_count( ops );
350 if ( mAvailableOpCount < 1 )
353 const int errNo = proj_context_errno( context );
354 if ( errNo && errNo != -61 )
356 nonAvailableError = QString( proj_errno_string( errNo ) );
360 nonAvailableError = QObject::tr(
"No coordinate operations are available between these two reference systems" );
363 else if ( mAvailableOpCount == 1 )
366 transform.reset( proj_list_get( context, ops, 0 ) );
369 if ( !proj_coordoperation_is_instantiable( context, transform.get() ) )
372 for (
int j = 0; j < proj_coordoperation_get_grid_used_count( context, transform.get() ); ++j )
374 const char *shortName =
nullptr;
375 const char *fullName =
nullptr;
376 const char *packageName =
nullptr;
377 const char *url =
nullptr;
378 int directDownload = 0;
381 proj_coordoperation_get_grid_used( context, transform.get(), j, &shortName, &fullName, &packageName, &url, &directDownload, &openLicense, &isAvailable );
385 if ( sMissingRequiredGridHandler )
388 gridDetails.
shortName = QString( shortName );
389 gridDetails.
fullName = QString( fullName );
391 gridDetails.
url = QString( url );
395 sMissingRequiredGridHandler( mSourceCRS, mDestCRS, gridDetails );
399 const QString err = QObject::tr(
"Cannot create transform between %1 and %2, missing required grid %3" ).arg( mSourceCRS.authid(),
412 transform.reset( proj_normalize_for_visualization( context, transform.get() ) );
415 const QString err = QObject::tr(
"Cannot normalize transform between %1 and %2" ).arg( mSourceCRS.authid(),
426 bool missingPreferred =
false;
427 bool stillLookingForPreferred =
true;
428 for (
int i = 0; i < mAvailableOpCount; ++ i )
430 transform.reset( proj_list_get( context, ops, i ) );
431 const bool isInstantiable = transform && proj_coordoperation_is_instantiable( context, transform.get() );
432 if ( stillLookingForPreferred && transform && !isInstantiable )
436 if ( !candidate.
proj.isEmpty() )
438 preferred = candidate;
439 missingPreferred =
true;
440 stillLookingForPreferred =
false;
443 if ( transform && isInstantiable )
451 if ( transform && missingPreferred )
455 if ( sMissingPreferredGridHandler )
457 sMissingPreferredGridHandler( mSourceCRS, mDestCRS, preferred, available );
461 const QString err = QObject::tr(
"Using non-preferred coordinate operation between %1 and %2. Using %3, preferred %4." ).arg( mSourceCRS.authid(),
471 transform.reset( proj_normalize_for_visualization( context, transform.get() ) );
474 const QString err = QObject::tr(
"Cannot normalize transform between %1 and %2" ).arg( mSourceCRS.authid(),
479 proj_list_destroy( ops );
481 proj_operation_factory_context_destroy( operationContext );
484 if ( !transform && nonAvailableError.isEmpty() )
486 const int errNo = proj_context_errno( context );
487 if ( errNo && errNo != -61 )
489 nonAvailableError = QString( proj_errno_string( errNo ) );
491 else if ( !projErrors.empty() )
493 nonAvailableError = projErrors.constLast();
496 if ( nonAvailableError.isEmpty() )
498 nonAvailableError = QObject::tr(
"No coordinate operations are available between these two reference systems" );
503 nonAvailableError = nonAvailableError.remove( QStringLiteral(
"internal_proj_create_operations: " ) );
507 if ( !nonAvailableError.isEmpty() )
509 if ( sCoordinateOperationCreationErrorHandler )
511 sCoordinateOperationCreationErrorHandler( mSourceCRS, mDestCRS, nonAvailableError );
515 const QString err = QObject::tr(
"Cannot create transform between %1 and %2: %3" ).arg( mSourceCRS.authid(),
523 proj_log_func( context,
nullptr, proj_logger );
531 ProjData res = transform.release();
532 mProjProjections.insert(
reinterpret_cast< uintptr_t
>( context ), res );
536 ProjData QgsCoordinateTransformPrivate::threadLocalFallbackProjData()
541 const QMap < uintptr_t, ProjData >::const_iterator it = mProjFallbackProjections.constFind(
reinterpret_cast< uintptr_t
>( context ) );
543 if ( it != mProjFallbackProjections.constEnd() )
545 ProjData res = it.value();
554 transform.reset( proj_normalize_for_visualization(
QgsProjContext::get(), transform.get() ) );
556 ProjData res = transform.release();
557 mProjFallbackProjections.insert(
reinterpret_cast< uintptr_t
>( context ), res );
563 sMissingRequiredGridHandler = handler;
568 sMissingPreferredGridHandler = handler;
573 sCoordinateOperationCreationErrorHandler = handler;
578 sMissingGridUsedByContextHandler = handler;
583 sDynamicCrsToDynamicCrsWarningHandler = handler;
586 void QgsCoordinateTransformPrivate::freeProj()
589 if ( mProjProjections.isEmpty() && mProjFallbackProjections.isEmpty() )
591 QMap < uintptr_t, ProjData >::const_iterator it = mProjProjections.constBegin();
598 PJ_CONTEXT *tmpContext = proj_context_create();
599 for ( ; it != mProjProjections.constEnd(); ++it )
601 proj_assign_context( it.value(), tmpContext );
602 proj_destroy( it.value() );
605 it = mProjFallbackProjections.constBegin();
606 for ( ; it != mProjFallbackProjections.constEnd(); ++it )
608 proj_assign_context( it.value(), tmpContext );
609 proj_destroy( it.value() );
612 proj_context_destroy( tmpContext );
613 mProjProjections.clear();
614 mProjFallbackProjections.clear();
617 bool QgsCoordinateTransformPrivate::removeObjectsBelongingToCurrentThread(
void *pj_context )
621 QMap < uintptr_t, ProjData >::iterator it = mProjProjections.find(
reinterpret_cast< uintptr_t
>( pj_context ) );
622 if ( it != mProjProjections.end() )
624 proj_destroy( it.value() );
625 mProjProjections.erase( it );
628 it = mProjFallbackProjections.find(
reinterpret_cast< uintptr_t
>( pj_context ) );
629 if ( it != mProjFallbackProjections.end() )
631 proj_destroy( it.value() );
632 mProjFallbackProjections.erase( it );
635 return mProjProjections.isEmpty();
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...
#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)