Skip to content

Commit fd88d91

Browse files
committed
Use QgsCRSCache in more places
1 parent 867dbe5 commit fd88d91

27 files changed

+133
-44
lines changed

python/core/qgscrscache.sip

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,14 @@ class QgsCRSCache
7171
*/
7272
QgsCoordinateReferenceSystem crsByProj4( const QString& proj4 ) const;
7373

74+
/** Returns the CRS from a WKT spatial ref sys definition string.
75+
* @param wkt WKT for the desired spatial reference system.
76+
* @returns matching CRS, or an invalid CRS if string could not be matched
77+
* @note added in QGIS 2.16
78+
* @see QgsCoordinateReferenceSystem::createFromWkt()
79+
*/
80+
QgsCoordinateReferenceSystem crsByWkt( const QString& wkt ) const;
81+
7482
/** Returns the CRS from a specified QGIS SRS ID.
7583
* @param srsId internal QGIS SRS ID
7684
* @returns matching CRS, or an invalid CRS if ID could not be found

src/app/ogr/qgsvectorlayersaveasdialog.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
* *
1717
***************************************************************************/
1818
#include "qgslogger.h"
19+
#include "qgscrscache.h"
1920
#include "qgsvectorlayersaveasdialog.h"
2021
#include "qgsgenericprojectionselector.h"
2122
#include "qgsvectordataprovider.h"
@@ -101,7 +102,7 @@ void QgsVectorLayerSaveAsDialog::setup()
101102
idx = 0;
102103
}
103104

