Skip to content
Permalink
Browse files

Ensure output of QgsCoordinateReferenceSystem::ellipsoidAcronym() mat…

…ches

the acronyms and format user in QgsEllipsoidUtils

Refs #33358

(cherry picked from commit c2441ca)
  • Loading branch information
nyalldawson committed Dec 16, 2019
1 parent 77cd431 commit f8eb274c96ac97b01fbb5b4a389ad69a496d2564
Showing with 116 additions and 4 deletions.
  1. +39 −1 src/core/qgscoordinatereferencesystem.cpp
  2. +77 −3 tests/src/core/testqgscoordinatereferencesystem.cpp
@@ -626,7 +626,11 @@ bool QgsCoordinateReferenceSystem::loadFromDatabase( const QString &db, const QS
d->mSrsId = statement.columnAsText( 0 ).toLong();
d->mDescription = statement.columnAsText( 1 );
d->mProjectionAcronym = statement.columnAsText( 2 );
#if PROJ_VERSION_MAJOR>=6
d->mEllipsoidAcronym.clear();
#else
d->mEllipsoidAcronym = statement.columnAsText( 3 );
#endif
d->mProj4 = statement.columnAsText( 4 );
d->mSRID = statement.columnAsText( 5 ).toLong();
d->mAuthId = statement.columnAsText( 6 );
@@ -856,6 +860,9 @@ bool QgsCoordinateReferenceSystem::createFromProj4( const QString &proj4String )

d->mProjectionAcronym = myProjRegExp.cap( 1 );

#if PROJ_VERSION_MAJOR>=6
d->mEllipsoidAcronym.clear();
#else
QRegExp myEllipseRegExp( "\\+ellps=(\\S+)" );
myStart = myEllipseRegExp.indexIn( myProj4String );
if ( myStart == -1 )
@@ -866,6 +873,7 @@ bool QgsCoordinateReferenceSystem::createFromProj4( const QString &proj4String )
{
d->mEllipsoidAcronym = myEllipseRegExp.cap( 1 );
}
#endif

QRegExp myAxisRegExp( "\\+a=(\\S+)" );
myStart = myAxisRegExp.indexIn( myProj4String );
@@ -1161,7 +1169,37 @@ QString QgsCoordinateReferenceSystem::ellipsoidAcronym() const
{
if ( d->mEllipsoidAcronym.isNull() )
{
#if PROJ_VERSION_MAJOR>=6
if ( d->mPj )
{
QgsProjUtils::proj_pj_unique_ptr ellipsoid( proj_get_ellipsoid( QgsProjContext::get(), d->mPj.get() ) );
if ( ellipsoid )
{
const QString ellipsoidAuthName( proj_get_id_auth_name( ellipsoid.get(), 0 ) );
const QString ellipsoidAuthCode( proj_get_id_code( ellipsoid.get(), 0 ) );
if ( !ellipsoidAuthName.isEmpty() && !ellipsoidAuthCode.isEmpty() )
d->mEllipsoidAcronym = QStringLiteral( "%1:%2" ).arg( ellipsoidAuthName, ellipsoidAuthCode );
else
{
double semiMajor, semiMinor, invFlattening;
int semiMinorComputed = 0;
if ( proj_ellipsoid_get_parameters( QgsProjContext::get(), ellipsoid.get(), &semiMajor, &semiMinor, &semiMinorComputed, &invFlattening ) )
{
d->mEllipsoidAcronym = QStringLiteral( "PARAMETER:%1:%2" ).arg( qgsDoubleToString( semiMajor ),
qgsDoubleToString( semiMinor ) );
}
else
{
d->mEllipsoidAcronym.clear();
}
}
}
}
return d->mEllipsoidAcronym;
#else
return QString();

#endif
}
else
{
@@ -2298,7 +2336,7 @@ bool QgsCoordinateReferenceSystem::loadFromAuthCode( const QString &auth, const
QString ellipsoid;
getOperationAndEllipsoidFromProjString( proj4, operation, ellipsoid );
d->mProjectionAcronym = operation;
d->mEllipsoidAcronym = ellipsoid;
d->mEllipsoidAcronym.clear();
d->mPj = std::move( crs );

const QString dbVals = sAuthIdToQgisSrsIdMap.value( QStringLiteral( "%1:%2" ).arg( auth, code ).toUpper() );
@@ -170,13 +170,25 @@ void TestQgsCoordinateReferenceSystem::wktCtor()
QgsCoordinateReferenceSystem myCrs( myWkt );
debugPrint( myCrs );
QVERIFY( myCrs.isValid() );

#if PROJ_VERSION_MAJOR>=6
QCOMPARE( myCrs.ellipsoidAcronym(), QStringLiteral( "EPSG:7030" ) );
#else
QCOMPARE( myCrs.ellipsoidAcronym(), QStringLiteral( "WGS84" ) );
#endif
}
void TestQgsCoordinateReferenceSystem::idCtor()
{
QgsCoordinateReferenceSystem myCrs( GEOSRID,
QgsCoordinateReferenceSystem::EpsgCrsId );
debugPrint( myCrs );
QVERIFY( myCrs.isValid() );

#if PROJ_VERSION_MAJOR>=6
QCOMPARE( myCrs.ellipsoidAcronym(), QStringLiteral( "EPSG:7030" ) );
#else
QCOMPARE( myCrs.ellipsoidAcronym(), QStringLiteral( "WGS84" ) );
#endif
}
void TestQgsCoordinateReferenceSystem::copyCtor()
{
@@ -220,6 +232,12 @@ void TestQgsCoordinateReferenceSystem::createFromId()
debugPrint( myCrs );
QVERIFY( myCrs.isValid() );
QCOMPARE( myCrs.srsid(), GEOCRS_ID );

#if PROJ_VERSION_MAJOR>=6
QCOMPARE( myCrs.ellipsoidAcronym(), QStringLiteral( "EPSG:7030" ) );
#else
QCOMPARE( myCrs.ellipsoidAcronym(), QStringLiteral( "WGS84" ) );
#endif
}

void TestQgsCoordinateReferenceSystem::fromEpsgId()
@@ -236,6 +254,10 @@ void TestQgsCoordinateReferenceSystem::fromEpsgId()
#if PROJ_VERSION_MAJOR>=6
QCOMPARE( myCrs.authid(), QStringLiteral( "ESRI:54030" ) );
#endif

#if PROJ_VERSION_MAJOR>=6
QCOMPARE( myCrs.ellipsoidAcronym(), QStringLiteral( "EPSG:7030" ) );
#endif
}
void TestQgsCoordinateReferenceSystem::createFromOgcWmsCrs()
{
@@ -264,13 +286,26 @@ void TestQgsCoordinateReferenceSystem::createFromOgcWmsCrs()
#if PROJ_VERSION_MAJOR>=6
QCOMPARE( myCrs.authid(), QString( "OGC:CRS84" ) );
#endif

#if PROJ_VERSION_MAJOR>=6
QCOMPARE( myCrs.ellipsoidAcronym(), QStringLiteral( "EPSG:7030" ) );
#else
QCOMPARE( myCrs.ellipsoidAcronym(), QStringLiteral( "WGS84" ) );
#endif
}

void TestQgsCoordinateReferenceSystem::fromOgcWmsCrs()
{
QgsCoordinateReferenceSystem myCrs = QgsCoordinateReferenceSystem::fromOgcWmsCrs( QStringLiteral( "EPSG:4326" ) );
QVERIFY( myCrs.isValid() );
QCOMPARE( myCrs.authid(), QString( "EPSG:4326" ) );

#if PROJ_VERSION_MAJOR>=6
QCOMPARE( myCrs.ellipsoidAcronym(), QStringLiteral( "EPSG:7030" ) );
#else
QCOMPARE( myCrs.ellipsoidAcronym(), QStringLiteral( "WGS84" ) );
#endif

myCrs = QgsCoordinateReferenceSystem::fromOgcWmsCrs( QStringLiteral( "not a crs" ) );
QVERIFY( !myCrs.isValid() );
}
@@ -306,6 +341,12 @@ void TestQgsCoordinateReferenceSystem::createFromSrid()
debugPrint( myCrs );
QVERIFY( myCrs.isValid() );
QCOMPARE( myCrs.srsid(), GEOCRS_ID );

#if PROJ_VERSION_MAJOR>=6
QCOMPARE( myCrs.ellipsoidAcronym(), QStringLiteral( "EPSG:7030" ) );
#else
QCOMPARE( myCrs.ellipsoidAcronym(), QStringLiteral( "WGS84" ) );
#endif
}

void TestQgsCoordinateReferenceSystem::sridCache()
@@ -343,6 +384,12 @@ void TestQgsCoordinateReferenceSystem::createFromWkt()
debugPrint( myCrs );
QVERIFY( myCrs.isValid() );
QCOMPARE( myCrs.srsid(), GEOCRS_ID );

#if PROJ_VERSION_MAJOR>=6
QCOMPARE( myCrs.ellipsoidAcronym(), QStringLiteral( "EPSG:7030" ) );
#else
QCOMPARE( myCrs.ellipsoidAcronym(), QStringLiteral( "WGS84" ) );
#endif
}

