Skip to content

Commit

Permalink
Add QgsRasterRange::asText for a text representation of a range
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jun 13, 2018
1 parent f934c5c commit 0f0a7dc
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 0 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 @@ -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

Expand Down
10 changes: 10 additions & 0 deletions src/core/raster/qgsrasterrange.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}




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 @@ -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();
Expand Down
18 changes: 18 additions & 0 deletions tests/src/python/test_qgsrasterrange.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.