Skip to content
Permalink
Browse files

Add QgsRasterRange::asText for a text representation of a range

  • Loading branch information
nyalldawson committed Jun 12, 2018
1 parent f934c5c commit 0f0a7dc64a578c533909a6438e3d1b6cdab1fa31
@@ -118,6 +118,13 @@ Tests if a ``value`` is within the list of ranges
%Docstring
Returns true if this range overlaps another range.

.. versionadded:: 3.2
%End

QString asText() const;
%Docstring
Returns a text representation of the range.

.. versionadded:: 3.2
%End

@@ -65,6 +65,16 @@ bool QgsRasterRange::overlaps( const QgsRasterRange &other ) const
return false;
}

QString QgsRasterRange::asText() const
{
const QString minText = std::isnan( mMin ) ? QStringLiteral( "-%1" ).arg( QChar( 0x221e ) ) : QString::number( mMin );
const QString maxText = std::isnan( mMax ) ? QChar( 0x221e ) : QString::number( mMax );
const QString operator1 = ( mType == IncludeMinAndMax || mType == IncludeMin ) ? QChar( 0x2264 ) : '<';
const QString operator2 = ( mType == IncludeMinAndMax || mType == IncludeMax ) ? QChar( 0x2264 ) : '<';

return QStringLiteral( "%1 %2 x %3 %4" ).arg( minText, operator1, operator2, maxText );
}




@@ -146,6 +146,12 @@ class CORE_EXPORT QgsRasterRange
*/
bool overlaps( const QgsRasterRange &other ) const;

/**
* Returns a text representation of the range.
* \since QGIS 3.2
*/
QString asText() const;

private:
double mMin = std::numeric_limits<double>::quiet_NaN();
double mMax = std::numeric_limits<double>::quiet_NaN();
@@ -320,6 +320,24 @@ def testOverlaps(self):
self.assertFalse(range.overlaps(QgsRasterRange(10, 11, QgsRasterRange.IncludeMin)))
self.assertFalse(range.overlaps(QgsRasterRange(10, 11, QgsRasterRange.IncludeMax)))

def testAsText(self):
self.assertEqual(QgsRasterRange(0, 10, QgsRasterRange.IncludeMinAndMax).asText(), '0 ≤ x ≤ 10')
self.assertEqual(QgsRasterRange(-1, float('NaN')).asText(), '-1 ≤ x ≤ ∞')
self.assertEqual(QgsRasterRange(float('NaN'), 5).asText(), '-∞ ≤ x ≤ 5')
self.assertEqual(QgsRasterRange(float('NaN'), float('NaN')).asText(), '-∞ ≤ x ≤ ∞')
self.assertEqual(QgsRasterRange(0, 10, QgsRasterRange.IncludeMin).asText(), '0 ≤ x < 10')
self.assertEqual(QgsRasterRange(-1, float('NaN'), QgsRasterRange.IncludeMin).asText(), '-1 ≤ x < ∞')
self.assertEqual(QgsRasterRange(float('NaN'), 5, QgsRasterRange.IncludeMin).asText(), '-∞ ≤ x < 5')
self.assertEqual(QgsRasterRange(float('NaN'), float('NaN'), QgsRasterRange.IncludeMin).asText(), '-∞ ≤ x < ∞')
self.assertEqual(QgsRasterRange(0, 10, QgsRasterRange.IncludeMax).asText(), '0 < x ≤ 10')
self.assertEqual(QgsRasterRange(-1, float('NaN'), QgsRasterRange.IncludeMax).asText(), '-1 < x ≤ ∞')
self.assertEqual(QgsRasterRange(float('NaN'), 5, QgsRasterRange.IncludeMax).asText(), '-∞ < x ≤ 5')
self.assertEqual(QgsRasterRange(float('NaN'), float('NaN'), QgsRasterRange.IncludeMax).asText(), '-∞ < x ≤ ∞')
self.assertEqual(QgsRasterRange(0, 10, QgsRasterRange.Exclusive).asText(), '0 < x < 10')
self.assertEqual(QgsRasterRange(-1, float('NaN'), QgsRasterRange.Exclusive).asText(), '-1 < x < ∞')
self.assertEqual(QgsRasterRange(float('NaN'), 5, QgsRasterRange.Exclusive).asText(), '-∞ < x < 5')
self.assertEqual(QgsRasterRange(float('NaN'), float('NaN'), QgsRasterRange.Exclusive).asText(), '-∞ < x < ∞')


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

0 comments on commit 0f0a7dc

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