Skip to content

Commit

Permalink
Add contains check to QgsRasterRange
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jun 13, 2018
1 parent d435a7a commit a69274a
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 4 deletions.
7 changes: 7 additions & 0 deletions python/core/auto_generated/raster/qgsrasterrange.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,13 @@ in the range.

bool operator==( QgsRasterRange o ) const;

bool contains( double value ) const;
%Docstring
Returns true if this range contains the specified ``value``.

.. versionadded:: 3.2
%End

static bool contains( double value, const QgsRasterRangeList &rangeList );
%Docstring
Tests if a ``value`` is within the list of ranges
Expand Down
17 changes: 13 additions & 4 deletions src/core/raster/qgsrasterrange.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,22 @@ QgsRasterRange::QgsRasterRange( double min, double max, BoundsType bounds )
{
}

bool QgsRasterRange::contains( double value ) const
{
return ( value > mMin
|| ( !std::isnan( mMin ) && qgsDoubleNear( value, mMin ) && ( mType == IncludeMinAndMax || mType == IncludeMin ) )
|| std::isnan( mMin ) )
&&
( value < mMax
|| ( !std::isnan( mMax ) && qgsDoubleNear( value, mMax ) && ( mType == IncludeMinAndMax || mType == IncludeMax ) )
|| std::isnan( mMax ) );
}

bool QgsRasterRange::contains( double value, const QgsRasterRangeList &rangeList )
{
Q_FOREACH ( QgsRasterRange range, rangeList )
for ( QgsRasterRange range : rangeList )
{
if ( ( value >= range.mMin && value <= range.mMax ) ||
qgsDoubleNear( value, range.mMin ) ||
qgsDoubleNear( value, range.mMax ) )
if ( range.contains( value ) )
{
return true;
}
Expand Down
6 changes: 6 additions & 0 deletions src/core/raster/qgsrasterrange.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,12 @@ class CORE_EXPORT QgsRasterRange
&& mType == o.mType;
}

/**
* Returns true if this range contains the specified \a value.
* \since QGIS 3.2
*/
bool contains( double value ) const;

/**
* \brief Tests if a \a value is within the list of ranges
* \param value value
Expand Down
47 changes: 47 additions & 0 deletions tests/src/python/test_qgsrasterrange.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,53 @@ def testEquality(self):
range2.setMax(5)
self.assertEqual(range, range2)

def testContains(self):
range = QgsRasterRange(1, 5)
self.assertTrue(range.contains(1))
self.assertTrue(range.contains(5))
self.assertTrue(range.contains(4))
self.assertTrue(range.contains(1.00001))
self.assertTrue(range.contains(4.99999))
self.assertFalse(range.contains(0.99999))
self.assertFalse(range.contains(5.00001))

# with nan min/maxs
range = QgsRasterRange()
self.assertTrue(range.contains(1))
self.assertTrue(range.contains(-909999999))
self.assertTrue(range.contains(999999999))
range.setMin(5)
self.assertFalse(range.contains(0))
self.assertTrue(range.contains(5))
self.assertTrue(range.contains(10000000))

range = QgsRasterRange()
range.setMax(5)
self.assertFalse(range.contains(6))
self.assertTrue(range.contains(5))
self.assertTrue(range.contains(-99999))

range = QgsRasterRange(1, 5, QgsRasterRange.IncludeMax)
self.assertFalse(range.contains(0))
self.assertFalse(range.contains(1))
self.assertTrue(range.contains(2))
self.assertTrue(range.contains(5))
self.assertFalse(range.contains(6))

range = QgsRasterRange(1, 5, QgsRasterRange.IncludeMin)
self.assertFalse(range.contains(0))
self.assertTrue(range.contains(1))
self.assertTrue(range.contains(2))
self.assertFalse(range.contains(5))
self.assertFalse(range.contains(6))

range = QgsRasterRange(1, 5, QgsRasterRange.Exclusive)
self.assertFalse(range.contains(0))
self.assertFalse(range.contains(1))
self.assertTrue(range.contains(2))
self.assertFalse(range.contains(5))
self.assertFalse(range.contains(6))


if __name__ == '__main__':
unittest.main()

0 comments on commit a69274a

Please sign in to comment.