Skip to content

Commit 339d061

Browse files
committedJul 5, 2016
Use QgsCRSCache instead of looking up CRS by srs id (refs #15193)
1 parent 9139872 commit 339d061

9 files changed

+61
-10
lines changed
 

‎python/core/qgscoordinatereferencesystem.sip

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ class QgsCoordinateReferenceSystem
7777
* @note Any members will be overwritten during this process.
7878
* @param theSrsId The QGIS SrsId for the desired spatial reference system.
7979
* @return bool TRUE if success else false
80+
* @note this method is expensive. Consider using QgsCRSCache::crsBySrsId() instead.
8081
*/
8182
bool createFromSrsId( const long theSrsId );
8283

‎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 specified QGIS SRS ID.
75+
* @param srsId internal QGIS SRS ID
76+
* @returns matching CRS, or an invalid CRS if ID could not be found
77+
* @note added in QGIS 2.16
78+
* @see QgsCoordinateReferenceSystem::createFromSrsId()
79+
*/
80+
QgsCoordinateReferenceSystem crsBySrsId( long srsId ) const;
81+
7482
/** Updates the cached definition of a CRS. Should be called if the definition of a user-created
7583
* CRS has been changed.
7684
* @param authid CRS auth ID, eg "EPSG:4326" or "USER:100009"

‎src/core/qgscoordinatereferencesystem.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ class CORE_EXPORT QgsCoordinateReferenceSystem
124124
* @note Any members will be overwritten during this process.
125125
* @param theSrsId The QGIS SrsId for the desired spatial reference system.
126126
* @return bool TRUE if success else false
127+
* @note this method is expensive. Consider using QgsCRSCache::crsBySrsId() instead.
127128
*/
128129
bool createFromSrsId( const long theSrsId );
129130

‎src/core/qgscoordinatetransform.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ void QgsCoordinateTransform::setDestCRS( const QgsCoordinateReferenceSystem& the
152152
void QgsCoordinateTransform::setDestCRSID( long theCRSID )
153153
{
154154
//!todo Add some logic here to determine if the srsid is a system or user one
155-
mDestCRS.createFromSrsId( theCRSID );
155+
mDestCRS = QgsCRSCache::instance()->crsBySrsId( theCRSID );
156156
initialise();
157157
}
158158

‎src/core/qgscrscache.cpp

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ QgsCoordinateReferenceSystem QgsCRSCache::crsByEpsgId( long epsg ) const
143143

144144
QgsCoordinateReferenceSystem QgsCRSCache::crsByProj4( const QString& proj4 ) const
145145
{
146-
QHash< QString, QgsCoordinateReferenceSystem >::const_iterator crsIt = mCRSProj4.find( proj4 );
146+
QHash< QString, QgsCoordinateReferenceSystem >::const_iterator crsIt = mCRSProj4.constFind( proj4 );
147147
if ( crsIt == mCRSProj4.constEnd() )
148148
{
149149
QgsCoordinateReferenceSystem s;
@@ -158,3 +158,21 @@ QgsCoordinateReferenceSystem QgsCRSCache::crsByProj4( const QString& proj4 ) con
158158
return crsIt.value();
159159
}
160160
}
161+
162+
QgsCoordinateReferenceSystem QgsCRSCache::crsBySrsId( long srsId ) const
163+
{
164+
QHash< long, QgsCoordinateReferenceSystem >::const_iterator crsIt = mCRSSrsId.constFind( srsId );
165+
if ( crsIt == mCRSSrsId.constEnd() )
166+
{
167+
QgsCoordinateReferenceSystem s;
168+
if ( ! s.createFromSrsId( srsId ) )
169+
{
170+
return mCRSSrsId.insert( srsId, mInvalidCRS ).value();
171+
}
172+
return mCRSSrsId.insert( srsId, s ).value();
173+
}
174+
else
175+
{
176+
return crsIt.value();
177+
}
178+
}

‎src/core/qgscrscache.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,14 @@ class CORE_EXPORT QgsCRSCache
9797
*/
9898
QgsCoordinateReferenceSystem crsByProj4( const QString& proj4 ) const;
9999

100+
/** Returns the CRS from a specified QGIS SRS ID.
101+
* @param srsId internal QGIS SRS ID
102+
* @returns matching CRS, or an invalid CRS if ID could not be found
103+
* @note added in QGIS 2.16
104+
* @see QgsCoordinateReferenceSystem::createFromSrsId()
105+
*/
106+
QgsCoordinateReferenceSystem crsBySrsId( long srsId ) const;
107+
100108
/** Updates the cached definition of a CRS. Should be called if the definition of a user-created
101109
* CRS has been changed.
102110
* @param authid CRS auth ID, eg "EPSG:4326" or "USER:100009"
@@ -110,6 +118,7 @@ class CORE_EXPORT QgsCRSCache
110118

111119
mutable QHash< QString, QgsCoordinateReferenceSystem > mCRS;
112120
mutable QHash< QString, QgsCoordinateReferenceSystem > mCRSProj4;
121+
mutable QHash< long, QgsCoordinateReferenceSystem > mCRSSrsId;
113122

114123
/** CRS that is not initialized (returned in case of error)*/
115124
QgsCoordinateReferenceSystem mInvalidCRS;

‎src/core/qgsdistancearea.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,7 @@ bool QgsDistanceArea::willUseEllipsoid() const
106106

107107
void QgsDistanceArea::setSourceCrs( long srsid )
108108
{
109-
QgsCoordinateReferenceSystem srcCRS;
110-
srcCRS.createFromSrsId( srsid );
109+
QgsCoordinateReferenceSystem srcCRS = QgsCRSCache::instance()->crsBySrsId( srsid );
111110
mCoordTransform->setSourceCrs( srcCRS );
112111
}
113112

‎src/gui/qgsprojectionselectionwidget.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,7 @@ QgsCoordinateReferenceSystem QgsProjectionSelectionWidget::crs() const
8787
case QgsProjectionSelectionWidget::RecentCrs:
8888
{
8989
long srsid = mCrsComboBox->itemData( mCrsComboBox->currentIndex(), Qt::UserRole + 1 ).toLongLong();
90-
QgsCoordinateReferenceSystem crs;
91-
crs.createFromSrsId( srsid );
90+
QgsCoordinateReferenceSystem crs = QgsCRSCache::instance()->crsBySrsId( srsid );
9291
return crs;
9392
}
9493
}
@@ -174,8 +173,7 @@ void QgsProjectionSelectionWidget::comboIndexChanged( int idx )
174173
case QgsProjectionSelectionWidget::RecentCrs:
175174
{
176175
long srsid = mCrsComboBox->itemData( idx, Qt::UserRole + 1 ).toLongLong();
177-
QgsCoordinateReferenceSystem crs;
178-
crs.createFromSrsId( srsid );
176+
QgsCoordinateReferenceSystem crs = QgsCRSCache::instance()->crsBySrsId( srsid );
179177
emit crsChanged( crs );
180178
return;
181179
}
@@ -252,8 +250,7 @@ void QgsProjectionSelectionWidget::addRecentCrs()
252250
}
253251

