Skip to content

Commit

Permalink
Use QgsCRSCache in more places
Browse files Browse the repository at this point in the history
Forward port from sourcepole/kadas-albireo
  • Loading branch information
nyalldawson committed Jun 6, 2016
1 parent c61daf8 commit 768fc2c
Show file tree
Hide file tree
Showing 48 changed files with 266 additions and 164 deletions.
9 changes: 4 additions & 5 deletions python/core/qgscoordinatereferencesystem.sip
Expand Up @@ -33,7 +33,7 @@ class QgsCoordinateReferenceSystem
/** Use this constructor when you want to create a CRS object using
* a postgis SRID, an EpsgCrsId id or a QGIS CRS_ID.
* @note We encourage you to use EpsgCrsId, WKT or Proj4 to describe CRS's in your code
* wherever possible. QGSI CRS_IDs are not guaranteed to be permanent / involatile.
* wherever possible. QGIS CRS_IDs are not guaranteed to be permanent / involatile.
* @param theId The ID no valid for the chosen coordinate system id type
* @param theType One of the types described in QgsCoordinateReferenceSystem::CrsType
*/
Expand All @@ -44,11 +44,9 @@ class QgsCoordinateReferenceSystem
bool createFromId( const long theId, CrsType theType = PostgisCrsId );

/**
* \brief Set up this CRS from the given OGC CRS
*
* Sets this CRS to the given OGC WMS-format Coordinate Reference Systems.
*
* \retval false if not given an valid label
* @returns false if not given an valid label
* @note this method is expensive. Consider using QgsCRSCache::crsByOgcWmsCrs() instead.
*/
bool createFromOgcWmsCrs( QString theCrs );

Expand Down Expand Up @@ -107,6 +105,7 @@ class QgsCoordinateReferenceSystem
*
* @param theProjString A proj4 format string
* @return bool TRUE if success else false
* @note this method is expensive. Consider using QgsCRSCache::crsByProj4() instead.
*/
bool createFromProj4( const QString &theProjString );

Expand Down
47 changes: 42 additions & 5 deletions python/core/qgscrscache.sip
Expand Up @@ -22,20 +22,57 @@ class QgsCoordinateTransformCache
QgsCoordinateTransformCache( const QgsCoordinateTransformCache& rh );
};

/** \ingroup core
* \class QgsCRSCache
* \brief Caches QgsCoordinateReferenceSystem construction, which may be expensive.
*
* QgsCRSCache maintains a cache of previously constructed coordinate systems, so that
* creating a new CRS from the cache can reuse previously calculated parameters. The
* constructors for QgsCoordinateReferenceSystem can be expensive, so it's recommended
* to use QgsCRSCache instead of directly calling the QgsCoordinateReferenceSystem
* constructors.
*/

