From 7c014e606c8091e8ce2ac058e58baa7594c2406f Mon Sep 17 00:00:00 2001 From: Sandro Santilli Date: Tue, 17 Oct 2023 17:01:11 +0200 Subject: [PATCH] Handle Null rectangle in QgsRectangle::buffered Includes unit test --- src/core/geometry/qgsrectangle.h | 2 ++ tests/src/core/geometry/testqgsrectangle.cpp | 30 +++++++++++--------- 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/src/core/geometry/qgsrectangle.h b/src/core/geometry/qgsrectangle.h index da3cb3ae932e..8aa210cef45a 100644 --- a/src/core/geometry/qgsrectangle.h +++ b/src/core/geometry/qgsrectangle.h @@ -349,6 +349,8 @@ class CORE_EXPORT QgsRectangle */ QgsRectangle buffered( double width ) const { + if ( isNull() ) + return QgsRectangle(); return QgsRectangle( mXmin - width, mYmin - width, mXmax + width, mYmax + width ); } diff --git a/tests/src/core/geometry/testqgsrectangle.cpp b/tests/src/core/geometry/testqgsrectangle.cpp index 1adf3922b808..c9d449a52e02 100644 --- a/tests/src/core/geometry/testqgsrectangle.cpp +++ b/tests/src/core/geometry/testqgsrectangle.cpp @@ -461,19 +461,23 @@ void TestQgsRectangle::include() void TestQgsRectangle::buffered() { - QgsRectangle rect = QgsRectangle( 10.0, 20.0, 110.0, 220.0 ); - const QgsRectangle rect1 = rect.buffered( 11 ); - QCOMPARE( rect1.xMinimum(), -1.0 ); - QCOMPARE( rect1.yMinimum(), 9.0 ); - QCOMPARE( rect1.xMaximum(), 121.0 ); - QCOMPARE( rect1.yMaximum(), 231.0 ); - - rect = QgsRectangle( -110.0, -220.0, -10.0, -20.0 ); - const QgsRectangle rect2 = rect.buffered( 11 ); - QCOMPARE( rect2.xMinimum(), -121.0 ); - QCOMPARE( rect2.yMinimum(), -231.0 ); - QCOMPARE( rect2.xMaximum(), 1.0 ); - QCOMPARE( rect2.yMaximum(), -9.0 ); + QgsRectangle rectIn = QgsRectangle( 10.0, 20.0, 110.0, 220.0 ); + QgsRectangle rectOut = rectIn.buffered( 11 ); + QCOMPARE( rectOut.xMinimum(), -1.0 ); + QCOMPARE( rectOut.yMinimum(), 9.0 ); + QCOMPARE( rectOut.xMaximum(), 121.0 ); + QCOMPARE( rectOut.yMaximum(), 231.0 ); + + rectIn = QgsRectangle( -110.0, -220.0, -10.0, -20.0 ); + rectOut = rectIn.buffered( 11 ); + QCOMPARE( rectOut.xMinimum(), -121.0 ); + QCOMPARE( rectOut.yMinimum(), -231.0 ); + QCOMPARE( rectOut.xMaximum(), 1.0 ); + QCOMPARE( rectOut.yMaximum(), -9.0 ); + + rectIn.setMinimal(); + rectOut = rectIn.buffered( 11 ); + QVERIFY( rectOut.isNull() ); } void TestQgsRectangle::isFinite()