24 #if PROJ_VERSION_MAJOR>=6 27 #include <proj_experimental.h> 34 #include <QStringList> 49 const QString &error )> QgsCoordinateTransformPrivate::sCoordinateOperationCreationErrorHandler =
nullptr;
55 #if PROJ_VERSION_MAJOR<6 56 #ifdef USE_THREAD_LOCAL 57 thread_local QgsProjContextStore QgsCoordinateTransformPrivate::mProjContext;
59 QThreadStorage< QgsProjContextStore * > QgsCoordinateTransformPrivate::mProjContext;
62 QgsProjContextStore::QgsProjContextStore()
64 context = pj_ctx_alloc();
67 QgsProjContextStore::~QgsProjContextStore()
69 pj_ctx_free( context );
75 QgsCoordinateTransformPrivate::QgsCoordinateTransformPrivate()
84 : mSourceCRS( source )
85 , mDestCRS( destination )
87 calculateTransforms( context );
93 : mSourceCRS( source )
94 , mDestCRS( destination )
95 , mSourceDatumTransform( sourceDatumTransform )
96 , mDestinationDatumTransform( destDatumTransform )
100 QgsCoordinateTransformPrivate::QgsCoordinateTransformPrivate(
const QgsCoordinateTransformPrivate &other )
101 : QSharedData( other )
102 , mIsValid( other.mIsValid )
103 , mShortCircuit( other.mShortCircuit )
104 , mSourceCRS( other.mSourceCRS )
105 , mDestCRS( other.mDestCRS )
106 , mSourceDatumTransform( other.mSourceDatumTransform )
107 , mDestinationDatumTransform( other.mDestinationDatumTransform )
108 , mProjCoordinateOperation( other.mProjCoordinateOperation )
110 #if PROJ_VERSION_MAJOR < 6 118 QgsCoordinateTransformPrivate::~QgsCoordinateTransformPrivate()
125 bool QgsCoordinateTransformPrivate::checkValidity()
127 if ( !mSourceCRS.isValid() || !mDestCRS.isValid() )
135 void QgsCoordinateTransformPrivate::invalidate()
137 mShortCircuit =
true;
141 bool QgsCoordinateTransformPrivate::initialize()
144 if ( !mSourceCRS.isValid() )
152 if ( !mDestCRS.isValid() )
156 mDestCRS = mSourceCRS;
166 #if PROJ_VERSION_MAJOR < 6 168 int sourceDatumTransform = mSourceDatumTransform;
169 int destDatumTransform = mDestinationDatumTransform;
170 bool useDefaultDatumTransform = ( sourceDatumTransform == - 1 && destDatumTransform == -1 );
172 mSourceProjString = mSourceCRS.toProj4();
173 if ( !useDefaultDatumTransform )
175 mSourceProjString = stripDatumTransform( mSourceProjString );
177 if ( sourceDatumTransform != -1 )
182 mDestProjString = mDestCRS.toProj4();
183 if ( !useDefaultDatumTransform )
185 mDestProjString = stripDatumTransform( mDestProjString );
187 if ( destDatumTransform != -1 )
192 if ( !useDefaultDatumTransform )
194 addNullGridShifts( mSourceProjString, mDestProjString, sourceDatumTransform, destDatumTransform );
200 ProjData res = threadLocalProjData();
202 #ifdef COORDINATE_TRANSFORM_VERBOSE 203 QgsDebugMsg(
"From proj : " + mSourceCRS.toProj4() );
207 #if PROJ_VERSION_MAJOR>=6 211 if ( !res.first || !res.second )
217 #ifdef COORDINATE_TRANSFORM_VERBOSE 220 QgsDebugMsg( QStringLiteral(
"------------------------------------------------------------" ) );
221 QgsDebugMsg( QStringLiteral(
"The OGR Coordinate transformation for this layer was set to" ) );
222 QgsLogger::debug<QgsCoordinateReferenceSystem>(
"Input", mSourceCRS, __FILE__, __FUNCTION__, __LINE__ );
223 QgsLogger::debug<QgsCoordinateReferenceSystem>(
"Output", mDestCRS, __FILE__, __FUNCTION__, __LINE__ );
224 QgsDebugMsg( QStringLiteral(
"------------------------------------------------------------" ) );
228 QgsDebugMsg( QStringLiteral(
"------------------------------------------------------------" ) );
229 QgsDebugMsg( QStringLiteral(
"The OGR Coordinate transformation FAILED TO INITIALIZE!" ) );
230 QgsDebugMsg( QStringLiteral(
"------------------------------------------------------------" ) );
235 QgsDebugMsg( QStringLiteral(
"Coordinate transformation failed to initialize!" ) );
242 if ( mSourceCRS == mDestCRS )
246 mShortCircuit =
true;
247 QgsDebugMsgLevel( QStringLiteral(
"Source/Dest CRS equal, shortcircuit is set." ), 3 );
252 mShortCircuit =
false;
253 QgsDebugMsgLevel( QStringLiteral(
"Source/Dest CRS not equal, shortcircuit is not set." ), 3 );
261 #if PROJ_VERSION_MAJOR >= 6 267 mDestinationDatumTransform = transforms.destinationTransformId;
272 #if PROJ_VERSION_MAJOR>=6 273 static void proj_collecting_logger(
void *user_data,
int ,
const char *message )
275 QStringList *dest =
reinterpret_cast< QStringList *
>( user_data );
276 dest->append( QString( message ) );
279 static void proj_logger(
void *,
int level,
const char *message )
284 if ( level == PJ_LOG_ERROR )
288 else if ( level == PJ_LOG_DEBUG )
295 ProjData QgsCoordinateTransformPrivate::threadLocalProjData()
299 #if PROJ_VERSION_MAJOR>=6 301 QMap < uintptr_t, ProjData >::const_iterator it = mProjProjections.constFind( reinterpret_cast< uintptr_t>( context ) );
303 #ifdef USE_THREAD_LOCAL 304 QMap < uintptr_t, QPair< projPJ, projPJ > >::const_iterator it = mProjProjections.constFind( reinterpret_cast< uintptr_t>( mProjContext.get() ) );
306 projCtx pContext =
nullptr;
307 if ( mProjContext.hasLocalData() )
309 pContext = mProjContext.localData()->get();
313 mProjContext.setLocalData(
new QgsProjContextStore() );
314 pContext = mProjContext.localData()->get();
316 QMap < uintptr_t, QPair< projPJ, projPJ > >::const_iterator it = mProjProjections.constFind( reinterpret_cast< uintptr_t>( pContext ) );
320 if ( it != mProjProjections.constEnd() )
322 ProjData res = it.value();
329 #if PROJ_VERSION_MAJOR>=6 331 QStringList projErrors;
332 proj_log_func( context, &projErrors, proj_collecting_logger );
334 QgsProjUtils::proj_pj_unique_ptr transform;
335 if ( !mProjCoordinateOperation.isEmpty() )
337 transform.reset( proj_create( context, mProjCoordinateOperation.toUtf8().constData() ) );
338 if ( !transform || !proj_coordoperation_is_instantiable( context, transform.get() ) )
340 if ( sMissingGridUsedByContextHandler )
343 desired.
proj = mProjCoordinateOperation;
345 desired.
grids = QgsProjUtils::gridsUsed( mProjCoordinateOperation );
346 sMissingGridUsedByContextHandler( mSourceCRS, mDestCRS, desired );
350 const QString err = QObject::tr(
"Could not use operation specified in project between %1 and %2. (Wanted to use: %3)." ).arg( mSourceCRS.authid(),
352 mProjCoordinateOperation );
361 transform.reset( proj_normalize_for_visualization( context, transform.get() ) );
364 const QString err = QObject::tr(
"Cannot normalize transform between %1 and %2" ).arg( mSourceCRS.authid(),
371 QString nonAvailableError;
374 if ( !mSourceCRS.projObject() || ! mDestCRS.projObject() )
376 proj_log_func( context,
nullptr,
nullptr );
380 PJ_OPERATION_FACTORY_CONTEXT *operationContext = proj_create_operation_factory_context( context,
nullptr );
383 proj_operation_factory_context_set_grid_availability_use( context, operationContext, PROJ_GRID_AVAILABILITY_IGNORED );
386 proj_operation_factory_context_set_spatial_criterion( context, operationContext, PROJ_SPATIAL_CRITERION_PARTIAL_INTERSECTION );
388 if ( PJ_OBJ_LIST *ops = proj_create_operations( context, mSourceCRS.projObject(), mDestCRS.projObject(), operationContext ) )
390 int count = proj_list_get_count( ops );
394 int errNo = proj_context_errno( context );
395 if ( errNo && errNo != -61 )
397 nonAvailableError = QString( proj_errno_string( errNo ) );
401 nonAvailableError = QObject::tr(
"No coordinate operations are available between these two reference systems" );
404 else if ( count == 1 )
407 transform.reset( proj_list_get( context, ops, 0 ) );
410 if ( !proj_coordoperation_is_instantiable( context, transform.get() ) )
413 for (
int j = 0; j < proj_coordoperation_get_grid_used_count( context, transform.get() ); ++j )
415 const char *shortName =
nullptr;
416 const char *fullName =
nullptr;
417 const char *packageName =
nullptr;
418 const char *url =
nullptr;
419 int directDownload = 0;
422 proj_coordoperation_get_grid_used( context, transform.get(), j, &shortName, &fullName, &packageName, &url, &directDownload, &openLicense, &isAvailable );
426 if ( sMissingRequiredGridHandler )
429 gridDetails.
shortName = QString( shortName );
430 gridDetails.
fullName = QString( fullName );
432 gridDetails.
url = QString( url );
436 sMissingRequiredGridHandler( mSourceCRS, mDestCRS, gridDetails );
440 const QString err = QObject::tr(
"Cannot create transform between %1 and %2, missing required grid %3" ).arg( mSourceCRS.authid(),
453 transform.reset( proj_normalize_for_visualization( context, transform.get() ) );
456 const QString err = QObject::tr(
"Cannot normalize transform between %1 and %2" ).arg( mSourceCRS.authid(),
467 bool missingPreferred =
false;
468 for (
int i = 0; i < count; ++ i )
470 transform.reset( proj_list_get( context, ops, i ) );
471 const bool isInstantiable = transform && proj_coordoperation_is_instantiable( context, transform.get() );
472 if ( i == 0 && transform && !isInstantiable )
475 missingPreferred =
true;
476 preferred = QgsDatumTransform::transformDetailsFromPj( transform.get() );
478 if ( transform && isInstantiable )
486 if ( transform && missingPreferred )
490 if ( sMissingPreferredGridHandler )
492 sMissingPreferredGridHandler( mSourceCRS, mDestCRS, preferred, available );
496 const QString err = QObject::tr(
"Using non-preferred coordinate operation between %1 and %2. Using %3, preferred %4." ).arg( mSourceCRS.authid(),
506 transform.reset( proj_normalize_for_visualization( context, transform.get() ) );
509 const QString err = QObject::tr(
"Cannot normalize transform between %1 and %2" ).arg( mSourceCRS.authid(),
514 proj_list_destroy( ops );
516 proj_operation_factory_context_destroy( operationContext );
519 if ( !transform && nonAvailableError.isEmpty() )
521 int errNo = proj_context_errno( context );
522 if ( errNo && errNo != -61 )
524 nonAvailableError = QString( proj_errno_string( errNo ) );
526 else if ( !projErrors.empty() )
528 nonAvailableError = projErrors.constLast();
531 if ( nonAvailableError.isEmpty() )
533 nonAvailableError = QObject::tr(
"No coordinate operations are available between these two reference systems" );
538 nonAvailableError = nonAvailableError.remove( QStringLiteral(
"internal_proj_create_operations: " ) );
542 if ( !nonAvailableError.isEmpty() )
544 if ( sCoordinateOperationCreationErrorHandler )
546 sCoordinateOperationCreationErrorHandler( mSourceCRS, mDestCRS, nonAvailableError );
550 const QString err = QObject::tr(
"Cannot create transform between %1 and %2: %3" ).arg( mSourceCRS.authid(),
558 proj_log_func( context,
nullptr, proj_logger );
566 ProjData res = transform.release();
567 mProjProjections.insert( reinterpret_cast< uintptr_t>( context ), res );
569 #ifdef USE_THREAD_LOCAL 571 QPair<projPJ, projPJ> res = qMakePair( pj_init_plus_ctx( mProjContext.get(), mSourceProjString.toUtf8() ),
572 pj_init_plus_ctx( mProjContext.get(), mDestProjString.toUtf8() ) );
574 mProjProjections.insert( reinterpret_cast< uintptr_t>( mProjContext.get() ), res );
576 QPair<projPJ, projPJ> res = qMakePair( pj_init_plus_ctx( pContext, mSourceProjString.toUtf8() ),
577 pj_init_plus_ctx( pContext, mDestProjString.toUtf8() ) );
578 mProjProjections.insert( reinterpret_cast< uintptr_t>( pContext ), res );
586 sMissingRequiredGridHandler = handler;
591 sMissingPreferredGridHandler = handler;
596 sCoordinateOperationCreationErrorHandler = handler;
601 sMissingGridUsedByContextHandler = handler;
604 #if PROJ_VERSION_MAJOR<6 605 QString QgsCoordinateTransformPrivate::stripDatumTransform(
const QString &proj4 )
const 607 QStringList parameterSplit = proj4.split(
'+', QString::SkipEmptyParts );
608 QString currentParameter;
609 QString newProjString;
611 for (
int i = 0; i < parameterSplit.size(); ++i )
613 currentParameter = parameterSplit.at( i );
614 if ( !currentParameter.startsWith( QLatin1String(
"towgs84" ), Qt::CaseInsensitive )
615 && !currentParameter.startsWith( QLatin1String(
"nadgrids" ), Qt::CaseInsensitive ) )
617 newProjString.append(
'+' );
618 newProjString.append( currentParameter );
619 newProjString.append(
' ' );
622 return newProjString;
625 void QgsCoordinateTransformPrivate::addNullGridShifts( QString &srcProjString, QString &destProjString,
626 int sourceDatumTransform,
int destinationDatumTransform )
const 629 if ( destinationDatumTransform == -1 && srcProjString.contains( QLatin1String(
"+nadgrids" ) ) )
631 destProjString += QLatin1String(
" +nadgrids=@null" );
634 if ( sourceDatumTransform == -1 && destProjString.contains( QLatin1String(
"+nadgrids" ) ) )
636 srcProjString += QLatin1String(
" +nadgrids=@null" );
642 if ( mSourceCRS.authid().compare( QLatin1String(
"EPSG:3857" ), Qt::CaseInsensitive ) == 0 && sourceDatumTransform == -1 )
644 srcProjString += QLatin1String(
" +nadgrids=@null" );
646 if ( mDestCRS.authid().compare( QLatin1String(
"EPSG:3857" ), Qt::CaseInsensitive ) == 0 && destinationDatumTransform == -1 )
648 destProjString += QLatin1String(
" +nadgrids=@null" );
653 void QgsCoordinateTransformPrivate::freeProj()
656 if ( mProjProjections.isEmpty() )
658 QMap < uintptr_t, ProjData >::const_iterator it = mProjProjections.constBegin();
659 #if PROJ_VERSION_MAJOR>=6 665 PJ_CONTEXT *tmpContext = proj_context_create();
666 for ( ; it != mProjProjections.constEnd(); ++it )
668 proj_assign_context( it.value(), tmpContext );
669 proj_destroy( it.value() );
671 proj_context_destroy( tmpContext );
673 projCtx tmpContext = pj_ctx_alloc();
674 for ( ; it != mProjProjections.constEnd(); ++it )
676 pj_set_ctx( it.value().first, tmpContext );
677 pj_free( it.value().first );
678 pj_set_ctx( it.value().second, tmpContext );
679 pj_free( it.value().second );
681 pj_ctx_free( tmpContext );
683 mProjProjections.clear();
686 #if PROJ_VERSION_MAJOR>=6 687 bool QgsCoordinateTransformPrivate::removeObjectsBelongingToCurrentThread(
void *pj_context )
691 QMap < uintptr_t, ProjData >::iterator it = mProjProjections.find( reinterpret_cast< uintptr_t>( pj_context ) );
692 if ( it != mProjProjections.end() )
694 proj_destroy( it.value() );
695 mProjProjections.erase( it );
698 return mProjProjections.isEmpty();
#define Q_NOWARN_DEPRECATED_PUSH
The QgsReadWriteLocker class is a convenience class that simplifies locking and unlocking QReadWriteL...
#define QgsDebugMsgLevel(str, level)
static void logMessage(const QString &message, const QString &tag=QString(), Qgis::MessageLevel level=Qgis::Warning, bool notifyUser=true)
Adds a message to the log instance (and creates it if necessary).
Contains information about the context in which a coordinate transform is executed.
#define Q_NOWARN_DEPRECATED_POP
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...
Q_DECL_DEPRECATED QgsDatumTransform::TransformPair calculateDatumTransforms(const QgsCoordinateReferenceSystem &source, const QgsCoordinateReferenceSystem &destination) const
Returns the pair of source and destination datum transforms to use for a transform from the specified...
This class represents a coordinate reference system (CRS).
static PJ_CONTEXT * get()
Returns a thread local instance of a proj context, safe for use in the current thread.