void TestQgsCoordinateReferenceSystem::createFromWktWithIdentify()
@@ -354,6 +401,7 @@ void TestQgsCoordinateReferenceSystem::createFromWktWithIdentify()
QVERIFY( crs.isValid() );
QCOMPARE( crs.authid(), QStringLiteral( "EPSG:7855" ) );

QCOMPARE( crs.ellipsoidAcronym(), QStringLiteral( "EPSG:7019" ) );
#endif
}

@@ -380,6 +428,12 @@ void TestQgsCoordinateReferenceSystem::createFromWktUnknown()
QCOMPARE( crs.authid(), QStringLiteral( "USER:100001" ) );
QCOMPARE( crs.mapUnits(), QgsUnitTypes::DistanceMeters );

#if PROJ_VERSION_MAJOR>=6
QCOMPARE( crs.ellipsoidAcronym().left( 30 ), QStringLiteral( "PARAMETER:6378388:6356911.9461" ) );
#else
QCOMPARE( crs.ellipsoidAcronym(), QStringLiteral( "intl" ) );
#endif

// try creating new ones with same def
QgsCoordinateReferenceSystem crs2( QStringLiteral( "USER:100001" ) );
QVERIFY( crs2.isValid() );
@@ -612,6 +666,12 @@ void TestQgsCoordinateReferenceSystem::createFromProj4()
debugPrint( myCrs );
QVERIFY( myCrs.isValid() );
QCOMPARE( myCrs.srsid(), GEOCRS_ID );

