Skip to content

Commit 439ef20

Browse files
committed
Prepare for removal of QgsCoordinateTransformCache
1 parent 018bfb4 commit 439ef20

15 files changed

+87
-25
lines changed

src/analysis/vector/geometry_checker/qgsgeometrycheck.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
#include "qgsgeometrycheck.h"
1919
#include "qgsfeaturepool.h"
2020

21-
QgsGeometryCheckerContext::QgsGeometryCheckerContext( int _precision, const QString &_mapCrs, const QMap<QString, QgsFeaturePool *> &_featurePools )
21+
QgsGeometryCheckerContext::QgsGeometryCheckerContext( int _precision, const QgsCoordinateReferenceSystem &_mapCrs, const QMap<QString, QgsFeaturePool *> &_featurePools )
2222
: tolerance( std::pow( 10, -_precision ) )
2323
, reducedTolerance( std::pow( 10, -_precision / 2 ) )
2424
, mapCrs( _mapCrs )

src/analysis/vector/geometry_checker/qgsgeometrycheck.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ class QgsFeaturePool;
3535

3636
struct ANALYSIS_EXPORT QgsGeometryCheckerContext
3737
{
38-
QgsGeometryCheckerContext( int _precision, const QString &_mapCrs, const QMap<QString, QgsFeaturePool *> &_featurePools );
38+
QgsGeometryCheckerContext( int _precision, const QgsCoordinateReferenceSystem &_mapCrs, const QMap<QString, QgsFeaturePool *> &_featurePools );
3939
const double tolerance;
4040
const double reducedTolerance;
41-
const QString mapCrs;
41+
const QgsCoordinateReferenceSystem mapCrs;
4242
const QMap<QString, QgsFeaturePool *> featurePools;
4343
};
4444

src/analysis/vector/geometry_checker/qgsgeometrychecker.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "qgsgeometrychecker.h"
1919
#include "qgsgeometrycheck.h"
2020
#include "qgsfeaturepool.h"
21+
#include "qgsproject.h"
2122

