Navigation Menu

Skip to content

Commit

Permalink
Use QgsCRSCache instead of looking up CRS by srs id (refs #15193)
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jul 5, 2016
1 parent 9139872 commit 339d061
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 10 deletions.
1 change: 1 addition & 0 deletions python/core/qgscoordinatereferencesystem.sip
Expand Up @@ -77,6 +77,7 @@ class QgsCoordinateReferenceSystem
* @note Any members will be overwritten during this process.
* @param theSrsId The QGIS SrsId for the desired spatial reference system.
* @return bool TRUE if success else false
* @note this method is expensive. Consider using QgsCRSCache::crsBySrsId() instead.
*/
bool createFromSrsId( const long theSrsId );

Expand Down
8 changes: 8 additions & 0 deletions python/core/qgscrscache.sip
Expand Up @@ -71,6 +71,14 @@ class QgsCRSCache
*/
QgsCoordinateReferenceSystem crsByProj4( const QString& proj4 ) const;

/** Returns the CRS from a specified QGIS SRS ID.
* @param srsId internal QGIS SRS ID
* @returns matching CRS, or an invalid CRS if ID could not be found
* @note added in QGIS 2.16
* @see QgsCoordinateReferenceSystem::createFromSrsId()
*/
QgsCoordinateReferenceSystem crsBySrsId( long srsId ) const;

/** Updates the cached definition of a CRS. Should be called if the definition of a user-created
* CRS has been changed.
* @param authid CRS auth ID, eg "EPSG:4326" or "USER:100009"
Expand Down
1 change: 1 addition & 0 deletions src/core/qgscoordinatereferencesystem.h
Expand Up @@ -124,6 +124,7 @@ class CORE_EXPORT QgsCoordinateReferenceSystem
* @note Any members will be overwritten during this process.
* @param theSrsId The QGIS SrsId for the desired spatial reference system.
* @return bool TRUE if success else false
* @note this method is expensive. Consider using QgsCRSCache::crsBySrsId() instead.
*/
bool createFromSrsId( const long theSrsId );

Expand Down
2 changes: 1 addition & 1 deletion src/core/qgscoordinatetransform.cpp
Expand Up @@ -152,7 +152,7 @@ void QgsCoordinateTransform::setDestCRS( const QgsCoordinateReferenceSystem& the
void QgsCoordinateTransform::setDestCRSID( long theCRSID )
{
//!todo Add some logic here to determine if the srsid is a system or user one
mDestCRS.createFromSrsId( theCRSID );
mDestCRS = QgsCRSCache::instance()->crsBySrsId( theCRSID );
initialise();
}

Expand Down
20 changes: 19 additions & 1 deletion src/core/qgscrscache.cpp
Expand Up @@ -143,7 +143,7 @@ QgsCoordinateReferenceSystem QgsCRSCache::crsByEpsgId( long epsg ) const

QgsCoordinateReferenceSystem QgsCRSCache::crsByProj4( const QString& proj4 ) const
{
QHash< QString, QgsCoordinateReferenceSystem >::const_iterator crsIt = mCRSProj4.find( proj4 );
QHash< QString, QgsCoordinateReferenceSystem >::const_iterator crsIt = mCRSProj4.constFind( proj4 );
if ( crsIt == mCRSProj4.constEnd() )
{
QgsCoordinateReferenceSystem s;
Expand All @@ -158,3 +158,21 @@ QgsCoordinateReferenceSystem QgsCRSCache::crsByProj4( const QString& proj4 ) con
return crsIt.value();
}
}

QgsCoordinateReferenceSystem QgsCRSCache::crsBySrsId( long srsId ) const
{
QHash< long, QgsCoordinateReferenceSystem >::const_iterator crsIt = mCRSSrsId.constFind( srsId );
if ( crsIt == mCRSSrsId.constEnd() )
{
QgsCoordinateReferenceSystem s;
if ( ! s.createFromSrsId( srsId ) )
{
return mCRSSrsId.insert( srsId, mInvalidCRS ).value();
}
return mCRSSrsId.insert( srsId, s ).value();
}
else
{
return crsIt.value();
}
}
9 changes: 9 additions & 0 deletions src/core/qgscrscache.h
Expand Up @@ -97,6 +97,14 @@ class CORE_EXPORT QgsCRSCache
*/
QgsCoordinateReferenceSystem crsByProj4( const QString& proj4 ) const;

/** Returns the CRS from a specified QGIS SRS ID.
* @param srsId internal QGIS SRS ID
* @returns matching CRS, or an invalid CRS if ID could not be found
* @note added in QGIS 2.16
* @see QgsCoordinateReferenceSystem::createFromSrsId()
*/
QgsCoordinateReferenceSystem crsBySrsId( long srsId ) const;

/** Updates the cached definition of a CRS. Should be called if the definition of a user-created
* CRS has been changed.
* @param authid CRS auth ID, eg "EPSG:4326" or "USER:100009"
Expand All @@ -110,6 +118,7 @@ class CORE_EXPORT QgsCRSCache

mutable QHash< QString, QgsCoordinateReferenceSystem > mCRS;
mutable QHash< QString, QgsCoordinateReferenceSystem > mCRSProj4;
mutable QHash< long, QgsCoordinateReferenceSystem > mCRSSrsId;

/** CRS that is not initialized (returned in case of error)*/
QgsCoordinateReferenceSystem mInvalidCRS;
Expand Down
3 changes: 1 addition & 2 deletions src/core/qgsdistancearea.cpp
Expand Up @@ -106,8 +106,7 @@ bool QgsDistanceArea::willUseEllipsoid() const

void QgsDistanceArea::setSourceCrs( long srsid )
{
QgsCoordinateReferenceSystem srcCRS;
srcCRS.createFromSrsId( srsid );
QgsCoordinateReferenceSystem srcCRS = QgsCRSCache::instance()->crsBySrsId( srsid );
mCoordTransform->setSourceCrs( srcCRS );
}

Expand Down
9 changes: 3 additions & 6 deletions src/gui/qgsprojectionselectionwidget.cpp
Expand Up @@ -87,8 +87,7 @@ QgsCoordinateReferenceSystem QgsProjectionSelectionWidget::crs() const
case QgsProjectionSelectionWidget::RecentCrs:
{
long srsid = mCrsComboBox->itemData( mCrsComboBox->currentIndex(), Qt::UserRole + 1 ).toLongLong();
QgsCoordinateReferenceSystem crs;
crs.createFromSrsId( srsid );
QgsCoordinateReferenceSystem crs = QgsCRSCache::instance()->crsBySrsId( srsid );
return crs;
}
}
Expand Down Expand Up @@ -174,8 +173,7 @@ void QgsProjectionSelectionWidget::comboIndexChanged( int idx )
case QgsProjectionSelectionWidget::RecentCrs:
{
long srsid = mCrsComboBox->itemData( idx, Qt::UserRole + 1 ).toLongLong();
QgsCoordinateReferenceSystem crs;
crs.createFromSrsId( srsid );
QgsCoordinateReferenceSystem crs = QgsCRSCache::instance()->crsBySrsId( srsid );
emit crsChanged( crs );
return;
}
Expand Down Expand Up @@ -252,8 +250,7 @@ void QgsProjectionSelectionWidget::addRecentCrs()
}

