Skip to content

Commit

Permalink
Improve crs comparison operators
Browse files Browse the repository at this point in the history
and fallback to wkt comparison if both are user defined
  • Loading branch information
m-kuhn authored and nyalldawson committed Mar 31, 2021
1 parent 39e9046 commit ba1a411
Showing 1 changed file with 54 additions and 4 deletions.
58 changes: 54 additions & 4 deletions src/core/qgscoordinatereferencesystem.cpp
Expand Up @@ -2585,19 +2585,69 @@ void QgsCoordinateReferenceSystem::invalidateCache( bool disableCache )
sCrsStringLock()->unlock();
}

// invalid < regular < user
bool operator> ( const QgsCoordinateReferenceSystem &c1, const QgsCoordinateReferenceSystem &c2 )
{
return c1.d->mSrsId > c2.d->mSrsId;
if ( c1.d == c2.d )
return false;

if ( !c1.d->mIsValid && !c2.d->mIsValid )
return false;

if ( !c1.d->mIsValid && c2.d->mIsValid )
return false;

if ( c1.d->mIsValid && !c2.d->mIsValid )
return true;

const bool c1IsUser = c1.d->mSrsId >= USER_CRS_START_ID;
const bool c2IsUser = c2.d->mSrsId >= USER_CRS_START_ID;

if ( c1IsUser && !c2IsUser )
return true;

if ( !c1IsUser && c2IsUser )
return false;

if ( !c1IsUser && !c2IsUser )
return c1.d->mAuthId > c2.d->mAuthId;

return c1.toWkt( QgsCoordinateReferenceSystem::WKT_PREFERRED ) > c2.toWkt( QgsCoordinateReferenceSystem::WKT_PREFERRED );
}

bool operator< ( const QgsCoordinateReferenceSystem &c1, const QgsCoordinateReferenceSystem &c2 )
{
return c1.d->mSrsId < c2.d->mSrsId;
if ( c1.d == c2.d )
return false;

if ( !c1.d->mIsValid && !c2.d->mIsValid )
return false;

if ( c1.d->mIsValid && !c2.d->mIsValid )
return false;

if ( !c1.d->mIsValid && c2.d->mIsValid )
return true;

const bool c1IsUser = c1.d->mSrsId >= USER_CRS_START_ID;
const bool c2IsUser = c2.d->mSrsId >= USER_CRS_START_ID;

if ( !c1IsUser && c2IsUser )
return true;

if ( c1IsUser && !c2IsUser )
return false;

if ( !c1IsUser && !c2IsUser )
return c1.d->mAuthId < c2.d->mAuthId;

return c1.toWkt( QgsCoordinateReferenceSystem::WKT_PREFERRED ) < c2.toWkt( QgsCoordinateReferenceSystem::WKT_PREFERRED );
}
bool operator>= ( const QgsCoordinateReferenceSystem &c1, const QgsCoordinateReferenceSystem &c2 )
{
return c1.d->mSrsId >= c2.d->mSrsId;
return !( c1 < c2 );
}
bool operator<= ( const QgsCoordinateReferenceSystem &c1, const QgsCoordinateReferenceSystem &c2 )
{
return c1.d->mSrsId <= c2.d->mSrsId;
return !( c1 > c2 );
}

0 comments on commit ba1a411

Please sign in to comment.