2223
#include <QtConcurrentMap>
2324
#include <QFutureWatcher>
@@ -144,7 +145,7 @@ bool QgsGeometryChecker::fixError( QgsGeometryCheckError *error, int method, boo
144145
{
145146
const QMap<QgsFeatureId, QList<QgsGeometryCheck::Change>> &layerChanges = changes[layerId];
146147
QgsFeaturePool *featurePool = mContext->featurePools[layerId];
147-
QgsCoordinateTransform t = QgsCoordinateTransformCache::instance()->transform( featurePool->getLayer()->crs().authid(), mContext->mapCrs );
148+
QgsCoordinateTransform t( featurePool->getLayer()->crs(), mContext->mapCrs, QgsProject::instance() );
148149
for ( QgsFeatureId id : layerChanges.keys() )
149150
{
150151
bool removed = false;
@@ -183,7 +184,7 @@ bool QgsGeometryChecker::fixError( QgsGeometryCheckError *error, int method, boo
183184
for ( const QString &layerId : mContext->featurePools.keys() )
184185
{
185186
QgsFeaturePool *featurePool = mContext->featurePools[layerId];
186-
QgsCoordinateTransform t = QgsCoordinateTransformCache::instance()->transform( mContext->mapCrs, featurePool->getLayer()->crs().authid() );
187+
QgsCoordinateTransform t( mContext->mapCrs, featurePool->getLayer()->crs(), QgsProject::instance() );
187188
recheckAreaFeatures[layerId] = featurePool->getIntersects( t.transform( recheckArea ) );
188189
}
189190

src/analysis/vector/geometry_checker/qgsgeometryfollowboundariescheck.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@
1616
#include "qgscrscache.h"
1717
#include "qgsgeometryfollowboundariescheck.h"
1818
#include "qgsgeometryengine.h"
19+
#include "qgsproject.h"
1920
#include "qgsspatialindex.h"
2021

22+
2123
QgsGeometryFollowBoundariesCheck::QgsGeometryFollowBoundariesCheck( QgsGeometryCheckerContext *context, QgsVectorLayer *checkLayer )
2224
: QgsGeometryCheck( FeatureNodeCheck, {QgsWkbTypes::PolygonGeometry}, context )
2325
{
@@ -48,7 +50,7 @@ void QgsGeometryFollowBoundariesCheck::collectErrors( QList<QgsGeometryCheckErro
4850
const QgsAbstractGeometry *geom = layerFeature.geometry();
4951

5052
// The geometry to crs of the check layer
51-
QgsCoordinateTransform crst = QgsCoordinateTransformCache::instance()->transform( layerFeature.layer().crs().authid(), mCheckLayer->crs().authid() );
53+
QgsCoordinateTransform crst( layerFeature.layer().crs(), mCheckLayer->crs(), QgsProject::instance() );
5254
QgsGeometry geomt( geom->clone() );
5355
geomt.transform( crst );
5456

src/app/qgscustomprojectiondialog.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ bool QgsCustomProjectionDialog::deleteCrs( const QString &id )
169169
}
170170

171171
QgsCoordinateReferenceSystem::invalidateCache();
172-
QgsCoordinateTransformCache::instance()->invalidateCrs( QStringLiteral( "USER:%1" ).arg( id ) );
172+
QgsCoordinateTransform::invalidateCache();
173173

174174
return result == SQLITE_OK;
175175
}
@@ -279,7 +279,7 @@ bool QgsCustomProjectionDialog::saveCrs( QgsCoordinateReferenceSystem parameters
279279
mExistingCRSnames[id] = name;
280280

281281
QgsCoordinateReferenceSystem::invalidateCache();
282-
QgsCoordinateTransformCache::instance()->invalidateCrs( QStringLiteral( "USER:%1" ).arg( id ) );
282+
QgsCoordinateTransform::invalidateCache();
283283

284284
// If we have a projection acronym not in the user db previously, add it.
285285
// This is a must, or else we can't select it from the vw_srs table.

src/core/composer/qgsatlascomposition.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -815,7 +815,7 @@ QgsGeometry QgsAtlasComposition::currentGeometry( const QgsCoordinateReferenceSy
815815
}
816816

817817
QgsGeometry transformed = mCurrentFeature.geometry();
818-
transformed.transform( QgsCoordinateTransformCache::instance()->transform( mCoverageLayer->crs().authid(), crs.authid() ) );
818+
transformed.transform( QgsCoordinateTransform( mCoverageLayer->crs(), crs, mComposition->project() ) );
819819
mGeometryCache[crs.srsid()] = transformed;
820820
return transformed;
821821
}

src/core/qgscoordinatetransform.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ extern "C"
4141
// if defined shows all information about transform to stdout
4242
// #define COORDINATE_TRANSFORM_VERBOSE
4343

44+
QReadWriteLock QgsCoordinateTransform::sCacheLock;
45+
QMultiHash< QPair< QString, QString >, QgsCoordinateTransform > QgsCoordinateTransform::sTransforms;
46+
4447
QgsCoordinateTransform::QgsCoordinateTransform()
4548
{
4649
d = new QgsCoordinateTransformPrivate();
@@ -67,6 +70,14 @@ QgsCoordinateTransform::QgsCoordinateTransform( const QgsCoordinateReferenceSyst
6770
#endif
6871
}
6972

73+
QgsCoordinateTransform::QgsCoordinateTransform( const QgsCoordinateReferenceSystem &source, const QgsCoordinateReferenceSystem &destination, int sourceDatumTransform, int destinationDatumTransform )
74+
{
75+
d = new QgsCoordinateTransformPrivate( source, destination, sourceDatumTransform, destinationDatumTransform );
76+
#ifdef QGISDEBUG
77+
d->mHasContext = true; // not strictly true, but we don't need to worry if datums have been explicitly set
78+
#endif
79+
}
80+
7081
QgsCoordinateTransform::QgsCoordinateTransform( const QgsCoordinateTransform &o )
7182
{
7283
d = o.d;
@@ -788,3 +799,10 @@ void QgsCoordinateTransform::initialize()
788799
d.detach();
789800
d->initialize();
790801
}
802+
803+
void QgsCoordinateTransform::invalidateCache()
804+
{
805+
sCacheLock.lockForWrite();
806+
sTransforms.clear();
807+
sCacheLock.unlock();
808+
}

src/core/qgscoordinatetransform.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,18 @@ class CORE_EXPORT QgsCoordinateTransform
101101
const QgsCoordinateReferenceSystem &destination,
102102
const QgsProject *project );
103103

104+
/**
105+
* Constructs a QgsCoordinateTransform to transform from the \a source
106+
* to \a destination coordinate reference system, with the specified
107+
* datum transforms.
108+
*
109+
* \since QGIS 3.0
110+
*/
111+
explicit QgsCoordinateTransform( const QgsCoordinateReferenceSystem &source,
112+
const QgsCoordinateReferenceSystem &destination,
113+
int sourceDatumTransform,
114+
int destinationDatumTransform );
115+
104116
/**
105117
* Copy constructor
106118
*/
@@ -364,11 +376,24 @@ class CORE_EXPORT QgsCoordinateTransform
364376
//!initialize is used to actually create the Transformer instance
365377
void initialize();
366378

379+
/**
380+
* Clears the internal cache used to initialize QgsCoordinateTransform objects.
381+
* This should be called whenever the srs database has
382+
* been modified in order to ensure that outdated CRS transforms are not created.
383+
* \since QGIS 3.0
384+
*/
385+
static void invalidateCache();
386+
367387
private:
368388

369389
static void searchDatumTransform( const QString &sql, QList< int > &transforms );
370390

371391
mutable QExplicitlySharedDataPointer<QgsCoordinateTransformPrivate> d;
392+
393+
// cache
394+
static QReadWriteLock sCacheLock;
395+
static QMultiHash< QPair< QString, QString >, QgsCoordinateTransform > sTransforms; //same auth_id pairs might have different datum transformations
396+
372397
};
373398

374399
//! Output stream operator

src/core/qgscoordinatetransform_p.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,16 @@ QgsCoordinateTransformPrivate::QgsCoordinateTransformPrivate( const QgsCoordinat
6262
initialize();
6363
}
6464

65+
QgsCoordinateTransformPrivate::QgsCoordinateTransformPrivate( const QgsCoordinateReferenceSystem &source, const QgsCoordinateReferenceSystem &destination, int sourceDatumTransform, int destDatumTransform )
66+
: mSourceCRS( source )
67+
, mDestCRS( destination )
68+
, mSourceDatumTransform( sourceDatumTransform )
69+
, mDestinationDatumTransform( destDatumTransform )
70+
{
71+
setFinder();
72+
initialize();
73+
}
74+
6575
QgsCoordinateTransformPrivate::QgsCoordinateTransformPrivate( const QgsCoordinateTransformPrivate &other )
6676
: QSharedData( other )
6777
, mIsValid( other.mIsValid )

src/core/qgscoordinatetransform_p.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,11 @@ class QgsCoordinateTransformPrivate : public QSharedData
7272
const QgsCoordinateReferenceSystem &destination,
7373
const QgsCoordinateTransformContext &context );
7474

75+
QgsCoordinateTransformPrivate( const QgsCoordinateReferenceSystem &source,
76+
const QgsCoordinateReferenceSystem &destination,
77+
int sourceDatumTransform,
78+
int destDatumTransform );
79+
7580
QgsCoordinateTransformPrivate( const QgsCoordinateTransformPrivate &other );
7681

7782
~QgsCoordinateTransformPrivate();

0 commit comments

Comments
 (0)