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 , mSourceCRS( other.mSourceCRS )
88 , mDestCRS( other.mDestCRS )
89 , mSourceDatumTransform( other.mSourceDatumTransform )
90 , mDestinationDatumTransform( other.mDestinationDatumTransform )
91 , mProjCoordinateOperation( other.mProjCoordinateOperation )
92 , mShouldReverseCoordinateOperation( other.mShouldReverseCoordinateOperation )
93 , mAllowFallbackTransforms( other.mAllowFallbackTransforms )
94 , mSourceIsDynamic( other.mSourceIsDynamic )
95 , mDestIsDynamic( other.mDestIsDynamic )
96 , mSourceCoordinateEpoch( other.mSourceCoordinateEpoch )
97 , mDestCoordinateEpoch( other.mDestCoordinateEpoch )
98 , mDefaultTime( other.mDefaultTime )
99 , mIsReversed( other.mIsReversed )
102 , mProjFallbackProjections()
108QgsCoordinateTransformPrivate::~QgsCoordinateTransformPrivate()
115bool QgsCoordinateTransformPrivate::checkValidity()
117 if ( !mSourceCRS.isValid() || !mDestCRS.isValid() )
125void QgsCoordinateTransformPrivate::invalidate()
127 mShortCircuit =
true;
129 mAvailableOpCount = -1;
132bool QgsCoordinateTransformPrivate::initialize()
135 if ( !mSourceCRS.isValid() )
143 if ( !mDestCRS.isValid() )
147 mDestCRS = mSourceCRS;
154 if ( mSourceCRS == mDestCRS )
158 mShortCircuit =
true;
162 mGeographicToWebMercator =
163 mSourceCRS.isGeographic() &&
164 mDestCRS.authid() == QLatin1String(
"EPSG:3857" );
166 mSourceIsDynamic = mSourceCRS.isDynamic();
167 mSourceCoordinateEpoch = mSourceCRS.coordinateEpoch();
168 mDestIsDynamic = mDestCRS.isDynamic();
169 mDestCoordinateEpoch = mDestCRS.coordinateEpoch();
176 mDefaultTime = ( mSourceIsDynamic && !std::isnan( mSourceCoordinateEpoch ) && !mDestIsDynamic )
177 ? mSourceCoordinateEpoch
178 : ( mDestIsDynamic && !std::isnan( mDestCoordinateEpoch ) && !mSourceIsDynamic )
179 ? mDestCoordinateEpoch : std::numeric_limits< double >::quiet_NaN();
181 if ( mSourceIsDynamic && mDestIsDynamic && !
qgsNanCompatibleEquals( mSourceCoordinateEpoch, mDestCoordinateEpoch ) )
184 if ( sDynamicCrsToDynamicCrsWarningHandler )
186 sDynamicCrsToDynamicCrsWarningHandler( mSourceCRS, mDestCRS );
194 ProjData res = threadLocalProjData();
196#ifdef COORDINATE_TRANSFORM_VERBOSE
197 QgsDebugMsg(
"From proj : " + mSourceCRS.toProj() );
204#ifdef COORDINATE_TRANSFORM_VERBOSE
207 QgsDebugMsg( QStringLiteral(
"------------------------------------------------------------" ) );
208 QgsDebugMsg( QStringLiteral(
"The OGR Coordinate transformation for this layer was set to" ) );
209 QgsLogger::debug<QgsCoordinateReferenceSystem>(
"Input", mSourceCRS, __FILE__, __FUNCTION__, __LINE__ );
210 QgsLogger::debug<QgsCoordinateReferenceSystem>(
"Output", mDestCRS, __FILE__, __FUNCTION__, __LINE__ );
211 QgsDebugMsg( QStringLiteral(
"------------------------------------------------------------" ) );
215 QgsDebugMsg( QStringLiteral(
"------------------------------------------------------------" ) );
216 QgsDebugMsg( QStringLiteral(
"The OGR Coordinate transformation FAILED TO INITIALIZE!" ) );
217 QgsDebugMsg( QStringLiteral(
"------------------------------------------------------------" ) );
222 QgsDebugMsg( QStringLiteral(
"Coordinate transformation failed to initialize!" ) );
227 mShortCircuit =
false;
235 if ( mSourceCRS.isValid() && mDestCRS.isValid() )
243 mProjCoordinateOperation.clear();
244 mShouldReverseCoordinateOperation =
false;
245 mAllowFallbackTransforms =
false;
249static void proj_collecting_logger(
void *user_data,
int ,
const char *message )
251 QStringList *dest =
reinterpret_cast< QStringList *
>( user_data );
252 dest->append( QString( message ) );
255static void proj_logger(
void *,
int level,
const char *message )
260 if ( level == PJ_LOG_ERROR )
262 const QString messageString( message );
263 if ( messageString == QLatin1String(
"push: Invalid latitude" ) )
273 else if ( level == PJ_LOG_DEBUG )
279ProjData QgsCoordinateTransformPrivate::threadLocalProjData()
284 const QMap < uintptr_t, ProjData >::const_iterator it = mProjProjections.constFind(
reinterpret_cast< uintptr_t
>( context ) );
286 if ( it != mProjProjections.constEnd() )
288 ProjData res = it.value();
296 QStringList projErrors;
297 proj_log_func( context, &projErrors, proj_collecting_logger );
302 if ( !mProjCoordinateOperation.isEmpty() )
304 transform.reset( proj_create( context, mProjCoordinateOperation.toUtf8().constData() ) );
314 proj_context_is_network_enabled( context ) &&
315 !proj_coordoperation_is_instantiable( context, transform.get() ) )
318 if ( sMissingGridUsedByContextHandler )
321 desired.
proj = mProjCoordinateOperation;
324 sMissingGridUsedByContextHandler( mSourceCRS, mDestCRS, desired );
328 const QString err = QObject::tr(
"Could not use operation specified in project between %1 and %2. (Wanted to use: %3)." ).arg( mSourceCRS.authid(),
330 mProjCoordinateOperation );
338 mIsReversed = mShouldReverseCoordinateOperation;
342 QString nonAvailableError;
345 if ( !mSourceCRS.projObject() || ! mDestCRS.projObject() )
347 proj_log_func( context,
nullptr,
nullptr );
351 PJ_OPERATION_FACTORY_CONTEXT *operationContext = proj_create_operation_factory_context( context,
nullptr );
354 proj_operation_factory_context_set_grid_availability_use( context, operationContext, PROJ_GRID_AVAILABILITY_IGNORED );
357 proj_operation_factory_context_set_spatial_criterion( context, operationContext, PROJ_SPATIAL_CRITERION_PARTIAL_INTERSECTION );
359 if ( PJ_OBJ_LIST *ops = proj_create_operations( context, mSourceCRS.projObject(), mDestCRS.projObject(), operationContext ) )
361 mAvailableOpCount = proj_list_get_count( ops );
362 if ( mAvailableOpCount < 1 )
365 const int errNo = proj_context_errno( context );
366 if ( errNo && errNo != -61 )
368 nonAvailableError = QString( proj_errno_string( errNo ) );
372 nonAvailableError = QObject::tr(
"No coordinate operations are available between these two reference systems" );
375 else if ( mAvailableOpCount == 1 )
378 transform.reset( proj_list_get( context, ops, 0 ) );
381 if ( !proj_coordoperation_is_instantiable( context, transform.get() ) )
384 for (
int j = 0; j < proj_coordoperation_get_grid_used_count( context, transform.get() ); ++j )
386 const char *shortName =
nullptr;
387 const char *fullName =
nullptr;
388 const char *packageName =
nullptr;
389 const char *url =
nullptr;
390 int directDownload = 0;
393 proj_coordoperation_get_grid_used( context, transform.get(), j, &shortName, &fullName, &packageName, &url, &directDownload, &openLicense, &isAvailable );
397 if ( sMissingRequiredGridHandler )
400 gridDetails.
shortName = QString( shortName );
401 gridDetails.
fullName = QString( fullName );
403 gridDetails.
url = QString( url );
407 sMissingRequiredGridHandler( mSourceCRS, mDestCRS, gridDetails );
411 const QString err = QObject::tr(
"Cannot create transform between %1 and %2, missing required grid %3" ).arg( mSourceCRS.authid(),
424 transform.reset( proj_normalize_for_visualization( context, transform.get() ) );
427 const QString err = QObject::tr(
"Cannot normalize transform between %1 and %2" ).arg( mSourceCRS.authid(),
438 bool missingPreferred =
false;
439 bool stillLookingForPreferred =
true;
440 for (
int i = 0; i < mAvailableOpCount; ++ i )
442 transform.reset( proj_list_get( context, ops, i ) );
443 const bool isInstantiable = transform && proj_coordoperation_is_instantiable( context, transform.get() );
444 if ( stillLookingForPreferred && transform && !isInstantiable )
448 if ( !candidate.
proj.isEmpty() )
450 preferred = candidate;
451 missingPreferred =
true;
452 stillLookingForPreferred =
false;
455 if ( transform && isInstantiable )
463 if ( transform && missingPreferred )
467 if ( sMissingPreferredGridHandler )
469 sMissingPreferredGridHandler( mSourceCRS, mDestCRS, preferred, available );
473 const QString err = QObject::tr(
"Using non-preferred coordinate operation between %1 and %2. Using %3, preferred %4." ).arg( mSourceCRS.authid(),
483 transform.reset( proj_normalize_for_visualization( context, transform.get() ) );
486 const QString err = QObject::tr(
"Cannot normalize transform between %1 and %2" ).arg( mSourceCRS.authid(),
491 proj_list_destroy( ops );
493 proj_operation_factory_context_destroy( operationContext );
496 if ( !transform && nonAvailableError.isEmpty() )
498 const int errNo = proj_context_errno( context );
499 if ( errNo && errNo != -61 )
501 nonAvailableError = QString( proj_errno_string( errNo ) );
503 else if ( !projErrors.empty() )
505 nonAvailableError = projErrors.constLast();
508 if ( nonAvailableError.isEmpty() )
510 nonAvailableError = QObject::tr(
"No coordinate operations are available between these two reference systems" );
515 nonAvailableError = nonAvailableError.remove( QStringLiteral(
"internal_proj_create_operations: " ) );
519 if ( !nonAvailableError.isEmpty() )
521 if ( sCoordinateOperationCreationErrorHandler )
523 sCoordinateOperationCreationErrorHandler( mSourceCRS, mDestCRS, nonAvailableError );
527 const QString err = QObject::tr(
"Cannot create transform between %1 and %2: %3" ).arg( mSourceCRS.authid(),
535 proj_log_func( context,
nullptr, proj_logger );
543 ProjData res = transform.release();
544 mProjProjections.insert(
reinterpret_cast< uintptr_t
>( context ), res );
548ProjData QgsCoordinateTransformPrivate::threadLocalFallbackProjData()
553 const QMap < uintptr_t, ProjData >::const_iterator it = mProjFallbackProjections.constFind(
reinterpret_cast< uintptr_t
>( context ) );
555 if ( it != mProjFallbackProjections.constEnd() )
557 ProjData res = it.value();
566 transform.reset( proj_normalize_for_visualization(
QgsProjContext::get(), transform.get() ) );
568 ProjData res = transform.release();
569 mProjFallbackProjections.insert(
reinterpret_cast< uintptr_t
>( context ), res );
575 sMissingRequiredGridHandler = handler;
580 sMissingPreferredGridHandler = handler;
585 sCoordinateOperationCreationErrorHandler = handler;
590 sMissingGridUsedByContextHandler = handler;
595 sDynamicCrsToDynamicCrsWarningHandler = handler;
598void QgsCoordinateTransformPrivate::freeProj()
601 if ( mProjProjections.isEmpty() && mProjFallbackProjections.isEmpty() )
603 QMap < uintptr_t, ProjData >::const_iterator it = mProjProjections.constBegin();
610 PJ_CONTEXT *tmpContext = proj_context_create();
611 for ( ; it != mProjProjections.constEnd(); ++it )
613 proj_assign_context( it.value(), tmpContext );
614 proj_destroy( it.value() );
617 it = mProjFallbackProjections.constBegin();
618 for ( ; it != mProjFallbackProjections.constEnd(); ++it )
620 proj_assign_context( it.value(), tmpContext );
621 proj_destroy( it.value() );
624 proj_context_destroy( tmpContext );
625 mProjProjections.clear();
626 mProjFallbackProjections.clear();
629bool QgsCoordinateTransformPrivate::removeObjectsBelongingToCurrentThread(
void *pj_context )
633 QMap < uintptr_t, ProjData >::iterator it = mProjProjections.find(
reinterpret_cast< uintptr_t
>( pj_context ) );
634 if ( it != mProjProjections.end() )
636 proj_destroy( it.value() );
637 mProjProjections.erase( it );
640 it = mProjFallbackProjections.find(
reinterpret_cast< uintptr_t
>( pj_context ) );
641 if ( it != mProjFallbackProjections.end() )
643 proj_destroy( it.value() );
644 mProjFallbackProjections.erase( it );
647 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)