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 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() ) );
291 if ( !transform || !proj_coordoperation_is_instantiable( context, transform.get() ) )
293 if ( sMissingGridUsedByContextHandler )
296 desired.
proj = mProjCoordinateOperation;
299 sMissingGridUsedByContextHandler( mSourceCRS, mDestCRS, desired );
303 const QString err = QObject::tr(
"Could not use operation specified in project between %1 and %2. (Wanted to use: %3)." ).arg( mSourceCRS.authid(),
305 mProjCoordinateOperation );
313 mIsReversed = mShouldReverseCoordinateOperation;
317 QString nonAvailableError;
320 if ( !mSourceCRS.projObject() || ! mDestCRS.projObject() )
322 proj_log_func( context,
nullptr,
nullptr );
326 PJ_OPERATION_FACTORY_CONTEXT *operationContext = proj_create_operation_factory_context( context,
nullptr );
329 proj_operation_factory_context_set_grid_availability_use( context, operationContext, PROJ_GRID_AVAILABILITY_IGNORED );
332 proj_operation_factory_context_set_spatial_criterion( context, operationContext, PROJ_SPATIAL_CRITERION_PARTIAL_INTERSECTION );
334 if ( PJ_OBJ_LIST *ops = proj_create_operations( context, mSourceCRS.projObject(), mDestCRS.projObject(), operationContext ) )
336 mAvailableOpCount = proj_list_get_count( ops );
337 if ( mAvailableOpCount < 1 )
340 int errNo = proj_context_errno( context );
341 if ( errNo && errNo != -61 )
343 nonAvailableError = QString( proj_errno_string( errNo ) );
347 nonAvailableError = QObject::tr(
"No coordinate operations are available between these two reference systems" );
350 else if ( mAvailableOpCount == 1 )
353 transform.reset( proj_list_get( context, ops, 0 ) );
356 if ( !proj_coordoperation_is_instantiable( context, transform.get() ) )
359 for (
int j = 0; j < proj_coordoperation_get_grid_used_count( context, transform.get() ); ++j )
361 const char *shortName =
nullptr;
362 const char *fullName =
nullptr;
363 const char *packageName =
nullptr;
364 const char *url =
nullptr;
365 int directDownload = 0;
368 proj_coordoperation_get_grid_used( context, transform.get(), j, &shortName, &fullName, &packageName, &url, &directDownload, &openLicense, &isAvailable );
372 if ( sMissingRequiredGridHandler )
375 gridDetails.
shortName = QString( shortName );
376 gridDetails.
fullName = QString( fullName );
378 gridDetails.
url = QString( url );
382 sMissingRequiredGridHandler( mSourceCRS, mDestCRS, gridDetails );
386 const QString err = QObject::tr(
"Cannot create transform between %1 and %2, missing required grid %3" ).arg( mSourceCRS.authid(),
399 transform.reset( proj_normalize_for_visualization( context, transform.get() ) );
402 const QString err = QObject::tr(
"Cannot normalize transform between %1 and %2" ).arg( mSourceCRS.authid(),
413 bool missingPreferred =
false;
414 bool stillLookingForPreferred =
true;
415 for (
int i = 0; i < mAvailableOpCount; ++ i )
417 transform.reset( proj_list_get( context, ops, i ) );
418 const bool isInstantiable = transform && proj_coordoperation_is_instantiable( context, transform.get() );
419 if ( stillLookingForPreferred && transform && !isInstantiable )
423 if ( !candidate.
proj.isEmpty() )
425 preferred = candidate;
426 missingPreferred =
true;
427 stillLookingForPreferred =
false;
430 if ( transform && isInstantiable )
438 if ( transform && missingPreferred )
442 if ( sMissingPreferredGridHandler )
444 sMissingPreferredGridHandler( mSourceCRS, mDestCRS, preferred, available );
448 const QString err = QObject::tr(
"Using non-preferred coordinate operation between %1 and %2. Using %3, preferred %4." ).arg( mSourceCRS.authid(),
458 transform.reset( proj_normalize_for_visualization( context, transform.get() ) );
461 const QString err = QObject::tr(
"Cannot normalize transform between %1 and %2" ).arg( mSourceCRS.authid(),
466 proj_list_destroy( ops );
468 proj_operation_factory_context_destroy( operationContext );
471 if ( !transform && nonAvailableError.isEmpty() )
473 int errNo = proj_context_errno( context );
474 if ( errNo && errNo != -61 )
476 nonAvailableError = QString( proj_errno_string( errNo ) );
478 else if ( !projErrors.empty() )
480 nonAvailableError = projErrors.constLast();
483 if ( nonAvailableError.isEmpty() )
485 nonAvailableError = QObject::tr(
"No coordinate operations are available between these two reference systems" );
490 nonAvailableError = nonAvailableError.remove( QStringLiteral(
"internal_proj_create_operations: " ) );
494 if ( !nonAvailableError.isEmpty() )
496 if ( sCoordinateOperationCreationErrorHandler )
498 sCoordinateOperationCreationErrorHandler( mSourceCRS, mDestCRS, nonAvailableError );
502 const QString err = QObject::tr(
"Cannot create transform between %1 and %2: %3" ).arg( mSourceCRS.authid(),
510 proj_log_func( context,
nullptr, proj_logger );
518 ProjData res = transform.release();
519 mProjProjections.insert(
reinterpret_cast< uintptr_t
>( context ), res );
523 ProjData QgsCoordinateTransformPrivate::threadLocalFallbackProjData()
528 QMap < uintptr_t, ProjData >::const_iterator it = mProjFallbackProjections.constFind(
reinterpret_cast< uintptr_t
>( context ) );
530 if ( it != mProjFallbackProjections.constEnd() )
532 ProjData res = it.value();
541 transform.reset( proj_normalize_for_visualization(
QgsProjContext::get(), transform.get() ) );
543 ProjData res = transform.release();
544 mProjFallbackProjections.insert(
reinterpret_cast< uintptr_t
>( context ), res );
550 sMissingRequiredGridHandler = handler;
555 sMissingPreferredGridHandler = handler;
560 sCoordinateOperationCreationErrorHandler = handler;
565 sMissingGridUsedByContextHandler = handler;
570 sDynamicCrsToDynamicCrsWarningHandler = handler;
573 void QgsCoordinateTransformPrivate::freeProj()
576 if ( mProjProjections.isEmpty() && mProjFallbackProjections.isEmpty() )
578 QMap < uintptr_t, ProjData >::const_iterator it = mProjProjections.constBegin();
585 PJ_CONTEXT *tmpContext = proj_context_create();
586 for ( ; it != mProjProjections.constEnd(); ++it )
588 proj_assign_context( it.value(), tmpContext );
589 proj_destroy( it.value() );
592 it = mProjFallbackProjections.constBegin();
593 for ( ; it != mProjFallbackProjections.constEnd(); ++it )
595 proj_assign_context( it.value(), tmpContext );
596 proj_destroy( it.value() );
599 proj_context_destroy( tmpContext );
600 mProjProjections.clear();
601 mProjFallbackProjections.clear();
604 bool QgsCoordinateTransformPrivate::removeObjectsBelongingToCurrentThread(
void *pj_context )
608 QMap < uintptr_t, ProjData >::iterator it = mProjProjections.find(
reinterpret_cast< uintptr_t
>( pj_context ) );
609 if ( it != mProjProjections.end() )
611 proj_destroy( it.value() );
612 mProjProjections.erase( it );
615 it = mProjFallbackProjections.find(
reinterpret_cast< uintptr_t
>( pj_context ) );
616 if ( it != mProjFallbackProjections.end() )
618 proj_destroy( it.value() );
619 mProjFallbackProjections.erase( it );
622 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)