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
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
  • Loading branch information
strk committed Oct 9, 2023
1 parent a0fc443 commit 0c80066
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 12 deletions.
4 changes: 3 additions & 1 deletion python/core/auto_generated/geometry/qgsrectangle.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,9 @@ Returns the distance from ``point`` to the nearest point on the boundary of the
bool isEmpty() const;
%Docstring
Returns ``True`` if the rectangle is empty.
An empty rectangle may still be non-null if it contains valid information (e.g. bounding box of a point).
An empty rectangle may still be non-null if it contains valid
information (e.g. bounding box of a point or of a vertical or
or horizontal line).
%End

bool isNull() const;
Expand Down
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
15 changes: 8 additions & 7 deletions src/core/geometry/qgsrectangle.h
Original file line number Diff line number Diff line change
Expand Up @@ -464,11 +464,13 @@ class CORE_EXPORT QgsRectangle

/**
* Returns TRUE if the rectangle is empty.
* An empty rectangle may still be non-null if it contains valid information (e.g. bounding box of a point).
* An empty rectangle may still be non-null if it contains valid
* information (e.g. bounding box of a point or of a vertical or
* or horizontal line).
*/
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 @@ -480,7 +482,6 @@ class CORE_EXPORT QgsRectangle
{
// rectangle created QgsRectangle() or with rect.setMinimal() ?
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 @@ -612,10 +613,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 @@ -73,11 +73,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 0c80066

Please sign in to comment.