Skip to content

Commit

Permalink
Add a bit of tolerance to graduated symbol range resolving
Browse files Browse the repository at this point in the history
If a value doesn't fall exactly within a range, then try to see
if it falls just outside of any ranges (within double precision
tolerance), to correctly handle double precision values coming
from different sources.

Fixes #27420

(cherry picked from commit e9d51e5)
  • Loading branch information
nyalldawson committed Nov 14, 2019
1 parent 4033bd4 commit ac66b07
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/core/symbology/qgsgraduatedsymbolrenderer.cpp
Expand Up @@ -84,6 +84,20 @@ const QgsRendererRange *QgsGraduatedSymbolRenderer::rangeForValue( double value
return nullptr;
}
}

// second chance -- use a bit of double tolerance to avoid floating point equality fuzziness
// if a value falls just outside of a range, but within acceptable double precision tolerance
// then we accept it anyway
for ( const QgsRendererRange &range : mRanges )
{
if ( qgsDoubleNear( range.lowerValue(), value ) || qgsDoubleNear( range.upperValue(), value ) )
{
if ( range.renderState() || mCounting )
return ⦥
else
return nullptr;
}
}
// the value is out of the range: return NULL instead of symbol
return nullptr;
}
Expand Down
4 changes: 4 additions & 0 deletions tests/src/core/testqgsgraduatedsymbolrenderer.cpp
Expand Up @@ -309,6 +309,10 @@ void TestQgsGraduatedSymbolRenderer::testMatchingRangeForValue()
QCOMPARE( renderer.legendKeyForValue( 3.25 ), QStringLiteral( "1" ) );
QCOMPARE( renderer.legendKeyForValue( 3.7 ), QStringLiteral( "3" ) );
QVERIFY( renderer.legendKeyForValue( 3.5 ).isEmpty() );

// test values which fall just outside ranges, e.g. due to double precision (refs https://github.com/qgis/QGIS/issues/27420)
QCOMPARE( renderer.rangeForValue( 1.1 - std::numeric_limits<double>::epsilon() * 2 )->label(), QStringLiteral( "r1" ) );
QCOMPARE( renderer.rangeForValue( 3.7 + std::numeric_limits<double>::epsilon() * 2 )->label(), QStringLiteral( "r4" ) );
}

QGSTEST_MAIN( TestQgsGraduatedSymbolRenderer )
Expand Down

0 comments on commit ac66b07

Please sign in to comment.