class QgsCRSCache
{
%TypeHeaderCode
#include <qgscrscache.h>
%End

public:
static QgsCRSCache* instance();

/** Returns the CRS for authid, e.g. 'EPSG:4326' (or an invalid CRS in case of error)*/
const QgsCoordinateReferenceSystem& crsByAuthId( const QString& authid );
const QgsCoordinateReferenceSystem& crsByEpsgId( long epsg );
/** Returns the CRS for authid, e.g. 'EPSG:4326' (or an invalid CRS in case of error)
* @deprecated use crsByOgcWmsCrs() instead
*/
QgsCoordinateReferenceSystem crsByAuthId( const QString& authid ) /Deprecated/;

/** Returns the CRS from a given OGC WMS-format Coordinate Reference System string.
* @param ogcCrs OGR compliant CRS definition, eg "EPSG:4326"
* @returns matching CRS, or an invalid CRS if string could not be matched
* @note added in QGIS 2.16
* @see QgsCoordinateReferenceSystem::createFromOgcWmsCrs()
*/
QgsCoordinateReferenceSystem crsByOgcWmsCrs( const QString& ogcCrs ) const;

/** Returns the CRS from a given EPSG ID.
* @param epsg epsg CRS ID
* @returns matching CRS, or an invalid CRS if string could not be matched
*/
QgsCoordinateReferenceSystem crsByEpsgId( long epsg ) const;

void updateCRSCache( const QString &authid );
/** Returns the CRS from a proj4 style formatted string.
* @param proj4 proj4 format string
* @returns matching CRS, or an invalid CRS if string could not be matched
* @note added in QGIS 2.16
* @see QgsCoordinateReferenceSystem::createFromProj4()
*/
QgsCoordinateReferenceSystem crsByProj4( const QString& proj4 ) 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"
*/
void updateCRSCache( const QString& authid );

protected:
QgsCRSCache();
Expand Down
4 changes: 2 additions & 2 deletions src/app/gps/qgsgpsinformationwidget.cpp
Expand Up @@ -36,7 +36,7 @@
#include "qgsvectordataprovider.h"
#include "qgsvectorlayer.h"
#include "qgswkbptr.h"

#include "qgscrscache.h"

// QWT Charting widget

Expand Down Expand Up @@ -230,7 +230,7 @@ QgsGPSInformationWidget::QgsGPSInformationWidget( QgsMapCanvas * thepCanvas, QWi
radRecenterWhenNeeded->setChecked( true );
}

mWgs84CRS.createFromOgcWmsCrs( "EPSG:4326" );
mWgs84CRS = QgsCRSCache::instance()->crsByOgcWmsCrs( "EPSG:4326" );

mBtnDebug->setVisible( mySettings.value( "/gps/showDebug", "false" ).toBool() ); // use a registry setting to control - power users/devs could set it

Expand Down
4 changes: 2 additions & 2 deletions src/app/gps/qgsgpsmarker.cpp
Expand Up @@ -19,13 +19,13 @@
#include "qgscoordinatetransform.h"
#include "qgsmapcanvas.h"
#include "qgsmaprenderer.h"

#include "qgscrscache.h"

QgsGpsMarker::QgsGpsMarker( QgsMapCanvas* mapCanvas )
: QgsMapCanvasItem( mapCanvas )
{
mSize = 16;
mWgs84CRS.createFromOgcWmsCrs( "EPSG:4326" );
mWgs84CRS = QgsCRSCache::instance()->crsByOgcWmsCrs( "EPSG:4326" );
mSvg.load( QString( ":/images/north_arrows/gpsarrow2.svg" ) );
if ( ! mSvg.isValid() )
{
Expand Down
8 changes: 4 additions & 4 deletions src/app/qgisapp.cpp
Expand Up @@ -127,6 +127,7 @@
#include "qgscoordinatetransform.h"
#include "qgscoordinateutils.h"
#include "qgscredentialdialog.h"
#include "qgscrscache.h"
#include "qgscursors.h"
#include "qgscustomization.h"
#include "qgscustomlayerorderwidget.h"
Expand Down Expand Up @@ -494,8 +495,8 @@ void QgisApp::validateSrs( QgsCoordinateReferenceSystem &srs )
if ( authid.isNull() )
authid = QgisApp::instance()->mapCanvas()->mapSettings().destinationCrs().authid();

QgsCoordinateReferenceSystem defaultCrs;
if ( defaultCrs.createFromOgcWmsCrs( authid ) )
QgsCoordinateReferenceSystem defaultCrs = QgsCRSCache::instance()->crsByOgcWmsCrs( authid );
if ( defaultCrs.isValid() )
{
mySelector->setSelectedCrsId( defaultCrs.srsid() );
}
Expand Down Expand Up @@ -4379,8 +4380,7 @@ void QgisApp::fileNew( bool thePromptToSaveFlag, bool forceBlank )

// set project CRS
QString defCrs = settings.value( "/Projections/projectDefaultCrs", GEO_EPSG_CRS_AUTHID ).toString();
QgsCoordinateReferenceSystem srs;
srs.createFromOgcWmsCrs( defCrs );
QgsCoordinateReferenceSystem srs = QgsCRSCache::instance()->crsByOgcWmsCrs( defCrs );
mMapCanvas->setDestinationCrs( srs );
// write the projections _proj string_ to project settings
prj->writeEntry( "SpatialRefSys", "/ProjectCRSProj4String", srs.toProj4() );
Expand Down
4 changes: 2 additions & 2 deletions src/app/qgsdecorationnortharrow.cpp
Expand Up @@ -31,6 +31,7 @@ email : tim@linfiniti.com
#include "qgslogger.h"
#include "qgsmapcanvas.h"
#include "qgsmaprenderer.h"
#include "qgscrscache.h"

// qt includes
#include <QPainter>
Expand Down Expand Up @@ -230,8 +231,7 @@ bool QgsDecorationNorthArrow::calculateNorthDirection()
if ( outputCRS.isValid() && !outputCRS.geographicFlag() )
{
// Use a geographic CRS to get lat/long to work out direction
QgsCoordinateReferenceSystem ourCRS;
ourCRS.createFromOgcWmsCrs( GEO_EPSG_CRS_AUTHID );
QgsCoordinateReferenceSystem ourCRS = QgsCRSCache::instance()->crsByOgcWmsCrs( GEO_EPSG_CRS_AUTHID );
assert( ourCRS.isValid() );

QgsCoordinateTransform transform( outputCRS, ourCRS );
Expand Down
7 changes: 3 additions & 4 deletions src/app/qgsnewspatialitelayerdialog.cpp
Expand Up @@ -29,6 +29,7 @@
#include "qgscoordinatereferencesystem.h"
#include "qgsgenericprojectionselector.h"
#include "qgsslconnect.h"
#include "qgscrscache.h"

#include "qgslogger.h"

Expand Down Expand Up @@ -74,8 +75,7 @@ QgsNewSpatialiteLayerDialog::QgsNewSpatialiteLayerDialog( QWidget *parent, Qt::W
mOkButton->setEnabled( false );

// Set the SRID box to a default of WGS84
QgsCoordinateReferenceSystem srs;
srs.createFromOgcWmsCrs( settings.value( "/Projections/layerDefaultCrs", GEO_EPSG_CRS_AUTHID ).toString() );
QgsCoordinateReferenceSystem srs = QgsCRSCache::instance()->crsByOgcWmsCrs( settings.value( "/Projections/layerDefaultCrs", GEO_EPSG_CRS_AUTHID ).toString() );
srs.validate();
mCrsId = srs.authid();
leSRID->setText( srs.authid() + " - " + srs.description() );
Expand Down Expand Up @@ -244,8 +244,7 @@ void QgsNewSpatialiteLayerDialog::on_pbnFindSRID_clicked()

if ( mySelector->exec() )
{
QgsCoordinateReferenceSystem srs;
srs.createFromOgcWmsCrs( mySelector->selectedAuthId() );
QgsCoordinateReferenceSystem srs = QgsCRSCache::instance()->crsByOgcWmsCrs( mySelector->selectedAuthId() );
QString crsId = srs.authid();
if ( crsId != mCrsId )
{
Expand Down
5 changes: 3 additions & 2 deletions src/app/qgsoptions.cpp
Expand Up @@ -31,6 +31,7 @@
#include "qgsnetworkaccessmanager.h"
#include "qgsproject.h"
#include "qgsdualview.h"
#include "qgscrscache.h"

#include "qgsattributetablefiltermodel.h"
#include "qgsrasterformatsaveoptionswidget.h"
Expand Down Expand Up @@ -394,7 +395,7 @@ QgsOptions::QgsOptions( QWidget *parent, Qt::WindowFlags fl )
radUseGlobalProjection->setChecked( true );
}
QString myLayerDefaultCrs = mSettings->value( "/Projections/layerDefaultCrs", GEO_EPSG_CRS_AUTHID ).toString();
mLayerDefaultCrs.createFromOgcWmsCrs( myLayerDefaultCrs );
mLayerDefaultCrs = QgsCRSCache::instance()->crsByOgcWmsCrs( myLayerDefaultCrs );
leLayerGlobalCrs->setCrs( mLayerDefaultCrs );

//on the fly CRS transformation settings
Expand All @@ -413,7 +414,7 @@ QgsOptions::QgsOptions( QWidget *parent, Qt::WindowFlags fl )
}

QString myDefaultCrs = mSettings->value( "/Projections/projectDefaultCrs", GEO_EPSG_CRS_AUTHID ).toString();
mDefaultCrs.createFromOgcWmsCrs( myDefaultCrs );
mDefaultCrs = QgsCRSCache::instance()->crsByOgcWmsCrs( myDefaultCrs );
leProjectGlobalCrs->setCrs( mDefaultCrs );
leProjectGlobalCrs->setOptionVisible( QgsProjectionSelectionWidget::DefaultCrs, false );

Expand Down
4 changes: 2 additions & 2 deletions src/app/qgswelcomepageitemsmodel.cpp
Expand Up @@ -16,6 +16,7 @@
#include "qgswelcomepageitemsmodel.h"
#include "qgscoordinatereferencesystem.h"
#include "qgsmessagelog.h"
#include "qgscrscache.h"

#include <QApplication>
#include <QAbstractTextDocumentLayout>
Expand Down Expand Up @@ -149,8 +150,7 @@ QVariant QgsWelcomePageItemsModel::data( const QModelIndex& index, int role ) co
case CrsRole:
if ( mRecentProjects.at( index.row() ).crs != "" )
{
QgsCoordinateReferenceSystem crs;
crs.createFromOgcWmsCrs( mRecentProjects.at( index.row() ).crs );
QgsCoordinateReferenceSystem crs = QgsCRSCache::instance()->crsByOgcWmsCrs( mRecentProjects.at( index.row() ).crs );
return QString( "%1 (%2)" ).arg( mRecentProjects.at( index.row() ).crs, crs.description() );
}
else
Expand Down
4 changes: 2 additions & 2 deletions src/core/qgscoordinatereferencesystem.cpp
Expand Up @@ -263,7 +263,7 @@ void QgsCoordinateReferenceSystem::validate()

if ( !d->mIsValid )
{
*this = QgsCRSCache::instance()->crsByAuthId( GEO_EPSG_CRS_AUTHID );
*this = QgsCRSCache::instance()->crsByOgcWmsCrs( GEO_EPSG_CRS_AUTHID );
}
}

Expand Down Expand Up @@ -1142,7 +1142,7 @@ bool QgsCoordinateReferenceSystem::readXML( const QDomNode & theNode )
myNode = srsNode.namedItem( "authid" );
if ( !myNode.isNull() )
{
operator=( QgsCRSCache::instance()->crsByAuthId( myNode.toElement().text() ) );
operator=( QgsCRSCache::instance()->crsByOgcWmsCrs( myNode.toElement().text() ) );
if ( isValid() )
{
initialized = true;
Expand Down
9 changes: 4 additions & 5 deletions src/core/qgscoordinatereferencesystem.h
Expand Up @@ -74,7 +74,7 @@ class CORE_EXPORT QgsCoordinateReferenceSystem
/** Use this constructor when you want to create a CRS object using
* a postgis SRID, an EpsgCrsId id or a QGIS CRS_ID.
* @note We encourage you to use EpsgCrsId, WKT or Proj4 to describe CRS's in your code
* wherever possible. QGSI CRS_IDs are not guaranteed to be permanent / involatile.
* wherever possible. QGIS CRS_IDs are not guaranteed to be permanent / involatile.
* @param theId The ID no valid for the chosen coordinate system id type
* @param theType One of the types described in QgsCoordinateReferenceSystem::CrsType
*/
Expand All @@ -91,11 +91,9 @@ class CORE_EXPORT QgsCoordinateReferenceSystem
bool createFromId( const long theId, CrsType theType = PostgisCrsId );

/**
* \brief Set up this CRS from the given OGC CRS
*
* Sets this CRS to the given OGC WMS-format Coordinate Reference Systems.
*
* \retval false if not given an valid label
* @returns false if not given an valid label
* @note this method is expensive. Consider using QgsCRSCache::crsByOgcWmsCrs() instead.
*/
bool createFromOgcWmsCrs( const QString& theCrs );

Expand Down Expand Up @@ -154,6 +152,7 @@ class CORE_EXPORT QgsCoordinateReferenceSystem
*
* @param theProjString A proj4 format string
* @return bool TRUE if success else false
* @note this method is expensive. Consider using QgsCRSCache::crsByProj4() instead.
*/
bool createFromProj4( const QString &theProjString );

Expand Down
8 changes: 3 additions & 5 deletions src/core/qgscoordinatetransform.cpp
Expand Up @@ -175,7 +175,7 @@ void QgsCoordinateTransform::initialise()
{
//No destination projection is set so we set the default output projection to
//be the same as input proj.
mDestCRS = QgsCRSCache::instance()->crsByAuthId( mSourceCRS.authid() );
mDestCRS = QgsCRSCache::instance()->crsByOgcWmsCrs( mSourceCRS.authid() );
}

bool useDefaultDatumTransform = ( mSourceDatumTransform == - 1 && mDestinationDatumTransform == -1 );
Expand Down Expand Up @@ -1022,11 +1022,9 @@ bool QgsCoordinateTransform::datumTransformCrsInfo( int datumTransform, int& eps
preferred = sqlite3_column_int( stmt, 5 ) != 0;
deprecated = sqlite3_column_int( stmt, 6 ) != 0;

QgsCoordinateReferenceSystem srcCrs;
srcCrs.createFromOgcWmsCrs( QString( "EPSG:%1" ).arg( srcCrsId ) );
QgsCoordinateReferenceSystem srcCrs = QgsCRSCache::instance()->crsByOgcWmsCrs( QString( "EPSG:%1" ).arg( srcCrsId ) );
srcProjection = srcCrs.description();
QgsCoordinateReferenceSystem destCrs;
destCrs.createFromOgcWmsCrs( QString( "EPSG:%1" ).arg( destCrsId ) );
QgsCoordinateReferenceSystem destCrs = QgsCRSCache::instance()->crsByOgcWmsCrs( QString( "EPSG:%1" ).arg( destCrsId ) );
dstProjection = destCrs.description();

sqlite3_finalize( stmt );
Expand Down
41 changes: 32 additions & 9 deletions src/core/qgscrscache.cpp
Expand Up @@ -55,8 +55,8 @@ const QgsCoordinateTransform* QgsCoordinateTransformCache::transform( const QStr
}

//not found, insert new value
const QgsCoordinateReferenceSystem& srcCrs = QgsCRSCache::instance()->crsByAuthId( srcAuthId );
const QgsCoordinateReferenceSystem& destCrs = QgsCRSCache::instance()->crsByAuthId( destAuthId );
QgsCoordinateReferenceSystem srcCrs = QgsCRSCache::instance()->crsByOgcWmsCrs( srcAuthId );
QgsCoordinateReferenceSystem destCrs = QgsCRSCache::instance()->crsByOgcWmsCrs( destAuthId );
QgsCoordinateTransform* ct = new QgsCoordinateTransform( srcCrs, destCrs );
ct->setSourceDatumTransform( srcDatumTransform );
ct->setDestinationDatumTransform( destDatumTransform );
Expand Down Expand Up @@ -113,25 +113,48 @@ void QgsCRSCache::updateCRSCache( const QString& authid )
QgsCoordinateTransformCache::instance()->invalidateCrs( authid );
}

const QgsCoordinateReferenceSystem& QgsCRSCache::crsByAuthId( const QString& authid )
QgsCoordinateReferenceSystem QgsCRSCache::crsByAuthId( const QString& authid )
{
QHash< QString, QgsCoordinateReferenceSystem >::const_iterator crsIt = mCRS.constFind( authid );
return crsByOgcWmsCrs( authid );
}

QgsCoordinateReferenceSystem QgsCRSCache::crsByOgcWmsCrs( const QString& ogcCrs ) const
{
QHash< QString, QgsCoordinateReferenceSystem >::const_iterator crsIt = mCRS.constFind( ogcCrs );
if ( crsIt == mCRS.constEnd() )
{
QgsCoordinateReferenceSystem s;
if ( ! s.createFromOgcWmsCrs( authid ) )
if ( ! s.createFromOgcWmsCrs( ogcCrs ) )
{
return mInvalidCRS;
return mCRS.insert( ogcCrs, mInvalidCRS ).value();
}
return mCRS.insert( authid, s ).value();
return mCRS.insert( ogcCrs, s ).value();
}
else
{
return crsIt.value();
}
}

const QgsCoordinateReferenceSystem& QgsCRSCache::crsByEpsgId( long epsg )
QgsCoordinateReferenceSystem QgsCRSCache::crsByEpsgId( long epsg ) const
{
return crsByOgcWmsCrs( "EPSG:" + QString::number( epsg ) );
}

QgsCoordinateReferenceSystem QgsCRSCache::crsByProj4( const QString& proj4 ) const
{
return crsByAuthId( "EPSG:" + QString::number( epsg ) );
QHash< QString, QgsCoordinateReferenceSystem >::const_iterator crsIt = mCRSProj4.find( proj4 );
if ( crsIt == mCRSProj4.constEnd() )
{
QgsCoordinateReferenceSystem s;
if ( ! s.createFromProj4( proj4 ) )
{
return mCRSProj4.insert( proj4, mInvalidCRS ).value();
}
return mCRSProj4.insert( proj4, s ).value();
}
else
{
return crsIt.value();
}
}

0 comments on commit 768fc2c

Please sign in to comment.