Skip to content

Commit

Permalink
Handle Null rectangle in QgsRectangle::buffered
Browse files Browse the repository at this point in the history
Includes unit test
  • Loading branch information
strk authored and nyalldawson committed Oct 19, 2023
1 parent c20c395 commit 7c014e6
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
2 changes: 2 additions & 0 deletions src/core/geometry/qgsrectangle.h
Expand Up @@ -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 );
}

Expand Down
30 changes: 17 additions & 13 deletions tests/src/core/geometry/testqgsrectangle.cpp
Expand Up @@ -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()
Expand Down

0 comments on commit 7c014e6

Please sign in to comment.