Skip to content

Commit

Permalink
Add bounds type handling to QgsRasterRange
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jun 13, 2018
1 parent efb44ad commit d435a7a
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 4 deletions.
35 changes: 34 additions & 1 deletion python/core/auto_generated/raster/qgsrasterrange.sip.in
Expand Up @@ -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;
Expand All @@ -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 );
Expand All @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion src/core/raster/qgsrasterrange.cpp
Expand Up @@ -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 )
{
}

Expand Down
36 changes: 34 additions & 2 deletions src/core/raster/qgsrasterrange.h
Expand Up @@ -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.
Expand All @@ -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()
Expand All @@ -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;
}

/**
Expand All @@ -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
Expand Down
7 changes: 7 additions & 0 deletions tests/src/python/test_qgsrasterrange.py
Expand Up @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit d435a7a

Please sign in to comment.