Skip to content
Permalink
Browse files

Use QgsCRSCache in more places

Forward port from sourcepole/kadas-albireo
  • Loading branch information
nyalldawson committed Jun 6, 2016
1 parent c61daf8 commit 768fc2c79081a223ed6ed289c9615c13cb8e53f4
Showing with 266 additions and 164 deletions.
  1. +4 −5 python/core/qgscoordinatereferencesystem.sip
  2. +42 −5 python/core/qgscrscache.sip
  3. +2 −2 src/app/gps/qgsgpsinformationwidget.cpp
  4. +2 −2 src/app/gps/qgsgpsmarker.cpp
  5. +4 −4 src/app/qgisapp.cpp
  6. +2 −2 src/app/qgsdecorationnortharrow.cpp
  7. +3 −4 src/app/qgsnewspatialitelayerdialog.cpp
  8. +3 −2 src/app/qgsoptions.cpp
  9. +2 −2 src/app/qgswelcomepageitemsmodel.cpp
  10. +2 −2 src/core/qgscoordinatereferencesystem.cpp
  11. +4 −5 src/core/qgscoordinatereferencesystem.h
  12. +3 −5 src/core/qgscoordinatetransform.cpp
  13. +32 −9 src/core/qgscrscache.cpp
  14. +50 −5 src/core/qgscrscache.h
  15. +3 −4 src/core/qgsdistancearea.cpp
  16. +5 −4 src/core/qgsexpression.cpp
  17. +4 −3 src/core/qgsgml.cpp
  18. +7 −4 src/core/qgsogcutils.cpp
  19. +2 −2 src/gui/qgsmapcanvas.cpp
  20. +2 −2 src/gui/qgsnewgeopackagelayerdialog.cpp
  21. +2 −2 src/gui/qgsnewmemorylayerdialog.cpp
  22. +2 −2 src/gui/qgsnewvectorlayerdialog.cpp
  23. +4 −4 src/gui/qgsowssourceselect.cpp
  24. +4 −4 src/gui/qgsprojectionselectionwidget.cpp
  25. +2 −2 src/gui/qgssourceselectdialog.cpp
  26. +2 −2 src/plugins/georeferencer/qgstransformsettingsdialog.cpp
  27. +1 −1 src/providers/arcgisrest/qgsafsprovider.cpp
  28. +3 −3 src/providers/gdal/qgsgdalprovider.cpp
  29. +2 −1 src/providers/postgres/qgspostgresprovider.cpp
  30. +3 −3 src/providers/spatialite/qgsspatialiteprovider.cpp
  31. +3 −2 src/providers/wcs/qgswcscapabilities.cpp
  32. +2 −1 src/providers/wcs/qgswcsdataitems.cpp
  33. +6 −8 src/providers/wcs/qgswcsprovider.cpp
  34. +5 −5 src/providers/wfs/qgswfsprovider.cpp
  35. +2 −2 src/providers/wfs/qgswfssourceselect.cpp
  36. +10 −14 src/providers/wms/qgswmscapabilities.cpp
  37. +2 −1 src/providers/wms/qgswmsdataitems.cpp
  38. +10 −11 src/providers/wms/qgswmsprovider.cpp
  39. +4 −4 src/providers/wms/qgswmssourceselect.cpp
  40. +2 −2 src/server/qgsconfigparserutils.cpp
  41. +3 −2 src/server/qgshostedrdsbuilder.cpp
  42. +2 −2 src/server/qgshostedvdsbuilder.cpp
  43. +3 −3 src/server/qgsserverprojectparser.cpp
  44. +1 −1 src/server/qgsserverprojectparser.h
  45. +3 −4 src/server/qgssldconfigparser.cpp
  46. +2 −2 src/server/qgswcsserver.cpp
  47. +2 −2 src/server/qgswmsprojectparser.cpp
  48. +1 −1 src/server/qgswmsserver.cpp
@@ -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
*/
@@ -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 );

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

@@ -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();
@@ -36,7 +36,7 @@
#include "qgsvectordataprovider.h"
#include "qgsvectorlayer.h"
#include "qgswkbptr.h"

#include "qgscrscache.h"

// QWT Charting widget

@@ -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

@@ -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() )
{
@@ -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"
@@ -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() );
}
@@ -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() );
@@ -31,6 +31,7 @@ email : tim@linfiniti.com
#include "qgslogger.h"
#include "qgsmapcanvas.h"
#include "qgsmaprenderer.h"
#include "qgscrscache.h"

// qt includes
#include <QPainter>
@@ -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 );
@@ -29,6 +29,7 @@
#include "qgscoordinatereferencesystem.h"
#include "qgsgenericprojectionselector.h"
#include "qgsslconnect.h"
#include "qgscrscache.h"

#include "qgslogger.h"

@@ -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() );
@@ -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 )
{
@@ -31,6 +31,7 @@
#include "qgsnetworkaccessmanager.h"
#include "qgsproject.h"
#include "qgsdualview.h"
#include "qgscrscache.h"

#include "qgsattributetablefiltermodel.h"
#include "qgsrasterformatsaveoptionswidget.h"
@@ -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
@@ -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 );

@@ -16,6 +16,7 @@
#include "qgswelcomepageitemsmodel.h"
#include "qgscoordinatereferencesystem.h"
#include "qgsmessagelog.h"
#include "qgscrscache.h"

#include <QApplication>
#include <QAbstractTextDocumentLayout>
@@ -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
@@ -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 );
}
}

@@ -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;
@@ -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
*/
@@ -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 );

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

@@ -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 );
@@ -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 );
@@ -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 );
@@ -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.
You can’t perform that action at this time.