Skip to content

Commit 376f979

Browse files
strknyalldawson
authored andcommittedOct 23, 2023
Stop considering 0,0,0,0 as a Null QgsRectangle
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
1 parent fc81e81 commit 376f979

File tree

4 files changed

+11
-10
lines changed

4 files changed

+11
-10
lines changed
 

‎src/core/geometry/qgsrectangle.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,8 @@ QString QgsRectangle::toString( int precision ) const
146146
}
147147
}
148148

149-
if ( isEmpty() )
150-
rep = QStringLiteral( "Empty" );
149+
if ( isNull() )
150+
rep = QStringLiteral( "Null" );
151151
else
152152
rep = QStringLiteral( "%1,%2 : %3,%4" )
153153
.arg( mXmin, 0, 'f', precision )

‎src/core/geometry/qgsrectangle.h

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,7 @@ class CORE_EXPORT QgsRectangle
516516
*/
517517
bool isEmpty() const
518518
{
519-
return mXmax < mXmin || mYmax < mYmin || qgsDoubleNear( mXmax, mXmin ) || qgsDoubleNear( mYmax, mYmin );
519+
return isNull() || mXmax <= mXmin || mYmax <= mYmin || qgsDoubleNear( mXmax, mXmin ) || qgsDoubleNear( mYmax, mYmin );
520520
}
521521

522522
/**
@@ -533,7 +533,6 @@ class CORE_EXPORT QgsRectangle
533533
// rectangle created QgsRectangle() or with rect.setNull() or
534534
// otherwise having NaN ordinates
535535
return ( std::isnan( mXmin ) && std::isnan( mXmax ) && std::isnan( mYmin ) && std::isnan( mYmax ) ) ||
536-
( qgsDoubleNear( mXmin, 0.0 ) && qgsDoubleNear( mXmax, 0.0 ) && qgsDoubleNear( mYmin, 0.0 ) && qgsDoubleNear( mYmax, 0.0 ) ) ||
537536
( qgsDoubleNear( mXmin, std::numeric_limits<double>::max() ) && qgsDoubleNear( mYmin, std::numeric_limits<double>::max() ) &&
538537
qgsDoubleNear( mXmax, -std::numeric_limits<double>::max() ) && qgsDoubleNear( mYmax, -std::numeric_limits<double>::max() ) );
539538
}
@@ -665,10 +664,10 @@ class CORE_EXPORT QgsRectangle
665664

666665
private:
667666

668-
double mXmin = 0.0;
669-
double mYmin = 0.0;
670-
double mXmax = 0.0;
671-
double mYmax = 0.0;
667+
double mXmin = std::numeric_limits<double>::max();
668+
double mYmin = std::numeric_limits<double>::max();
669+
double mXmax = -std::numeric_limits<double>::max();
670+
double mYmax = -std::numeric_limits<double>::max();
672671

673672
};
674673

‎tests/src/core/geometry/testqgsrectangle.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,13 @@ void TestQgsRectangle::isNull()
7272
QVERIFY( QgsRectangle().isNull() );
7373
QVERIFY( QgsRectangle( std::numeric_limits<double>::quiet_NaN(), std::numeric_limits<double>::quiet_NaN(),
7474
std::numeric_limits<double>::quiet_NaN(), std::numeric_limits<double>::quiet_NaN() ).isNull() );
75-
QVERIFY( QgsRectangle( 0.0, 0.0, 0.0, 0.0 ).isNull() );
75+
QVERIFY( !QgsRectangle( 0.0, 0.0, 0.0, 0.0 ).isNull() );
76+
QVERIFY( !QgsRectangle( 1.0, 1.0, 1.0, 1.0 ).isNull() );
7677
QVERIFY( !QgsRectangle( std::numeric_limits<double>::max(), -std::numeric_limits<double>::max(), std::numeric_limits<double>::max(), -std::numeric_limits<double>::max() ).isNull() );
7778
QVERIFY( !QgsRectangle( 1, 2, 2, 1 ).isNull() );
7879
}
7980

81+
8082
void TestQgsRectangle::fromWkt()
8183
{
8284
QgsRectangle rect = QgsRectangle::fromWkt( QStringLiteral( "POLYGON((0 0,1 0,1 1,0 1,0 0))" ) );

‎tests/src/python/test_qgsrectangle.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ def testAsWktPolygon(self):
125125

126126
def testToString(self):
127127
"""Test the different string representations"""
128-
self.assertEqual(QgsRectangle().toString(), 'Empty')
128+
self.assertEqual(QgsRectangle().toString(), 'Null')
129129
rect = QgsRectangle(0, 0.1, 0.2, 0.3)
130130
self.assertEqual(rect.toString(), '0.0000000000000000,0.1000000000000000 : 0.2000000000000000,0.3000000000000000')
131131

0 commit comments

Comments
 (0)
Please sign in to comment.