Skip to content

Commit

Permalink
Use QgsCRSCache in more places
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jul 5, 2016
1 parent 867dbe5 commit fd88d91
Show file tree
Hide file tree
Showing 27 changed files with 133 additions and 44 deletions.
8 changes: 8 additions & 0 deletions python/core/qgscrscache.sip
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,14 @@ class QgsCRSCache
*/
QgsCoordinateReferenceSystem crsByProj4( const QString& proj4 ) const;

/** Returns the CRS from a WKT spatial ref sys definition string.
* @param wkt WKT for the desired spatial reference system.
* @returns matching CRS, or an invalid CRS if string could not be matched
* @note added in QGIS 2.16
* @see QgsCoordinateReferenceSystem::createFromWkt()
*/
QgsCoordinateReferenceSystem crsByWkt( const QString& wkt ) 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
Expand Down
3 changes: 2 additions & 1 deletion src/app/ogr/qgsvectorlayersaveasdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
* *
***************************************************************************/
#include "qgslogger.h"
#include "qgscrscache.h"
#include "qgsvectorlayersaveasdialog.h"
#include "qgsgenericprojectionselector.h"
#include "qgsvectordataprovider.h"
Expand Down Expand Up @@ -101,7 +102,7 @@ void QgsVectorLayerSaveAsDialog::setup()
idx = 0;
}