104-
QgsCoordinateReferenceSystem srs( mCRS, QgsCoordinateReferenceSystem::InternalCrsId );
105+
QgsCoordinateReferenceSystem srs = QgsCRSCache::instance()->crsBySrsId( mCRS );
105106
mCrsSelector->setCrs( srs );
106107
mCrsSelector->setLayerCrs( srs );
107108
mCrsSelector->dialog()->setMessage( tr( "Select the coordinate reference system for the vector file. "

src/app/openstreetmap/qgsosmdownloaddialog.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
#include "qgis.h"
2323
#include "qgisapp.h"
24+
#include "qgscrscache.h"
2425
#include "qgsmapcanvas.h"
2526
#include "qgsmaplayer.h"
2627
#include "qgsmaplayerregistry.h"
@@ -103,7 +104,7 @@ void QgsOSMDownloadDialog::onExtentCanvas()
103104

104105
if ( QgisApp::instance()->mapCanvas()->hasCrsTransformEnabled() )
105106
{
106-
QgsCoordinateReferenceSystem dst( GEOCRS_ID, QgsCoordinateReferenceSystem::InternalCrsId );
107+
QgsCoordinateReferenceSystem dst = QgsCRSCache::instance()->crsBySrsId( GEOCRS_ID );
107108

108109
QgsCoordinateTransform ct( QgisApp::instance()->mapCanvas()->mapSettings().destinationCrs(), dst );
109110
r = ct.transformBoundingBox( r );
@@ -142,7 +143,7 @@ void QgsOSMDownloadDialog::onCurrentLayerChanged( int index )
142143
if ( !layer )
143144
return;
144145

145-
QgsCoordinateReferenceSystem dst( GEOCRS_ID, QgsCoordinateReferenceSystem::InternalCrsId );
146+
QgsCoordinateReferenceSystem dst = QgsCRSCache::instance()->crsBySrsId( GEOCRS_ID );
146147

147148
QgsCoordinateTransform ct( layer->crs(), dst );
148149
QgsRectangle rect( ct.transformBoundingBox( layer->extent() ) );

src/app/qgisapp.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6166,7 +6166,7 @@ void QgisApp::saveAsVectorFileGeneral( QgsVectorLayer* vlayer, bool symbologyOpt
61666166
QgsWKBTypes::Type forcedGeometryType = dialog->geometryType();
61676167

61686168
QgsCoordinateTransform* ct = nullptr;
6169-
destCRS = QgsCoordinateReferenceSystem( dialog->crs(), QgsCoordinateReferenceSystem::InternalCrsId );
6169+
destCRS = QgsCRSCache::instance()->crsBySrsId( dialog->crs() );
61706170

61716171
if ( destCRS.isValid() && destCRS != vlayer->crs() )
61726172
{
@@ -8504,7 +8504,7 @@ void QgisApp::setLayerCRS()
85048504
return;
85058505
}
85068506

8507-
QgsCoordinateReferenceSystem crs( mySelector.selectedCrsId(), QgsCoordinateReferenceSystem::InternalCrsId );
8507+
QgsCoordinateReferenceSystem crs = QgsCRSCache::instance()->crsBySrsId( mySelector.selectedCrsId() );
85088508

85098509
Q_FOREACH ( QgsLayerTreeNode* node, mLayerTreeView->selectedNodes() )
85108510
{
@@ -8661,7 +8661,7 @@ void QgisApp::legendGroupSetCRS()
86618661
return;
86628662
}
86638663

8664-
QgsCoordinateReferenceSystem crs( mySelector.selectedCrsId(), QgsCoordinateReferenceSystem::InternalCrsId );
8664+
QgsCoordinateReferenceSystem crs = QgsCRSCache::instance()->crsBySrsId( mySelector.selectedCrsId() );
86658665
Q_FOREACH ( QgsLayerTreeLayer* nodeLayer, currentGroup->findLayers() )
86668666
{
86678667
if ( nodeLayer->layer() )

src/app/qgsbookmarks.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "qgsmaprenderer.h"
2323
#include "qgsproject.h"
2424
#include "qgsmessagelog.h"
25+
#include "qgscrscache.h"
2526

2627
#include "qgslogger.h"
2728

@@ -232,7 +233,7 @@ void QgsBookmarks::zoomToBookmark()
232233
if ( srid > 0 &&
233234
srid != QgisApp::instance()->mapCanvas()->mapSettings().destinationCrs().srsid() )
234235
{
235-
QgsCoordinateTransform ct( QgsCoordinateReferenceSystem( srid, QgsCoordinateReferenceSystem::InternalCrsId ),
236+
QgsCoordinateTransform ct( QgsCRSCache::instance()->crsBySrsId( srid ),
236237
QgisApp::instance()->mapCanvas()->mapSettings().destinationCrs() );
237238
rect = ct.transform( rect );
238239
if ( rect.isEmpty() )

src/app/qgsprojectproperties.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "qgscomposer.h"
2626
#include "qgscontexthelp.h"
2727
#include "qgscoordinatetransform.h"
28+
#include "qgscrscache.h"
2829
#include "qgslogger.h"
2930
#include "qgsmapcanvas.h"
3031
#include "qgsmaplayer.h"
@@ -129,7 +130,7 @@ QgsProjectProperties::QgsProjectProperties( QgsMapCanvas* mapCanvas, QWidget *pa
129130
// slot triggered by setChecked() might use it.
130131
mProjectSrsId = mMapCanvas->mapSettings().destinationCrs().srsid();
131132

132-
QgsCoordinateReferenceSystem srs( mProjectSrsId, QgsCoordinateReferenceSystem::InternalCrsId );
133+
QgsCoordinateReferenceSystem srs = QgsCRSCache::instance()->crsBySrsId( mProjectSrsId );
133134
updateGuiForMapUnits( srs.mapUnits() );
134135

135136
QgsDebugMsg( "Read project CRSID: " + QString::number( mProjectSrsId ) );
@@ -774,7 +775,7 @@ void QgsProjectProperties::apply()
774775
long myCRSID = projectionSelector->selectedCrsId();
775776
if ( myCRSID )
776777
{
777-
QgsCoordinateReferenceSystem srs( myCRSID, QgsCoordinateReferenceSystem::InternalCrsId );
778+
QgsCoordinateReferenceSystem srs = QgsCRSCache::instance()->crsBySrsId( myCRSID );
778779
mMapCanvas->setDestinationCrs( srs );
779780
QgsDebugMsg( QString( "Selected CRS " ) + srs.description() );
780781
// write the currently selected projections _proj string_ to project settings
@@ -1302,7 +1303,7 @@ void QgsProjectProperties::srIdUpdated()
13021303
if ( !isProjected() || !myCRSID )
13031304
return;
13041305

1305-
QgsCoordinateReferenceSystem srs( myCRSID, QgsCoordinateReferenceSystem::InternalCrsId );
1306+
QgsCoordinateReferenceSystem srs = QgsCRSCache::instance()->crsBySrsId( myCRSID );
13061307
//set radio button to crs map unit type
13071308
QGis::UnitType units = srs.mapUnits();
13081309

@@ -1395,7 +1396,7 @@ void QgsProjectProperties::on_pbnWMSSetUsedSRS_clicked()
13951396

13961397
if ( cbxProjectionEnabled->isChecked() )
13971398
{
1398-
QgsCoordinateReferenceSystem srs( projectionSelector->selectedCrsId(), QgsCoordinateReferenceSystem::InternalCrsId );
1399+
QgsCoordinateReferenceSystem srs = QgsCRSCache::instance()->crsBySrsId( projectionSelector->selectedCrsId() );
13991400
crsList << srs.authid();
14001401
}
14011402

src/browser/qgsbrowser.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "qgsdataitem.h"
2424
#include "qgsbrowser.h"
2525
#include "qgsbrowsermodel.h"
26+
#include "qgscrscache.h"
2627
#include "qgsencodingfiledialog.h"
2728
#include "qgsgenericprojectionselector.h"
2829
#include "qgslogger.h"
@@ -302,7 +303,7 @@ void QgsBrowser::on_mActionSetProjection_triggered()
302303
mySelector->setSelectedCrsId( mLayer->crs().srsid() );
303304
if ( mySelector->exec() )
304305
{
305-
QgsCoordinateReferenceSystem srs( mySelector->selectedCrsId(), QgsCoordinateReferenceSystem::InternalCrsId );
306+
QgsCoordinateReferenceSystem srs = QgsCRSCache::instance()->crsBySrsId( mySelector->selectedCrsId() );
306307

307308
// TODO: open data source in write mode set crs and save
308309
#if 0

src/core/qgscoordinatereferencesystem.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ class CORE_EXPORT QgsCoordinateReferenceSystem
114114
* @note SRID and EpsgCrsId may be blank if no match can be found on SRS db.
115115
* @param theWkt The WKT for the desired spatial reference system.
116116
* @return bool TRUE if success else false
117+
* @note this method is expensive. Consider using QgsCRSCache::crsByWkt() instead.
117118
*/
118119
bool createFromWkt( const QString &theWkt );
119120

src/core/qgscoordinatetransform.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ QgsCoordinateTransform::QgsCoordinateTransform( const QgsCoordinateReferenceSyst
6767
QgsCoordinateTransform::QgsCoordinateTransform( long theSourceSrsId, long theDestSrsId )
6868
: QObject()
6969
, mInitialisedFlag( false )
70-
, mSourceCRS( theSourceSrsId, QgsCoordinateReferenceSystem::InternalCrsId )
71-
, mDestCRS( theDestSrsId, QgsCoordinateReferenceSystem::InternalCrsId )
70+
, mSourceCRS( QgsCRSCache::instance()->crsBySrsId( theSourceSrsId ) )
71+
, mDestCRS( QgsCRSCache::instance()->crsBySrsId( theDestSrsId ) )
7272
, mSourceProjection( nullptr )
7373
, mDestinationProjection( nullptr )
7474
, mSourceDatumTransform( -1 )
@@ -86,8 +86,8 @@ QgsCoordinateTransform::QgsCoordinateTransform( const QString& theSourceCRS, con
8686
, mDestinationDatumTransform( -1 )
8787
{
8888
setFinder();
89-
mSourceCRS.createFromWkt( theSourceCRS );
90-
mDestCRS.createFromWkt( theDestCRS );
89+
mSourceCRS = QgsCRSCache::instance()->crsByWkt( theSourceCRS );
90+
mDestCRS = QgsCRSCache::instance()->crsByWkt( theDestCRS );
9191
// initialize the coordinate system data structures
9292
//XXX Who spells initialize initialise?
9393
//XXX A: Its the queen's english....
@@ -108,7 +108,7 @@ QgsCoordinateTransform::QgsCoordinateTransform( long theSourceSrid,
108108
setFinder();
109109

110110
mSourceCRS.createFromId( theSourceSrid, theSourceCRSType );
111-
mDestCRS.createFromWkt( theDestWkt );
111+
mDestCRS = QgsCRSCache::instance()->crsByWkt( theDestWkt );
112112
// initialize the coordinate system data structures
113113
//XXX Who spells initialize initialise?
114114
//XXX A: Its the queen's english....

src/core/qgscrscache.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,36 @@ QgsCoordinateReferenceSystem QgsCRSCache::crsByProj4( const QString& proj4 ) con
187187
}
188188
}
189189

190+
QgsCoordinateReferenceSystem QgsCRSCache::crsByWkt( const QString& wkt ) const
191+
{
192+
mCRSWktLock.lockForRead();
193+
QHash< QString, QgsCoordinateReferenceSystem >::const_iterator crsIt = mCRSWkt.constFind( wkt );
194+
if ( crsIt == mCRSWkt.constEnd() )
195+
{
196+
mCRSWktLock.unlock();
197+
mCRSWktLock.lockForWrite();
198+
QgsCoordinateReferenceSystem s;
199+
if ( ! s.createFromWkt( wkt ) )
200+
{
201+
QgsCoordinateReferenceSystem result = mCRSWkt.insert( wkt, mInvalidCRS ).value();
202+
mCRSWktLock.unlock();
203+
return result;
204+
}
205+
else
206+
{
207+
QgsCoordinateReferenceSystem result = mCRSWkt.insert( wkt, s ).value();
208+
mCRSWktLock.unlock();
209+
return result;
210+
}
211+
}
212+
else
213+
{
214+
QgsCoordinateReferenceSystem result = crsIt.value();
215+
mCRSWktLock.unlock();
216+
return result;
217+
}
218+
}
219+
190220
QgsCoordinateReferenceSystem QgsCRSCache::crsBySrsId( long srsId ) const
191221
{
192222
mCRSSrsIdLock.lockForRead();

0 commit comments

Comments
 (0)