From 376f9798cd0c38a11faec9873dba5b8756d4d5a3 Mon Sep 17 00:00:00 2001 From: Sandro Santilli Date: Fri, 20 Oct 2023 09:17:52 +0200 Subject: [PATCH] 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 --- src/core/geometry/qgsrectangle.cpp | 4 ++-- src/core/geometry/qgsrectangle.h | 11 +++++------ tests/src/core/geometry/testqgsrectangle.cpp | 4 +++- tests/src/python/test_qgsrectangle.py | 2 +- 4 files changed, 11 insertions(+), 10 deletions(-) diff --git a/src/core/geometry/qgsrectangle.cpp b/src/core/geometry/qgsrectangle.cpp index 6c2e1f6dde83..f7f855cca034 100644 --- a/src/core/geometry/qgsrectangle.cpp +++ b/src/core/geometry/qgsrectangle.cpp @@ -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 ) diff --git a/src/core/geometry/qgsrectangle.h b/src/core/geometry/qgsrectangle.h index 55b60b41beda..b5666fefcdf6 100644 --- a/src/core/geometry/qgsrectangle.h +++ b/src/core/geometry/qgsrectangle.h @@ -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 ); } /** @@ -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::max() ) && qgsDoubleNear( mYmin, std::numeric_limits::max() ) && qgsDoubleNear( mXmax, -std::numeric_limits::max() ) && qgsDoubleNear( mYmax, -std::numeric_limits::max() ) ); } @@ -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::max(); + double mYmin = std::numeric_limits::max(); + double mXmax = -std::numeric_limits::max(); + double mYmax = -std::numeric_limits::max(); }; diff --git a/tests/src/core/geometry/testqgsrectangle.cpp b/tests/src/core/geometry/testqgsrectangle.cpp index 0972783339ef..24f4295f9c9d 100644 --- a/tests/src/core/geometry/testqgsrectangle.cpp +++ b/tests/src/core/geometry/testqgsrectangle.cpp @@ -72,11 +72,13 @@ void TestQgsRectangle::isNull() QVERIFY( QgsRectangle().isNull() ); QVERIFY( QgsRectangle( std::numeric_limits::quiet_NaN(), std::numeric_limits::quiet_NaN(), std::numeric_limits::quiet_NaN(), std::numeric_limits::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::max(), -std::numeric_limits::max(), std::numeric_limits::max(), -std::numeric_limits::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))" ) ); diff --git a/tests/src/python/test_qgsrectangle.py b/tests/src/python/test_qgsrectangle.py index fe1193372c86..8ad9a0f3917c 100644 --- a/tests/src/python/test_qgsrectangle.py +++ b/tests/src/python/test_qgsrectangle.py @@ -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')