254252
i++;
255-
QgsCoordinateReferenceSystem crs;
256-
crs.createFromSrsId( srsid );
253+
QgsCoordinateReferenceSystem crs = QgsCRSCache::instance()->crsBySrsId( srsid );
257254
if ( crs.isValid() )
258255
{
259256
mCrsComboBox->addItem( tr( "%1 - %2" ).arg( crs.authid(), crs.description() ), QgsProjectionSelectionWidget::RecentCrs );

‎tests/src/python/test_qgscrscache.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,5 +89,23 @@ def testcrsByProj4(self):
8989
crs = QgsCRSCache.instance().crsByProj4('asdasdasd')
9090
self.assertFalse(crs.isValid())
9191

92+
def testcrsBySrsId(self):
93+
""" test retrieving CRS from cache using srs id """
94+
95+
crs = QgsCRSCache.instance().crsBySrsId(3452)
96+
self.assertTrue(crs.isValid())
97+
self.assertEqual(crs.authid(), 'EPSG:4326')
98+
# a second time, so crs is fetched from cache
99+
crs = QgsCRSCache.instance().crsBySrsId(3452)
100+
self.assertTrue(crs.isValid())
101+
self.assertEqual(crs.authid(), 'EPSG:4326')
102+
103+
# invalid
104+
crs = QgsCRSCache.instance().crsBySrsId(-9999)
105+
self.assertFalse(crs.isValid())
106+
# a second time, so invalid crs is fetched from cache
107+
crs = QgsCRSCache.instance().crsBySrsId(-9999)
108+
self.assertFalse(crs.isValid())
109+
92110
if __name__ == '__main__':
93111
unittest.main()

1 commit comments

Comments
 (1)

nirvn commented on Jul 5, 2016

@nirvn
Contributor

@nyalldawson it's a nice speed improvement over here.

Please sign in to comment.