Skip to content
Permalink
Browse files

Add bounds type handling to QgsRasterRange

  • Loading branch information
nyalldawson committed Jun 7, 2018
1 parent efb44ad commit d435a7a0fbb8f9aa59d80ae50f6cfb2f2f093ceb
@@ -24,14 +24,25 @@ including min and max value.
%End
public:

enum BoundsType
{
IncludeMinAndMax,
IncludeMax,
IncludeMin,
Exclusive,
};

QgsRasterRange();
%Docstring
Default constructor, both min and max value for the range will be set to NaN.
%End

QgsRasterRange( double min, double max );
QgsRasterRange( double min, double max, BoundsType bounds = IncludeMinAndMax );
%Docstring
Constructor for a range with the given ``min`` and ``max`` values.

The ``bounds`` argument dictates how the min and max value themselves
will be handled by the range.
%End

double min() const;
@@ -46,6 +57,17 @@ Returns the minimum value for the range.
Returns the maximum value for the range.

.. seealso:: :py:func:`setMax`
%End

BoundsType bounds() const;
%Docstring
Returns the bounds type for the range, which specifies
whether or not the min and max values themselves are included
in the range.

.. seealso:: :py:func:`setBounds`

.. versionadded:: 3.4
%End

double setMin( double min );
@@ -60,6 +82,17 @@ Sets the minimum value for the range.
Sets the maximum value for the range.

.. seealso:: :py:func:`max`
%End

void setBounds( BoundsType type );
%Docstring
Setss the bounds ``type`` for the range, which specifies
whether or not the min and max values themselves are included
in the range.

.. seealso:: :py:func:`bounds`

.. versionadded:: 3.4
%End

bool operator==( QgsRasterRange o ) const;
@@ -17,9 +17,10 @@

#include "qgsrasterrange.h"

QgsRasterRange::QgsRasterRange( double min, double max )
QgsRasterRange::QgsRasterRange( double min, double max, BoundsType bounds )
: mMin( min )
, mMax( max )
, mType( bounds )
{
}

@@ -36,15 +36,27 @@ class CORE_EXPORT QgsRasterRange
{
public:

//! Handling for min and max bounds
enum BoundsType
{
IncludeMinAndMax = 0, //!< Min and max values are inclusive
IncludeMax, //!< Include the max value, but not the min value, e.g. min < value <= max
IncludeMin, //!< Include the min value, but not the max value, e.g. min <= value < max
Exclusive, //!< Don't include either the min or max value, e.g. min < value < max
};

/**
* Default constructor, both min and max value for the range will be set to NaN.
*/
QgsRasterRange() = default;

/**
* Constructor for a range with the given \a min and \a max values.
*
* The \a bounds argument dictates how the min and max value themselves
* will be handled by the range.
*/
QgsRasterRange( double min, double max );
QgsRasterRange( double min, double max, BoundsType bounds = IncludeMinAndMax );

/**
* Returns the minimum value for the range.
@@ -58,6 +70,15 @@ class CORE_EXPORT QgsRasterRange
*/
double max() const { return mMax; }

/**
* Returns the bounds type for the range, which specifies
* whether or not the min and max values themselves are included
* in the range.
* \see setBounds()
* \since QGIS 3.4
*/
BoundsType bounds() const { return mType; }

/**
* Sets the minimum value for the range.
* \see min()
@@ -70,10 +91,20 @@ class CORE_EXPORT QgsRasterRange
*/
double setMax( double max ) { return mMax = max; }

/**
* Setss the bounds \a type for the range, which specifies
* whether or not the min and max values themselves are included
* in the range.
* \see bounds()
* \since QGIS 3.4
*/
void setBounds( BoundsType type ) { mType = type; }

inline bool operator==( QgsRasterRange o ) const
{
return ( ( std::isnan( mMin ) && std::isnan( o.mMin ) ) || qgsDoubleNear( mMin, o.mMin ) )
&& ( ( std::isnan( mMax ) && std::isnan( o.mMax ) ) || qgsDoubleNear( mMax, o.mMax ) );
&& ( ( std::isnan( mMax ) && std::isnan( o.mMax ) ) || qgsDoubleNear( mMax, o.mMax ) )
&& mType == o.mType;
}

/**
@@ -87,6 +118,7 @@ class CORE_EXPORT QgsRasterRange
private:
double mMin = std::numeric_limits<double>::quiet_NaN();
double mMax = std::numeric_limits<double>::quiet_NaN();
BoundsType mType = IncludeMinAndMax;
};

#endif
@@ -29,6 +29,9 @@ def testBasic(self):
range.setMax(10.4)
self.assertEqual(range.min(), 2.2)
self.assertEqual(range.max(), 10.4)
self.assertEqual(range.bounds(), QgsRasterRange.IncludeMinAndMax)
range.setBounds(QgsRasterRange.IncludeMin)
self.assertEqual(range.bounds(), QgsRasterRange.IncludeMin)

def testEquality(self):
range = QgsRasterRange(1, 5)
@@ -41,6 +44,10 @@ def testEquality(self):
self.assertNotEqual(range, range2)
range2.setMax(5)
self.assertEqual(range, range2)
range.setBounds(QgsRasterRange.IncludeMax)
self.assertNotEqual(range, range2)
range2.setBounds(QgsRasterRange.IncludeMax)
self.assertEqual(range, range2)
range = QgsRasterRange()
range2 = QgsRasterRange()
self.assertEqual(range, range2)

0 comments on commit d435a7a

Please sign in to comment.
You can’t perform that action at this time.