Skip to content

Commit

Permalink
Fix logarithmic classification with narrow ranges
Browse files Browse the repository at this point in the history
Fix #45454
  • Loading branch information
elpaso committed Feb 9, 2022
1 parent 2db7926 commit ea93537
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
20 changes: 16 additions & 4 deletions src/core/classification/qgsclassificationlogarithmic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,15 +94,27 @@ QList<double> QgsClassificationLogarithmic::calculateBreaks( double &minimum, do
}

// get the min/max in log10 scale
const double logMin = std::floor( std::log10( positiveMinimum ) );
const double actualLogMin { std::log10( positiveMinimum ) };
double logMin = std::floor( actualLogMin );
const double logMax = std::ceil( std::log10( maximum ) );

// calculate pretty breaks
breaks.append( QgsSymbolLayerUtils::prettyBreaks( logMin, logMax, nclasses ) );
QList<double> prettyBreaks { QgsSymbolLayerUtils::prettyBreaks( logMin, logMax, nclasses ) };

// If case the first class greater than the actual log min increase the minimum log
while ( ! prettyBreaks.isEmpty() && prettyBreaks.first() < actualLogMin )
{
logMin += 1.0;
prettyBreaks = QgsSymbolLayerUtils::prettyBreaks( logMin, logMax, nclasses );
}

breaks.append( prettyBreaks );

// create the value
for ( int i = 0; i < breaks.count(); i++ )
{
breaks[i] = std::pow( 10, breaks.at( i ) );
}

return breaks;
}
Expand All @@ -121,7 +133,7 @@ QString QgsClassificationLogarithmic::valueToLabel( double value ) const
}
else
{
return QString( QStringLiteral( "10^%1" ) ).arg( std::log10( value ) );
return QString( QStringLiteral( "10^%L1" ) ).arg( std::log10( value ) );
}
}
}
Expand All @@ -142,7 +154,7 @@ QString QgsClassificationLogarithmic::labelForRange( double lowerValue, double u
break;
}

return labelFormat().arg( lowerLabel ).arg( upperLabel );
return labelFormat().arg( lowerLabel, upperLabel );
}

bool QgsClassificationLogarithmic::valuesRequired() const
Expand Down
20 changes: 20 additions & 0 deletions tests/src/python/test_qgsclassificationmethod.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,26 @@ def testQgsClassificationLogarithmic(self):

self.assertEqual(len(m.classes(vl, 'value', 4)), 4)

def testQgsClassificationLogarithmicCloseMinimum(self):
"""See issue GH #45454: Incorrect scale range legend after applying
logarithmic graduated symbology to a vector layer"""

values = [0.009900019065438,
0.010851322017611,
0.01755707784994,
0.031925433036994,
0.046422733606398]

vl = createMemoryLayer(values)

m = QgsClassificationLogarithmic()
r = m.classes(vl, 'value', 4)

classes = [(c.lowerBound(), c.upperBound()) for c in r]

for l, h in classes:
self.assertLess(l, h)

def testQgsClassificationLogarithmic_FilterZeroNeg(self):
values = [-2, 0, 1, 7, 66, 555, 4444]
vl = createMemoryLayer(values)
Expand Down

0 comments on commit ea93537

Please sign in to comment.