Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PostgreSQL provider] Retrieve CRS from spatial_ref_sys #43338

Merged
merged 2 commits into from
May 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions src/core/proj/qgscoordinatereferencesystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,9 @@ bool QgsCoordinateReferenceSystem::createFromId( const long id, CrsType type )
result = createFromSrsId( id );
break;
case PostgisCrsId:
result = createFromPostgisSrid( id );
Q_NOWARN_DEPRECATED_PUSH
result = createFromSrid( id );
Q_NOWARN_DEPRECATED_POP
break;
case EpsgCrsId:
result = createFromOgcWmsCrs( QStringLiteral( "EPSG:%1" ).arg( id ) );
Expand Down Expand Up @@ -294,7 +296,9 @@ bool QgsCoordinateReferenceSystem::createFromString( const QString &definition )
else if ( authName == QLatin1String( "postgis" ) )
{
const long id = match.captured( 2 ).toLong();
result = createFromPostgisSrid( id );
Q_NOWARN_DEPRECATED_PUSH
result = createFromSrid( id );
Q_NOWARN_DEPRECATED_POP
}
else if ( authName == QLatin1String( "esri" ) || authName == QLatin1String( "osgeo" ) || authName == QLatin1String( "ignf" ) || authName == QLatin1String( "zangi" ) || authName == QLatin1String( "iau2000" ) )
{
Expand Down Expand Up @@ -491,11 +495,6 @@ 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
1 change: 0 additions & 1 deletion src/core/proj/qgscoordinatereferencesystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -1091,7 +1091,6 @@ class CORE_EXPORT QgsCoordinateReferenceSystem
static const QHash< long, QgsCoordinateReferenceSystem > &srIdCache();

friend class TestQgsCoordinateReferenceSystem;
friend class QgsPostgresProvider;
friend class QgsCoordinateReferenceSystemRegistry;
friend bool CORE_EXPORT operator> ( const QgsCoordinateReferenceSystem &c1, const QgsCoordinateReferenceSystem &c2 );
friend bool CORE_EXPORT operator< ( const QgsCoordinateReferenceSystem &c1, const QgsCoordinateReferenceSystem &c2 );
Expand Down
20 changes: 15 additions & 5 deletions src/providers/postgres/qgspostgresprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4658,9 +4658,6 @@ QgsCoordinateReferenceSystem QgsPostgresProvider::crs() const
QgsCoordinateReferenceSystem srs;
int srid = mRequestedSrid.isEmpty() ? mDetectedSrid.toInt() : mRequestedSrid.toInt();

// TODO QGIS 4 - move the logic from createFromSridInternal to sit within the postgres provider alone
srs.createFromPostgisSrid( srid );
if ( !srs.isValid() )
{
static QMutex sMutex;
QMutexLocker locker( &sMutex );
Expand All @@ -4672,10 +4669,23 @@ QgsCoordinateReferenceSystem QgsPostgresProvider::crs() const
QgsPostgresConn *conn = connectionRO();
if ( conn )
{
QgsPostgresResult result( conn->PQexec( QStringLiteral( "SELECT proj4text FROM spatial_ref_sys WHERE srid=%1" ).arg( srid ) ) );
QgsPostgresResult result( conn->PQexec( QStringLiteral( "SELECT auth_name, auth_srid, srtext, proj4text FROM spatial_ref_sys WHERE srid=%1" ).arg( srid ) ) );
if ( result.PQresultStatus() == PGRES_TUPLES_OK )
{
srs = QgsCoordinateReferenceSystem::fromProj( result.PQgetvalue( 0, 0 ) );
const QString authName = result.PQgetvalue( 0, 0 );
const QString authSRID = result.PQgetvalue( 0, 1 );
const QString srText = result.PQgetvalue( 0, 2 );
bool ok = false;
if ( authName == QLatin1String( "EPSG" ) || authName == QLatin1String( "ESRI" ) )
{
ok = srs.createFromUserInput( authName + ':' + authSRID );
}
if ( !ok && !srText.isEmpty() )
{
ok = srs.createFromUserInput( srText );
}
if ( !ok )
srs = QgsCoordinateReferenceSystem::fromProj( result.PQgetvalue( 0, 3 ) );
sCrsCache.insert( srid, srs );
}
}
Expand Down
24 changes: 17 additions & 7 deletions tests/src/core/testqgscoordinatereferencesystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,9 @@ void TestQgsCoordinateReferenceSystem::ogcWmsCrsCache()
void TestQgsCoordinateReferenceSystem::createFromSrid()
{
QgsCoordinateReferenceSystem myCrs;
myCrs.createFromPostgisSrid( GEOSRID );
Q_NOWARN_DEPRECATED_PUSH
myCrs.createFromSrid( GEOSRID );
Q_NOWARN_DEPRECATED_POP
debugPrint( myCrs );
QVERIFY( myCrs.isValid() );
QCOMPARE( myCrs.srsid(), GEOCRS_ID );
Expand All @@ -374,30 +376,34 @@ void TestQgsCoordinateReferenceSystem::createFromSrid()

void TestQgsCoordinateReferenceSystem::sridCache()
{
Q_NOWARN_DEPRECATED_PUSH

// test that crs can be retrieved correctly from cache
QgsCoordinateReferenceSystem crs;
crs.createFromPostgisSrid( 3112 );
crs.createFromSrid( 3112 );
QVERIFY( crs.isValid() );
QCOMPARE( crs.authid(), QStringLiteral( "EPSG:3112" ) );
QVERIFY( QgsCoordinateReferenceSystem::srIdCache().contains( 3112 ) );
// a second time, so crs is fetched from cache
QgsCoordinateReferenceSystem crs2;
crs2.createFromPostgisSrid( 3112 );
crs2.createFromSrid( 3112 );
QVERIFY( crs2.isValid() );
QCOMPARE( crs2.authid(), QStringLiteral( "EPSG:3112" ) );

// invalid
QgsCoordinateReferenceSystem crs3;
QVERIFY( !crs3.createFromPostgisSrid( -3141 ) );
QVERIFY( !crs3.createFromSrid( -3141 ) );
QVERIFY( !crs3.isValid() );
QVERIFY( QgsCoordinateReferenceSystem::srIdCache().contains( -3141 ) );
// a second time, so invalid crs is fetched from cache
QgsCoordinateReferenceSystem crs4;
QVERIFY( !crs4.createFromPostgisSrid( -3141 ) );
QVERIFY( !crs4.createFromSrid( -3141 ) );
QVERIFY( !crs4.isValid() );

QgsCoordinateReferenceSystem::invalidateCache();
QVERIFY( !QgsCoordinateReferenceSystem::srIdCache().contains( 3112 ) );

Q_NOWARN_DEPRECATED_POP
}

void TestQgsCoordinateReferenceSystem::createFromWkt()
Expand Down Expand Up @@ -593,7 +599,9 @@ QString TestQgsCoordinateReferenceSystem::testESRIWkt( int i, QgsCoordinateRefer
void TestQgsCoordinateReferenceSystem::createFromSrId()
{
QgsCoordinateReferenceSystem myCrs;
QVERIFY( myCrs.createFromPostgisSrid( GEOSRID ) );
Q_NOWARN_DEPRECATED_PUSH
QVERIFY( myCrs.createFromSrid( GEOSRID ) );
Q_NOWARN_DEPRECATED_POP
QVERIFY( myCrs.isValid() );
QCOMPARE( myCrs.srsid(), GEOCRS_ID );
}
Expand Down Expand Up @@ -1235,7 +1243,9 @@ void TestQgsCoordinateReferenceSystem::customSrsValidation()
void TestQgsCoordinateReferenceSystem::postgisSrid()
{
QgsCoordinateReferenceSystem myCrs;
myCrs.createFromPostgisSrid( GEOSRID );
Q_NOWARN_DEPRECATED_PUSH
myCrs.createFromSrid( GEOSRID );
Q_NOWARN_DEPRECATED_POP
QVERIFY( myCrs.postgisSrid() == GEOSRID );
debugPrint( myCrs );
}
Expand Down
28 changes: 28 additions & 0 deletions tests/src/python/test_provider_postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -2607,6 +2607,34 @@ def _test(vl, extent, ids):

_test(vl, QgsRectangle(-181, -90, 181, 90), [1, 2, 3]) # no use of spatial index currently

def testReadCustomSRID(self):
"""Test that we can correctly read the SRS from a custom SRID"""

md = QgsProviderRegistry.instance().providerMetadata("postgres")
conn = md.createConnection(self.dbconn, {})

# Cleanup if needed
try:
conn.dropVectorTable('qgis_test', 'test_custom_srid')
except QgsProviderConnectionException:
pass

conn.executeSql("DELETE FROM spatial_ref_sys WHERE srid = 543210 AND auth_name='FOO' AND auth_srid=32600;")
conn.executeSql("""INSERT INTO spatial_ref_sys (srid, auth_name, auth_srid, srtext, proj4text) VALUES (543210, 'FOO', 32600, 'PROJCS["my_projection",GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563,AUTHORITY["EPSG","7030"]],AUTHORITY["EPSG","6326"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]]],PROJECTION["Transverse_Mercator"],PARAMETER["latitude_of_origin",0],PARAMETER["central_meridian",0],PARAMETER["scale_factor",1],PARAMETER["false_easting",0],PARAMETER["false_northing",0],UNIT["metre",1,AUTHORITY["EPSG","9001"]],AXIS["Easting",EAST],AXIS["Northing",NORTH]]','+proj=tmerc +lat_0=0 +lon_0=0 +k=1 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs');""")

conn.executeSql('''
CREATE TABLE "qgis_test"."test_custom_srid" (
gid serial primary key,
geom geometry(Point, 543210)
);''')

layer = QgsVectorLayer(self.dbconn + ' sslmode=disable key=\'gid\'table="qgis_test"."test_custom_srid" (geom) sql=', 'test', 'postgres')

conn.executeSql("DELETE FROM spatial_ref_sys WHERE srid = 543210 AND auth_name='FOO' AND auth_srid=32600;")

self.assertTrue(layer.isValid())
self.assertEqual(layer.crs().description(), 'my_projection')


class TestPyQgsPostgresProviderCompoundKey(unittest.TestCase, ProviderTestCase):

Expand Down