Skip to content

Commit

Permalink
Stop considering 0,0,0,0 as a Null QgsRectangle
Browse files Browse the repository at this point in the history
Construct a proper null rectangle by default.

Make sure a Null rectangle is always also considered Empty.

Print Null rectangle as Null, still print details of Empty rectangles.
Update expected QgsRectangle::toString output on Null rectangle

Includes unit tests

Closes GH-45563
  • Loading branch information
strk committed Oct 22, 2023
1 parent fec18c6 commit 7ec8fa2
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/core/geometry/qgsrectangle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,8 @@ QString QgsRectangle::toString( int precision ) const
}
}

if ( isEmpty() )
rep = QStringLiteral( "Empty" );
if ( isNull() )
rep = QStringLiteral( "Null" );
else
rep = QStringLiteral( "%1,%2 : %3,%4" )
.arg( mXmin, 0, 'f', precision )
Expand Down
11 changes: 5 additions & 6 deletions src/core/geometry/qgsrectangle.h
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,7 @@ class CORE_EXPORT QgsRectangle
*/
bool isEmpty() const
{
return mXmax < mXmin || mYmax < mYmin || qgsDoubleNear( mXmax, mXmin ) || qgsDoubleNear( mYmax, mYmin );
return isNull() || mXmax <= mXmin || mYmax <= mYmin || qgsDoubleNear( mXmax, mXmin ) || qgsDoubleNear( mYmax, mYmin );
}

/**
Expand All @@ -533,7 +533,6 @@ class CORE_EXPORT QgsRectangle
// rectangle created QgsRectangle() or with rect.setNull() or
// otherwise having NaN ordinates
return ( std::isnan( mXmin ) && std::isnan( mXmax ) && std::isnan( mYmin ) && std::isnan( mYmax ) ) ||
( qgsDoubleNear( mXmin, 0.0 ) && qgsDoubleNear( mXmax, 0.0 ) && qgsDoubleNear( mYmin, 0.0 ) && qgsDoubleNear( mYmax, 0.0 ) ) ||
( qgsDoubleNear( mXmin, std::numeric_limits<double>::max() ) && qgsDoubleNear( mYmin, std::numeric_limits<double>::max() ) &&
qgsDoubleNear( mXmax, -std::numeric_limits<double>::max() ) && qgsDoubleNear( mYmax, -std::numeric_limits<double>::max() ) );
}
Expand Down Expand Up @@ -665,10 +664,10 @@ class CORE_EXPORT QgsRectangle

private:

double mXmin = 0.0;
double mYmin = 0.0;
double mXmax = 0.0;
double mYmax = 0.0;
double mXmin = std::numeric_limits<double>::max();
double mYmin = std::numeric_limits<double>::max();
double mXmax = -std::numeric_limits<double>::max();
double mYmax = -std::numeric_limits<double>::max();

};

Expand Down
4 changes: 3 additions & 1 deletion tests/src/core/geometry/testqgsrectangle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,13 @@ void TestQgsRectangle::isNull()
QVERIFY( QgsRectangle().isNull() );
QVERIFY( QgsRectangle( std::numeric_limits<double>::quiet_NaN(), std::numeric_limits<double>::quiet_NaN(),
std::numeric_limits<double>::quiet_NaN(), std::numeric_limits<double>::quiet_NaN() ).isNull() );
QVERIFY( QgsRectangle( 0.0, 0.0, 0.0, 0.0 ).isNull() );
QVERIFY( !QgsRectangle( 0.0, 0.0, 0.0, 0.0 ).isNull() );
QVERIFY( !QgsRectangle( 1.0, 1.0, 1.0, 1.0 ).isNull() );
QVERIFY( !QgsRectangle( std::numeric_limits<double>::max(), -std::numeric_limits<double>::max(), std::numeric_limits<double>::max(), -std::numeric_limits<double>::max() ).isNull() );
QVERIFY( !QgsRectangle( 1, 2, 2, 1 ).isNull() );
}


void TestQgsRectangle::fromWkt()
{
QgsRectangle rect = QgsRectangle::fromWkt( QStringLiteral( "POLYGON((0 0,1 0,1 1,0 1,0 0))" ) );
Expand Down
2 changes: 1 addition & 1 deletion tests/src/python/test_qgsrectangle.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ def testAsWktPolygon(self):

def testToString(self):
"""Test the different string representations"""
self.assertEqual(QgsRectangle().toString(), 'Empty')
self.assertEqual(QgsRectangle().toString(), 'Null')
rect = QgsRectangle(0, 0.1, 0.2, 0.3)
self.assertEqual(rect.toString(), '0.0000000000000000,0.1000000000000000 : 0.2000000000000000,0.3000000000000000')

Expand Down

0 comments on commit 7ec8fa2

Please sign in to comment.