Skip to content
Permalink
Browse files

Add contains check to QgsRasterRange

  • Loading branch information
nyalldawson committed Jun 7, 2018
1 parent d435a7a commit a69274ac6d81ba7dfee3c7b8f5e6a3dc93c1bf13
@@ -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
@@ -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;
}
@@ -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
@@ -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.
You can’t perform that action at this time.