Skip to content

Commit ccb8e16

Browse files
committed
Always get full proj string, including towgs parameters
See https://lists.osgeo.org/pipermail/proj/2019-May/008565.html Fixes some unit test failures
1 parent 83adb92 commit ccb8e16

File tree

2 files changed

+26
-9
lines changed

2 files changed

+26
-9
lines changed

src/core/qgscoordinatereferencesystem.cpp

+26-5
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
#if PROJ_VERSION_MAJOR>=6
4343
#include "qgsprojutils.h"
4444
#include <proj.h>
45+
#include <proj_experimental.h>
4546
#else
4647
#include <proj_api.h>
4748
#endif
@@ -72,6 +73,23 @@ QHash< long, QgsCoordinateReferenceSystem > QgsCoordinateReferenceSystem::sSrsId
7273
QReadWriteLock QgsCoordinateReferenceSystem::sCrsStringLock;
7374
QHash< QString, QgsCoordinateReferenceSystem > QgsCoordinateReferenceSystem::sStringCache;
7475

76+
77+
#if PROJ_VERSION_MAJOR>=6
78+
QString getFullProjString( PJ *obj )
79+
{
80+
// see https://lists.osgeo.org/pipermail/proj/2019-May/008565.html, it's not sufficient to just
81+
// use proj_as_proj_string
82+
QgsProjUtils::proj_pj_unique_ptr boundCrs( proj_crs_create_bound_crs_to_WGS84( QgsProjContext::get(), obj, nullptr ) );
83+
if ( const char *proj4src = proj_as_proj_string( QgsProjContext::get(), boundCrs.get(), PJ_PROJ_4, nullptr ) )
84+
{
85+
return QString( proj4src );
86+
}
87+
else
88+
{
89+
return QString( proj_as_proj_string( QgsProjContext::get(), obj, PJ_PROJ_4, nullptr ) );
90+
}
91+
}
92+
#endif
7593
//--------------------------
7694

7795
QgsCoordinateReferenceSystem::QgsCoordinateReferenceSystem()
@@ -91,7 +109,7 @@ QgsCoordinateReferenceSystem::QgsCoordinateReferenceSystem( const long id, CrsTy
91109
createFromId( id, type );
92110
}
93111

94-
QgsCoordinateReferenceSystem::QgsCoordinateReferenceSystem( const QgsCoordinateReferenceSystem &srs ) //NOLINT
112+
QgsCoordinateReferenceSystem::QgsCoordinateReferenceSystem( const QgsCoordinateReferenceSystem &srs ) //NOLINT
95113
: d( srs.d )
96114
{
97115
}
@@ -703,12 +721,13 @@ bool QgsCoordinateReferenceSystem::createFromWkt( const QString &wkt )
703721
// create the proj4 structs needed for transforming
704722
if ( d->mPj )
705723
{
706-
if ( const char *proj4src = proj_as_proj_string( QgsProjContext::get(), d->mPj.get(), PJ_PROJ_4, nullptr ) )
724+
const QString proj4String = getFullProjString( d->mPj.get() );
725+
if ( !proj4String.isEmpty() )
707726
{
708727
//now that we have the proj4string, delegate to createFromProj4 so
709728
// that we can try to fill in the remaining class members...
710729
//create from Proj will set the isValidFlag
711-
createFromProj4( proj4src );
730+
createFromProj4( proj4String );
712731
}
713732
}
714733
#else
@@ -1180,7 +1199,7 @@ QString QgsCoordinateReferenceSystem::toProj4() const
11801199
#if PROJ_VERSION_MAJOR>=6
11811200
if ( d->mPj )
11821201
{
1183-
d->mProj4 = QString( proj_as_proj_string( QgsProjContext::get(), d->mPj.get(), PJ_PROJ_4, nullptr ) );
1202+
d->mProj4 = getFullProjString( d->mPj.get() );
11841203
}
11851204
#else
11861205
char *proj4src = nullptr;
@@ -2199,7 +2218,9 @@ int QgsCoordinateReferenceSystem::syncDatabase()
21992218

22002219
crs = QgsProjUtils::crsToSingleCrs( crs.get() );
22012220

2202-
QString proj4( proj_as_proj_string( pjContext, crs.get(), PJ_PROJ_4, nullptr ) );
2221+
crs.reset( proj_crs_create_bound_crs_to_WGS84( pjContext, crs.get(), nullptr ) );
2222+
2223+
QString proj4 = getFullProjString( crs.get() );
22032224
proj4.replace( QStringLiteral( "+type=crs" ), QString() );
22042225
proj4 = proj4.trimmed();
22052226

tests/src/core/testqgscoordinatereferencesystem.cpp

-4
Original file line numberDiff line numberDiff line change
@@ -909,11 +909,7 @@ void TestQgsCoordinateReferenceSystem::projectEPSG25833()
909909
QSignalSpy spyCrsChanged( &p, &QgsProject::crsChanged );
910910
QVERIFY( p.read( TEST_DATA_DIR + QStringLiteral( "/projects/epsg25833.qgs" ) ) );
911911
QVERIFY( p.crs().isValid() );
912-
#if PROJ_VERSION_MAJOR>=6
913-
QCOMPARE( p.crs().toProj4(), QStringLiteral( "+proj=utm +zone=33 +ellps=GRS80 +units=m +no_defs" ) ); // matching proj string to EPSG:25833 is broken on proj6 --remove when fixed
914-
#else
915912
QCOMPARE( p.crs().authid(), QStringLiteral( "EPSG:25833" ) );
916-
#endif
917913
QCOMPARE( spyCrsChanged.count(), 1 );
918914
}
919915

0 commit comments

Comments
 (0)