QgsCoordinateReferenceSystem srs( mCRS, QgsCoordinateReferenceSystem::InternalCrsId );
QgsCoordinateReferenceSystem srs = QgsCRSCache::instance()->crsBySrsId( mCRS );
mCrsSelector->setCrs( srs );
mCrsSelector->setLayerCrs( srs );
mCrsSelector->dialog()->setMessage( tr( "Select the coordinate reference system for the vector file. "
Expand Down
5 changes: 3 additions & 2 deletions src/app/openstreetmap/qgsosmdownloaddialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

#include "qgis.h"
#include "qgisapp.h"
#include "qgscrscache.h"
#include "qgsmapcanvas.h"
#include "qgsmaplayer.h"
#include "qgsmaplayerregistry.h"
Expand Down Expand Up @@ -103,7 +104,7 @@ void QgsOSMDownloadDialog::onExtentCanvas()

if ( QgisApp::instance()->mapCanvas()->hasCrsTransformEnabled() )
{
QgsCoordinateReferenceSystem dst( GEOCRS_ID, QgsCoordinateReferenceSystem::InternalCrsId );
QgsCoordinateReferenceSystem dst = QgsCRSCache::instance()->crsBySrsId( GEOCRS_ID );

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

QgsCoordinateReferenceSystem dst( GEOCRS_ID, QgsCoordinateReferenceSystem::InternalCrsId );
QgsCoordinateReferenceSystem dst = QgsCRSCache::instance()->crsBySrsId( GEOCRS_ID );

QgsCoordinateTransform ct( layer->crs(), dst );
QgsRectangle rect( ct.transformBoundingBox( layer->extent() ) );
Expand Down
6 changes: 3 additions & 3 deletions src/app/qgisapp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6166,7 +6166,7 @@ void QgisApp::saveAsVectorFileGeneral( QgsVectorLayer* vlayer, bool symbologyOpt
QgsWKBTypes::Type forcedGeometryType = dialog->geometryType();

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

if ( destCRS.isValid() && destCRS != vlayer->crs() )
{
Expand Down Expand Up @@ -8504,7 +8504,7 @@ void QgisApp::setLayerCRS()
return;
}

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

Q_FOREACH ( QgsLayerTreeNode* node, mLayerTreeView->selectedNodes() )
{
Expand Down Expand Up @@ -8661,7 +8661,7 @@ void QgisApp::legendGroupSetCRS()
return;
}

QgsCoordinateReferenceSystem crs( mySelector.selectedCrsId(), QgsCoordinateReferenceSystem::InternalCrsId );
QgsCoordinateReferenceSystem crs = QgsCRSCache::instance()->crsBySrsId( mySelector.selectedCrsId() );
Q_FOREACH ( QgsLayerTreeLayer* nodeLayer, currentGroup->findLayers() )
{
if ( nodeLayer->layer() )
Expand Down
3 changes: 2 additions & 1 deletion src/app/qgsbookmarks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "qgsmaprenderer.h"
#include "qgsproject.h"
#include "qgsmessagelog.h"
#include "qgscrscache.h"

#include "qgslogger.h"

Expand Down Expand Up @@ -232,7 +233,7 @@ void QgsBookmarks::zoomToBookmark()
if ( srid > 0 &&
srid != QgisApp::instance()->mapCanvas()->mapSettings().destinationCrs().srsid() )
{
QgsCoordinateTransform ct( QgsCoordinateReferenceSystem( srid, QgsCoordinateReferenceSystem::InternalCrsId ),
QgsCoordinateTransform ct( QgsCRSCache::instance()->crsBySrsId( srid ),
QgisApp::instance()->mapCanvas()->mapSettings().destinationCrs() );
rect = ct.transform( rect );
if ( rect.isEmpty() )
Expand Down
9 changes: 5 additions & 4 deletions src/app/qgsprojectproperties.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "qgscomposer.h"
#include "qgscontexthelp.h"
#include "qgscoordinatetransform.h"
#include "qgscrscache.h"
#include "qgslogger.h"
#include "qgsmapcanvas.h"
#include "qgsmaplayer.h"
Expand Down Expand Up @@ -129,7 +130,7 @@ QgsProjectProperties::QgsProjectProperties( QgsMapCanvas* mapCanvas, QWidget *pa
// slot triggered by setChecked() might use it.
mProjectSrsId = mMapCanvas->mapSettings().destinationCrs().srsid();

QgsCoordinateReferenceSystem srs( mProjectSrsId, QgsCoordinateReferenceSystem::InternalCrsId );
QgsCoordinateReferenceSystem srs = QgsCRSCache::instance()->crsBySrsId( mProjectSrsId );
updateGuiForMapUnits( srs.mapUnits() );

QgsDebugMsg( "Read project CRSID: " + QString::number( mProjectSrsId ) );
Expand Down Expand Up @@ -774,7 +775,7 @@ void QgsProjectProperties::apply()
long myCRSID = projectionSelector->selectedCrsId();
if ( myCRSID )
{
QgsCoordinateReferenceSystem srs( myCRSID, QgsCoordinateReferenceSystem::InternalCrsId );
QgsCoordinateReferenceSystem srs = QgsCRSCache::instance()->crsBySrsId( myCRSID );
mMapCanvas->setDestinationCrs( srs );
QgsDebugMsg( QString( "Selected CRS " ) + srs.description() );
// write the currently selected projections _proj string_ to project settings
Expand Down Expand Up @@ -1302,7 +1303,7 @@ void QgsProjectProperties::srIdUpdated()
if ( !isProjected() || !myCRSID )
return;

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

Expand Down Expand Up @@ -1395,7 +1396,7 @@ void QgsProjectProperties::on_pbnWMSSetUsedSRS_clicked()

if ( cbxProjectionEnabled->isChecked() )
{
QgsCoordinateReferenceSystem srs( projectionSelector->selectedCrsId(), QgsCoordinateReferenceSystem::InternalCrsId );
QgsCoordinateReferenceSystem srs = QgsCRSCache::instance()->crsBySrsId( projectionSelector->selectedCrsId() );
crsList << srs.authid();
}

Expand Down
3 changes: 2 additions & 1 deletion src/browser/qgsbrowser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "qgsdataitem.h"
#include "qgsbrowser.h"
#include "qgsbrowsermodel.h"
#include "qgscrscache.h"
#include "qgsencodingfiledialog.h"
#include "qgsgenericprojectionselector.h"
#include "qgslogger.h"
Expand Down Expand Up @@ -302,7 +303,7 @@ void QgsBrowser::on_mActionSetProjection_triggered()
mySelector->setSelectedCrsId( mLayer->crs().srsid() );
if ( mySelector->exec() )
{
QgsCoordinateReferenceSystem srs( mySelector->selectedCrsId(), QgsCoordinateReferenceSystem::InternalCrsId );
QgsCoordinateReferenceSystem srs = QgsCRSCache::instance()->crsBySrsId( mySelector->selectedCrsId() );

// TODO: open data source in write mode set crs and save
#if 0
Expand Down
1 change: 1 addition & 0 deletions src/core/qgscoordinatereferencesystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ class CORE_EXPORT QgsCoordinateReferenceSystem
* @note SRID and EpsgCrsId may be blank if no match can be found on SRS db.
* @param theWkt The WKT for the desired spatial reference system.
* @return bool TRUE if success else false
* @note this method is expensive. Consider using QgsCRSCache::crsByWkt() instead.
*/
bool createFromWkt( const QString &theWkt );

Expand Down
10 changes: 5 additions & 5 deletions src/core/qgscoordinatetransform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ QgsCoordinateTransform::QgsCoordinateTransform( const QgsCoordinateReferenceSyst
QgsCoordinateTransform::QgsCoordinateTransform( long theSourceSrsId, long theDestSrsId )
: QObject()
, mInitialisedFlag( false )
, mSourceCRS( theSourceSrsId, QgsCoordinateReferenceSystem::InternalCrsId )
, mDestCRS( theDestSrsId, QgsCoordinateReferenceSystem::InternalCrsId )
, mSourceCRS( QgsCRSCache::instance()->crsBySrsId( theSourceSrsId ) )
, mDestCRS( QgsCRSCache::instance()->crsBySrsId( theDestSrsId ) )
, mSourceProjection( nullptr )
, mDestinationProjection( nullptr )
, mSourceDatumTransform( -1 )
Expand All @@ -86,8 +86,8 @@ QgsCoordinateTransform::QgsCoordinateTransform( const QString& theSourceCRS, con
, mDestinationDatumTransform( -1 )
{
setFinder();
mSourceCRS.createFromWkt( theSourceCRS );
mDestCRS.createFromWkt( theDestCRS );
mSourceCRS = QgsCRSCache::instance()->crsByWkt( theSourceCRS );
mDestCRS = QgsCRSCache::instance()->crsByWkt( theDestCRS );
// initialize the coordinate system data structures
//XXX Who spells initialize initialise?
//XXX A: Its the queen's english....
Expand All @@ -108,7 +108,7 @@ QgsCoordinateTransform::QgsCoordinateTransform( long theSourceSrid,
setFinder();

mSourceCRS.createFromId( theSourceSrid, theSourceCRSType );
mDestCRS.createFromWkt( theDestWkt );
mDestCRS = QgsCRSCache::instance()->crsByWkt( theDestWkt );
// initialize the coordinate system data structures
//XXX Who spells initialize initialise?
//XXX A: Its the queen's english....
Expand Down
30 changes: 30 additions & 0 deletions src/core/qgscrscache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,36 @@ QgsCoordinateReferenceSystem QgsCRSCache::crsByProj4( const QString& proj4 ) con
}
}

QgsCoordinateReferenceSystem QgsCRSCache::crsByWkt( const QString& wkt ) const
{
mCRSWktLock.lockForRead();
QHash< QString, QgsCoordinateReferenceSystem >::const_iterator crsIt = mCRSWkt.constFind( wkt );
if ( crsIt == mCRSWkt.constEnd() )
{
mCRSWktLock.unlock();
mCRSWktLock.lockForWrite();
QgsCoordinateReferenceSystem s;
if ( ! s.createFromWkt( wkt ) )
{
QgsCoordinateReferenceSystem result = mCRSWkt.insert( wkt, mInvalidCRS ).value();
mCRSWktLock.unlock();
return result;
}
else
{
QgsCoordinateReferenceSystem result = mCRSWkt.insert( wkt, s ).value();
mCRSWktLock.unlock();
return result;
}
}
else
{
QgsCoordinateReferenceSystem result = crsIt.value();
mCRSWktLock.unlock();
return result;
}
}

QgsCoordinateReferenceSystem QgsCRSCache::crsBySrsId( long srsId ) const
{
mCRSSrsIdLock.lockForRead();
Expand Down
10 changes: 10 additions & 0 deletions src/core/qgscrscache.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,14 @@ class CORE_EXPORT QgsCRSCache
*/
QgsCoordinateReferenceSystem crsByProj4( const QString& proj4 ) const;

/** Returns the CRS from a WKT spatial ref sys definition string.
* @param wkt WKT for the desired spatial reference system.
* @returns matching CRS, or an invalid CRS if string could not be matched
* @note added in QGIS 2.16
* @see QgsCoordinateReferenceSystem::createFromWkt()
*/
QgsCoordinateReferenceSystem crsByWkt( const QString& wkt ) 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
Expand All @@ -121,6 +129,8 @@ class CORE_EXPORT QgsCRSCache
mutable QHash< QString, QgsCoordinateReferenceSystem > mCRS;
mutable QReadWriteLock mCRSProj4Lock;
mutable QHash< QString, QgsCoordinateReferenceSystem > mCRSProj4;
mutable QReadWriteLock mCRSWktLock;
mutable QHash< QString, QgsCoordinateReferenceSystem > mCRSWkt;
mutable QReadWriteLock mCRSSrsIdLock;
mutable QHash< long, QgsCoordinateReferenceSystem > mCRSSrsId;

Expand Down
2 changes: 1 addition & 1 deletion src/core/qgsmaprenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ QgsMapRenderer::QgsMapRenderer()
mSize = QSize( 0, 0 );

mProjectionsEnabled = false;
mDestCRS = new QgsCoordinateReferenceSystem( GEOCRS_ID, QgsCoordinateReferenceSystem::InternalCrsId ); //WGS 84
mDestCRS = new QgsCoordinateReferenceSystem( QgsCRSCache::instance()->crsBySrsId( GEOCRS_ID ) ); //WGS 84

mOutputUnits = QgsMapRenderer::Millimeters;
mFullExtent.setMinimal();
Expand Down
2 changes: 1 addition & 1 deletion src/core/qgsmapsettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ QgsMapSettings::QgsMapSettings()
, mRotation( 0.0 )
, mMagnificationFactor( 1.0 )
, mProjectionsEnabled( false )
, mDestCRS( GEOCRS_ID, QgsCoordinateReferenceSystem::InternalCrsId ) // WGS 84
, mDestCRS( QgsCRSCache::instance()->crsBySrsId( GEOCRS_ID ) ) // WGS 84
, mDatumTransformStore( mDestCRS )
, mBackgroundColor( Qt::white )
, mSelectionColor( Qt::yellow )
Expand Down
2 changes: 1 addition & 1 deletion src/gui/qgsnewgeopackagelayerdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ bool QgsNewGeoPackageLayerDialog::apply()
int crsId = mCrsSelector->crs().srsid();
if ( wkbType != wkbNone && crsId > 0 )
{
QgsCoordinateReferenceSystem srs( crsId, QgsCoordinateReferenceSystem::InternalCrsId );
QgsCoordinateReferenceSystem srs = QgsCRSCache::instance()->crsBySrsId( crsId );
QString srsWkt = srs.toWkt();
hSRS = OSRNewSpatialReference( srsWkt.toLocal8Bit().data() );
}
Expand Down
2 changes: 1 addition & 1 deletion src/gui/qgsnewvectorlayerdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ QString QgsNewVectorLayerDialog::runAndCreateLayer( QWidget* parent, QString* pE
{
if ( geometrytype != QGis::WKBUnknown )
{
QgsCoordinateReferenceSystem srs( crsId, QgsCoordinateReferenceSystem::InternalCrsId );
QgsCoordinateReferenceSystem srs = QgsCRSCache::instance()->crsBySrsId( crsId );
if ( !createEmptyDataSource( fileName, fileformat, enc, geometrytype, attributes, &srs ) )
{
return QString::null;
Expand Down
2 changes: 1 addition & 1 deletion src/gui/qgsowssourceselect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ QgsOWSSourceSelect::QgsOWSSourceSelect( const QString& service, QWidget * parent
if ( currentCRS != -1 )
{
//convert CRS id to epsg
QgsCoordinateReferenceSystem currentRefSys( currentCRS, QgsCoordinateReferenceSystem::InternalCrsId );
QgsCoordinateReferenceSystem currentRefSys = QgsCRSCache::instance()->crsBySrsId( currentCRS );
if ( currentRefSys.isValid() )
{
mSelectedCRS = currentRefSys.authid();
Expand Down
2 changes: 1 addition & 1 deletion src/gui/qgssourceselectdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ QString QgsSourceSelectDialog::getPreferredCrs( const QSet<QString>& crsSet ) co
//first: project CRS
long ProjectCRSID = QgsProject::instance()->readNumEntry( "SpatialRefSys", "/ProjectCRSID", -1 );
//convert to EPSG
QgsCoordinateReferenceSystem projectRefSys( ProjectCRSID, QgsCoordinateReferenceSystem::InternalCrsId );
QgsCoordinateReferenceSystem projectRefSys = QgsCRSCache::instance()->crsBySrsId( ProjectCRSID );
QString ProjectCRS;
if ( projectRefSys.isValid() )
{
Expand Down
11 changes: 5 additions & 6 deletions src/plugins/grass/qgsgrassnewmapset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "qgisinterface.h"
#include "qgsapplication.h"
#include "qgscoordinatetransform.h"
#include "qgscrscache.h"
#include "qgslogger.h"
#include "qgsmapcanvas.h"
#include "qgsproject.h"
Expand Down Expand Up @@ -814,16 +815,15 @@ void QgsGrassNewMapset::setSelectedRegion()
{
// Warning: QgsCoordinateReferenceSystem::EpsgCrsId is broken (using epsg_id)
//QgsCoordinateReferenceSystem source ( 4326, QgsCoordinateReferenceSystem::EpsgCrsId );
QgsCoordinateReferenceSystem source( GEOCRS_ID, QgsCoordinateReferenceSystem::InternalCrsId );
QgsCoordinateReferenceSystem source = QgsCRSCache::instance()->crsBySrsId( GEOCRS_ID );

if ( !source.isValid() )
{
QgsGrass::warning( tr( "Cannot create QgsCoordinateReferenceSystem" ) );
return;
}

QgsCoordinateReferenceSystem dest( mProjectionSelector->selectedCrsId(),
QgsCoordinateReferenceSystem::InternalCrsId );
QgsCoordinateReferenceSystem dest = QgsCRSCache::instance()->crsBySrsId( mProjectionSelector->selectedCrsId() );

if ( !dest.isValid() )
{
Expand Down Expand Up @@ -1023,16 +1023,15 @@ void QgsGrassNewMapset::drawRegion()
// Warning: seems that crashes if source == dest
if ( mProjectionSelector->selectedCrsId() != GEOCRS_ID )
{
QgsCoordinateReferenceSystem source( mProjectionSelector->selectedCrsId(),
QgsCoordinateReferenceSystem::InternalCrsId );
QgsCoordinateReferenceSystem source = QgsCRSCache::instance()->crsBySrsId( mProjectionSelector->selectedCrsId() );

if ( !source.isValid() )
{
QgsGrass::warning( tr( "Cannot create QgsCoordinateReferenceSystem" ) );
return;
}

QgsCoordinateReferenceSystem dest( GEOCRS_ID, QgsCoordinateReferenceSystem::InternalCrsId );
QgsCoordinateReferenceSystem dest = QgsCRSCache::instance()->crsBySrsId( GEOCRS_ID );

if ( !dest.isValid() )
{
Expand Down
7 changes: 5 additions & 2 deletions src/providers/db2/qgsdb2provider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <qgscoordinatereferencesystem.h>
#include <qgsdataitem.h>
#include <qgslogger.h>
#include "qgscrscache.h"
#include "qgscredentials.h"

static const QString PROVIDER_KEY = "DB2";
Expand Down Expand Up @@ -514,9 +515,11 @@ QgsCoordinateReferenceSystem QgsDb2Provider::crs()
bool execOk = query.exec( QString( "SELECT DEFINITION FROM DB2GSE.ST_SPATIAL_REFERENCE_SYSTEMS WHERE SRS_ID = %1" ).arg( QString::number( mSRId ) ) );
if ( execOk && query.isActive() )
{
if ( query.next() && mCrs.createFromWkt( query.value( 0 ).toString() ) )
if ( query.next() )
{
return mCrs;
mCrs = QgsCRSCache::instance()->crsByWkt( query.value( 0 ).toString() );
if ( mCrs.isValid() )
return mCrs;
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/providers/gdal/qgsgdalprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ bool QgsGdalProvider::crsFromWkt( const char *wkt )
OGRFree( pszWkt );

// create CRS from Wkt
mCrs.createFromWkt( myWktString );
mCrs = QgsCRSCache::instance()->crsByWkt( myWktString );
}
}

Expand Down
Loading

0 comments on commit fd88d91

Please sign in to comment.