Skip to content
Permalink
Browse files

Deprecate methods for constructing CRS from Postgis srids

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 18, 2019
1 parent 3121990 commit c72565ea63d608b404cb8c017d10b5128114662f
@@ -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 );
@@ -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 ) );
@@ -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" ) )
{
@@ -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 )
@@ -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;
@@ -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
};

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

0 comments on commit c72565e

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