#if PROJ_VERSION_MAJOR>=6
QCOMPARE( myCrs.ellipsoidAcronym(), QStringLiteral( "EPSG:7030" ) );
#else
QCOMPARE( myCrs.ellipsoidAcronym(), QStringLiteral( "WGS84" ) );
#endif
}

void TestQgsCoordinateReferenceSystem::fromProj4()
@@ -626,6 +686,10 @@ void TestQgsCoordinateReferenceSystem::fromProj4()

myCrs = QgsCoordinateReferenceSystem::fromProj4( "+proj=utm +zone=36 +south +a=6378249.145 +b=6356514.966398753 +towgs84=-143,-90,-294,0,0,0,0 +units=m +no_defs" );
QCOMPARE( myCrs.authid(), QStringLiteral( "EPSG:20936" ) );

#if PROJ_VERSION_MAJOR>=6
QCOMPARE( myCrs.ellipsoidAcronym(), QStringLiteral( "EPSG:7013" ) );
#endif
}

void TestQgsCoordinateReferenceSystem::proj4Cache()
@@ -922,7 +986,6 @@ void TestQgsCoordinateReferenceSystem::readWriteXml()
#if PROJ_VERSION_MAJOR>=6
QCOMPARE( myCrs15.authid(), QStringLiteral( "USER:100009" ) );
QCOMPARE( myCrs15.toProj4(), QStringLiteral( "+proj=lcc +lat_0=-37.2 +lon_0=145.1 +lat_1=-36 +lat_2=-38 +x_0=2510000 +y_0=2520000 +ellps=GRS80 +towgs84=1,2,3,4,5,6,7 +units=m +no_defs +type=crs" ) );
QgsDebugMsg( myCrs15.toWkt() );
QCOMPARE( myCrs15.toWkt(), QStringLiteral( R"""(PROJCS["xxx",GEOGCS["GDA94",DATUM["Geocentric_Datum_of_Australia_1994",SPHEROID["GRS 1980",6378137,298.257222101,AUTHORITY["EPSG","7019"]],TOWGS84[1,2,3,4,5,6,7],AUTHORITY["EPSG","6283"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4283"]],PROJECTION["Lambert_Conformal_Conic_2SP"],PARAMETER["standard_parallel_1",-36],PARAMETER["standard_parallel_2",-38],PARAMETER["latitude_of_origin",-37.2],PARAMETER["central_meridian",145.1],PARAMETER["false_easting",2510000],PARAMETER["false_northing",2520000],UNIT["metre",1,AUTHORITY["EPSG","9001"]],AXIS["Easting",EAST],AXIS["Northing",NORTH]])""" ) );
#else
QCOMPARE( myCrs15.authid(), QStringLiteral( "USER:100003" ) );
@@ -1001,9 +1064,20 @@ void TestQgsCoordinateReferenceSystem::ellipsoidAcronym()
{
QgsCoordinateReferenceSystem myCrs;
myCrs.createFromSrid( GEOSRID );
QString myAcronym = myCrs.ellipsoidAcronym();
debugPrint( myCrs );
QCOMPARE( myAcronym, QStringLiteral( "WGS84" ) );
#if PROJ_VERSION_MAJOR>=6
QCOMPARE( myCrs.ellipsoidAcronym(), QStringLiteral( "EPSG:7030" ) );
#else
QCOMPARE( myCrs.ellipsoidAcronym(), QStringLiteral( "WGS84" ) );
#endif

myCrs.createFromString( QStringLiteral( "EPSG:2951" ) );
#if PROJ_VERSION_MAJOR>=6
QCOMPARE( myCrs.ellipsoidAcronym(), QStringLiteral( "EPSG:7019" ) );
#else
QCOMPARE( myCrs.ellipsoidAcronym(), QStringLiteral( "GRS80" ) );
#endif

}
void TestQgsCoordinateReferenceSystem::toWkt()
{

0 comments on commit f8eb274

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