i++;
QgsCoordinateReferenceSystem crs;
crs.createFromSrsId( srsid );
QgsCoordinateReferenceSystem crs = QgsCRSCache::instance()->crsBySrsId( srsid );
if ( crs.isValid() )
{
mCrsComboBox->addItem( tr( "%1 - %2" ).arg( crs.authid(), crs.description() ), QgsProjectionSelectionWidget::RecentCrs );
Expand Down
18 changes: 18 additions & 0 deletions tests/src/python/test_qgscrscache.py
Expand Up @@ -89,5 +89,23 @@ def testcrsByProj4(self):
crs = QgsCRSCache.instance().crsByProj4('asdasdasd')
self.assertFalse(crs.isValid())

def testcrsBySrsId(self):
""" test retrieving CRS from cache using srs id """

crs = QgsCRSCache.instance().crsBySrsId(3452)
self.assertTrue(crs.isValid())
self.assertEqual(crs.authid(), 'EPSG:4326')
# a second time, so crs is fetched from cache
crs = QgsCRSCache.instance().crsBySrsId(3452)
self.assertTrue(crs.isValid())
self.assertEqual(crs.authid(), 'EPSG:4326')

# invalid
crs = QgsCRSCache.instance().crsBySrsId(-9999)
self.assertFalse(crs.isValid())
# a second time, so invalid crs is fetched from cache
crs = QgsCRSCache.instance().crsBySrsId(-9999)
self.assertFalse(crs.isValid())

if __name__ == '__main__':
unittest.main()

1 comment on commit 339d061

@nirvn
Copy link
Contributor

@nirvn nirvn commented on 339d061 Jul 5, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Please sign in to comment.