Skip to content

Commit

Permalink
Deprecate methods for constructing CRS from Postgis srids
Browse files Browse the repository at this point in the history
Constructing CRS using Postgis srids is highly discouraged,
and instead CRSes should always be constructed using auth:id
codes or WKT strings.

QGIS 4.0: The logic should be isolated into the postgres
provider alone, and not exposed to stable API

(cherry picked from commit c76813c)
  • Loading branch information
nyalldawson committed Dec 20, 2019
1 parent 3121990 commit c72565e
Show file tree
Hide file tree
Showing 16 changed files with 98 additions and 134 deletions.
Expand Up @@ -349,13 +349,18 @@ and refer to QGIS internal CRS IDs.
%End


bool createFromSrid( long srid );
bool createFromSrid( long srid ) /Deprecated/;
%Docstring
Sets this CRS by lookup of the given PostGIS SRID in the CRS database.

:param srid: The PostGIS SRID for the desired spatial reference system.

:return: ``True`` on success else ``False``

.. deprecated::
Use alternative methods for SRS construction instead -- this
method was specifically created for use by the postgres provider alone,
and using it elsewhere will lead to subtle bugs.
%End

bool createFromWkt( const QString &wkt );
Expand Down
11 changes: 8 additions & 3 deletions src/core/qgscoordinatereferencesystem.cpp
Expand Up @@ -249,7 +249,7 @@ bool QgsCoordinateReferenceSystem::createFromId( const long id, CrsType type )
result = createFromSrsId( id );
break;
case PostgisCrsId:
result = createFromSrid( id );
result = createFromPostgisSrid( id );
break;
case EpsgCrsId:
result = createFromOgcWmsCrs( QStringLiteral( "EPSG:%1" ).arg( id ) );
Expand Down Expand Up @@ -289,7 +289,7 @@ bool QgsCoordinateReferenceSystem::createFromString( const QString &definition )
else if ( authName == QLatin1String( "postgis" ) )
{
const long id = match.captured( 2 ).toLong();
result = createFromId( id, PostgisCrsId );
result = createFromPostgisSrid( id );
}
else if ( authName == QLatin1String( "esri" ) || authName == QLatin1String( "osgeo" ) || authName == QLatin1String( "ignf" ) || authName == QLatin1String( "zangi" ) || authName == QLatin1String( "iau2000" ) )
{
Expand Down Expand Up @@ -495,6 +495,11 @@ void QgsCoordinateReferenceSystem::validate()
}

bool QgsCoordinateReferenceSystem::createFromSrid( const long id )
{
return createFromPostgisSrid( id );
}

bool QgsCoordinateReferenceSystem::createFromPostgisSrid( const long id )
{
QgsReadWriteLocker locker( sSrIdCacheLock, QgsReadWriteLocker::Read );
if ( !sDisableSrIdCache )
Expand Down Expand Up @@ -3292,7 +3297,7 @@ QStringList QgsCoordinateReferenceSystem::recentProjections()
if ( ! crs.isValid() )
{
// Couldn't create from EPSG, try the Proj4 string instead
if ( i >= projectionsProj4.size() || !crs.createFromProj4( projectionsProj4.at( i ) ) )
if ( i >= projectionsProj4.size() || !crs.createFromProj( projectionsProj4.at( i ) ) )
{
// No? Skip this entry
continue;
Expand Down
11 changes: 9 additions & 2 deletions src/core/qgscoordinatereferencesystem.h
Expand Up @@ -208,7 +208,7 @@ class CORE_EXPORT QgsCoordinateReferenceSystem
enum CrsType
{
InternalCrsId, //!< Internal ID used by QGIS in the local SQLite database
PostgisCrsId, //!< SRID used in PostGIS
PostgisCrsId, //!< SRID used in PostGIS. DEPRECATED -- DO NOT USE
EpsgCrsId //!< EPSG code
};

Expand Down Expand Up @@ -364,8 +364,12 @@ class CORE_EXPORT QgsCoordinateReferenceSystem
* Sets this CRS by lookup of the given PostGIS SRID in the CRS database.
* \param srid The PostGIS SRID for the desired spatial reference system.
* \returns TRUE on success else FALSE
*
* \deprecated Use alternative methods for SRS construction instead -- this
* method was specifically created for use by the postgres provider alone,
* and using it elsewhere will lead to subtle bugs.
*/
bool createFromSrid( long srid );
Q_DECL_DEPRECATED bool createFromSrid( long srid ) SIP_DEPRECATED;

/**
* Sets this CRS using a WKT definition.
Expand Down Expand Up @@ -942,6 +946,9 @@ class CORE_EXPORT QgsCoordinateReferenceSystem
static bool sDisableStringCache;

friend class TestQgsCoordinateReferenceSystem;
friend class QgsPostgresProvider;

bool createFromPostgisSrid( const long id );
};

Q_DECLARE_METATYPE( QgsCoordinateReferenceSystem )
Expand Down
6 changes: 0 additions & 6 deletions src/providers/db2/qgsdb2provider.cpp
Expand Up @@ -548,12 +548,6 @@ QgsCoordinateReferenceSystem QgsDb2Provider::crs() const
{
if ( !mCrs.isValid() && mSRId > 0 )
{
mCrs.createFromSrid( mSRId );
if ( mCrs.isValid() )
{
return mCrs;
}

// try to load crs from the database tables as a fallback
QSqlQuery query = QSqlQuery( mDatabase );
query.setForwardOnly( true );
Expand Down
2 changes: 1 addition & 1 deletion src/providers/gpx/qgsgpxprovider.cpp
Expand Up @@ -534,7 +534,7 @@ QString QgsGPXProvider::description() const

QgsCoordinateReferenceSystem QgsGPXProvider::crs() const
{
return QgsCoordinateReferenceSystem( GEOSRID, QgsCoordinateReferenceSystem::PostgisCrsId ); // use WGS84
return QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:4326" ) );
}

QgsDataProvider *QgsGpxProviderMetadata::createProvider( const QString &uri, const QgsDataProvider::ProviderOptions &options )
Expand Down
4 changes: 3 additions & 1 deletion src/providers/postgres/qgspostgresprovider.cpp
Expand Up @@ -4305,7 +4305,9 @@ QgsCoordinateReferenceSystem QgsPostgresProvider::crs() const
{
QgsCoordinateReferenceSystem srs;
int srid = mRequestedSrid.isEmpty() ? mDetectedSrid.toInt() : mRequestedSrid.toInt();
srs.createFromSrid( srid );

// TODO QGIS 4 - move the logic from createFromSridInternal to sit within the postgres provider alone
srs.createFromPostgisSrid( srid );
if ( !srs.isValid() )
{
static QMutex sMutex;
Expand Down

0 comments on commit c72565e

Please sign in to comment.