Skip to content
Permalink
Browse files

Use QgsCRSCache in more places

  • Loading branch information
nyalldawson committed Jul 5, 2016
1 parent 867dbe5 commit fd88d91bdebcd2b6f3172c395e0f105f0af8a436
@@ -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
@@ -16,6 +16,7 @@
* *
***************************************************************************/
#include "qgslogger.h"
#include "qgscrscache.h"
#include "qgsvectorlayersaveasdialog.h"
#include "qgsgenericprojectionselector.h"
#include "qgsvectordataprovider.h"
@@ -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. "
@@ -21,6 +21,7 @@

#include "qgis.h"
#include "qgisapp.h"
#include "qgscrscache.h"
#include "qgsmapcanvas.h"
#include "qgsmaplayer.h"
#include "qgsmaplayerregistry.h"
@@ -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 );
@@ -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() ) );
@@ -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() )
{
@@ -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() )
{
@@ -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() )
@@ -22,6 +22,7 @@
#include "qgsmaprenderer.h"
#include "qgsproject.h"
#include "qgsmessagelog.h"
#include "qgscrscache.h"

#include "qgslogger.h"

@@ -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() )
@@ -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"
@@ -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 ) );
@@ -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
@@ -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();

@@ -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();
}

@@ -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"
@@ -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
@@ -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 );

@@ -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 )
@@ -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....
@@ -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....
@@ -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();
@@ -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
@@ -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;

@@ -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();
@@ -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 )
@@ -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() );
}
@@ -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;
@@ -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();
@@ -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() )
{
@@ -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"
@@ -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() )
{
@@ -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() )
{
@@ -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";
@@ -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;
}
}
}
@@ -215,7 +215,7 @@ bool QgsGdalProvider::crsFromWkt( const char *wkt )
OGRFree( pszWkt );

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

0 comments on commit fd88d91

Please sign in to comment.
You can’t perform that action at this time.