Use larger precision for graduated values

This commit is contained in:
Joonalai 2025-06-05 13:07:05 +03:00 committed by Nyall Dawson
parent 654e1b9200
commit 1f9074a2c4
2 changed files with 28 additions and 26 deletions

View File

@ -1636,23 +1636,25 @@ QgsRuleBasedRenderer *QgsRuleBasedRenderer::convertFromRenderer( const QgsFeatur
auto rule = std::make_unique< QgsRuleBasedRenderer::Rule >( nullptr );
rule->setLabel( range.label() );
const QString upperValue = QString::number( range.upperValue(), 'f', 16 );
const QString lowerValue = QString::number( range.lowerValue(), 'f', 16 );
// Lower and upper boundaries have to be open-ended
if ( i == 0 )
{
expression = !isInverted ?
QStringLiteral( "%1 <= %2" ).arg( attr, QString::number( range.upperValue(), 'f' ) ) :
QStringLiteral( "%1 > %2" ).arg( attr, QString::number( range.lowerValue(), 'f' ) );
QStringLiteral( "%1 <= %2" ).arg( attr, upperValue ) :
QStringLiteral( "%1 > %2" ).arg( attr, lowerValue );
}
else if ( i == ranges.size() - 1 )
{
expression = !isInverted ?
QStringLiteral( "%1 > %2" ).arg( attr, QString::number( range.lowerValue(), 'f' ) ) :
QStringLiteral( "%1 <= %2" ).arg( attr, QString::number( range.upperValue(), 'f' ) );
QStringLiteral( "%1 > %2" ).arg( attr, lowerValue ) :
QStringLiteral( "%1 <= %2" ).arg( attr, upperValue );
}
else
{
expression = attr + " > " + QString::number( range.lowerValue(), 'f' ) + " AND " + \
attr + " <= " + QString::number( range.upperValue(), 'f' );
expression = attr + " > " + lowerValue + " AND " + \
attr + " <= " + upperValue;
}
rule->setFilterExpression( expression );

View File

@ -978,9 +978,9 @@ class TestQgsRuleBasedRenderer : public QgsTest
std::unique_ptr<QgsRuleBasedRenderer> r( QgsRuleBasedRenderer::convertFromRenderer( c.get(), layer.get() ) );
QCOMPARE( r->rootRule()->children().size(), 3 );
QCOMPARE( r->rootRule()->children()[0]->filterExpression(), "\"id\" <= 1.000000" );
QCOMPARE( r->rootRule()->children()[1]->filterExpression(), "\"id\" > 1.000000 AND \"id\" <= 2.000000" );
QCOMPARE( r->rootRule()->children()[2]->filterExpression(), "\"id\" > 2.000000" );
QCOMPARE( r->rootRule()->children()[0]->filterExpression(), "\"id\" <= 1.0000000000000000" );
QCOMPARE( r->rootRule()->children()[1]->filterExpression(), "\"id\" > 1.0000000000000000 AND \"id\" <= 2.0000000000000000" );
QCOMPARE( r->rootRule()->children()[2]->filterExpression(), "\"id\" > 2.0000000000000000" );
// Next try the same with inverted ranges
ranges.clear();
@ -991,9 +991,9 @@ class TestQgsRuleBasedRenderer : public QgsTest
r.reset( QgsRuleBasedRenderer::convertFromRenderer( c.get(), layer.get() ) );
QCOMPARE( r->rootRule()->children().size(), 3 );
QCOMPARE( r->rootRule()->children()[0]->filterExpression(), "\"id\" > 2.000000" );
QCOMPARE( r->rootRule()->children()[1]->filterExpression(), "\"id\" > 1.000000 AND \"id\" <= 2.000000" );
QCOMPARE( r->rootRule()->children()[2]->filterExpression(), "\"id\" <= 1.000000" );
QCOMPARE( r->rootRule()->children()[0]->filterExpression(), "\"id\" > 2.0000000000000000" );
QCOMPARE( r->rootRule()->children()[1]->filterExpression(), "\"id\" > 1.0000000000000000 AND \"id\" <= 2.0000000000000000" );
QCOMPARE( r->rootRule()->children()[2]->filterExpression(), "\"id\" <= 1.0000000000000000" );
// Next try with an expression based range
ranges.clear();
@ -1003,8 +1003,8 @@ class TestQgsRuleBasedRenderer : public QgsTest
r.reset( QgsRuleBasedRenderer::convertFromRenderer( c.get(), layer.get() ) );
QCOMPARE( r->rootRule()->children().size(), 2 );
QCOMPARE( r->rootRule()->children()[0]->filterExpression(), "(id / 2) <= 1.000000" );
QCOMPARE( r->rootRule()->children()[1]->filterExpression(), "(id / 2) > 1.000000" );
QCOMPARE( r->rootRule()->children()[0]->filterExpression(), "(id / 2) <= 1.0000000000000000" );
QCOMPARE( r->rootRule()->children()[1]->filterExpression(), "(id / 2) > 1.0000000000000000" );
// Last try with an expression which is just a quoted field name
ranges.clear();
@ -1014,8 +1014,8 @@ class TestQgsRuleBasedRenderer : public QgsTest
r.reset( QgsRuleBasedRenderer::convertFromRenderer( c.get(), layer.get() ) );
QCOMPARE( r->rootRule()->children().size(), 2 );
QCOMPARE( r->rootRule()->children()[0]->filterExpression(), "\"id\" <= 1.000000" );
QCOMPARE( r->rootRule()->children()[1]->filterExpression(), "\"id\" > 1.000000" );
QCOMPARE( r->rootRule()->children()[0]->filterExpression(), "\"id\" <= 1.0000000000000000" );
QCOMPARE( r->rootRule()->children()[1]->filterExpression(), "\"id\" > 1.0000000000000000" );
// Next try with a complex name
ranges.clear();
@ -1025,8 +1025,8 @@ class TestQgsRuleBasedRenderer : public QgsTest
r.reset( QgsRuleBasedRenderer::convertFromRenderer( c.get(), layer.get() ) );
QCOMPARE( r->rootRule()->children().size(), 2 );
QCOMPARE( r->rootRule()->children()[0]->filterExpression(), "\"fa_cy-fie+ld\" <= 1.000000" );
QCOMPARE( r->rootRule()->children()[1]->filterExpression(), "\"fa_cy-fie+ld\" > 1.000000" );
QCOMPARE( r->rootRule()->children()[0]->filterExpression(), "\"fa_cy-fie+ld\" <= 1.0000000000000000" );
QCOMPARE( r->rootRule()->children()[1]->filterExpression(), "\"fa_cy-fie+ld\" > 1.0000000000000000" );
}
void testConvertFromGraduatedRendererNoLayer()
@ -1055,8 +1055,8 @@ class TestQgsRuleBasedRenderer : public QgsTest
std::unique_ptr<QgsRuleBasedRenderer> r( QgsRuleBasedRenderer::convertFromRenderer( c.get() ) );
QCOMPARE( r->rootRule()->children().size(), 2 );
QCOMPARE( r->rootRule()->children()[0]->filterExpression(), "\"id\" <= 1.000000" );
QCOMPARE( r->rootRule()->children()[1]->filterExpression(), "\"id\" > 1.000000" );
QCOMPARE( r->rootRule()->children()[0]->filterExpression(), "\"id\" <= 1.0000000000000000" );
QCOMPARE( r->rootRule()->children()[1]->filterExpression(), "\"id\" > 1.0000000000000000" );
// Next try with an expression based range
ranges.clear();
@ -1066,8 +1066,8 @@ class TestQgsRuleBasedRenderer : public QgsTest
r.reset( QgsRuleBasedRenderer::convertFromRenderer( c.get() ) );
QCOMPARE( r->rootRule()->children().size(), 2 );
QCOMPARE( r->rootRule()->children()[0]->filterExpression(), "(id / 2) <= 1.000000" );
QCOMPARE( r->rootRule()->children()[1]->filterExpression(), "(id / 2) > 1.000000" );
QCOMPARE( r->rootRule()->children()[0]->filterExpression(), "(id / 2) <= 1.0000000000000000" );
QCOMPARE( r->rootRule()->children()[1]->filterExpression(), "(id / 2) > 1.0000000000000000" );
// Last try with an expression which is just a quoted field name
ranges.clear();
@ -1077,8 +1077,8 @@ class TestQgsRuleBasedRenderer : public QgsTest
r.reset( QgsRuleBasedRenderer::convertFromRenderer( c.get() ) );
QCOMPARE( r->rootRule()->children().size(), 2 );
QCOMPARE( r->rootRule()->children()[0]->filterExpression(), "\"id\" <= 1.000000" );
QCOMPARE( r->rootRule()->children()[1]->filterExpression(), "\"id\" > 1.000000" );
QCOMPARE( r->rootRule()->children()[0]->filterExpression(), "\"id\" <= 1.0000000000000000" );
QCOMPARE( r->rootRule()->children()[1]->filterExpression(), "\"id\" > 1.0000000000000000" );
// Next try with a complex name -- in this case since we don't have a layer or
// actual field names available, we must assume the complex field name is actually an expression
@ -1089,8 +1089,8 @@ class TestQgsRuleBasedRenderer : public QgsTest
r.reset( QgsRuleBasedRenderer::convertFromRenderer( c.get() ) );
QCOMPARE( r->rootRule()->children().size(), 2 );
QCOMPARE( r->rootRule()->children()[0]->filterExpression(), "(fa_cy-fie+ld) <= 1.000000" );
QCOMPARE( r->rootRule()->children()[1]->filterExpression(), "(fa_cy-fie+ld) > 1.000000" );
QCOMPARE( r->rootRule()->children()[0]->filterExpression(), "(fa_cy-fie+ld) <= 1.0000000000000000" );
QCOMPARE( r->rootRule()->children()[1]->filterExpression(), "(fa_cy-fie+ld) > 1.0000000000000000" );
}
void testConvertFromEmbedded()