Skip to content

Commit

Permalink
Fixes for refinement of rules.
Browse files Browse the repository at this point in the history
- quoting of columns (#5536 and #5542)
- quoting of string
- do not quote of numbers (avoids type cast during evaluation)
- use at least 4 floating point digits for doubles
  • Loading branch information
wonder-sk committed Jun 1, 2012
1 parent 1125571 commit bf86521
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 4 deletions.
2 changes: 2 additions & 0 deletions python/core/qgsexpression.sip
Expand Up @@ -122,6 +122,8 @@ public:

//! return quoted column reference (in double quotes)
static QString quotedColumnRef( QString name );
//! return quoted string (in single quotes)
static QString quotedString( QString text );

//////

Expand Down
2 changes: 2 additions & 0 deletions src/core/qgsexpression.h
Expand Up @@ -209,6 +209,8 @@ class CORE_EXPORT QgsExpression

//! return quoted column reference (in double quotes)
static QString quotedColumnRef( QString name ) { return QString( "\"%1\"" ).arg( name.replace( "\"", "\"\"" ) ); }
//! return quoted string (in single quotes)
static QString quotedString( QString text ) { return QString( "'%1'" ).arg( text.replace( "'", "''" ) ); }

//////

Expand Down
20 changes: 18 additions & 2 deletions src/core/symbology-ng/qgsrulebasedrendererv2.cpp
Expand Up @@ -803,7 +803,18 @@ void QgsRuleBasedRendererV2::refineRuleCategories( QgsRuleBasedRendererV2::Rule*
{
foreach( const QgsRendererCategoryV2& cat, r->categories() )
{
QString filter = QString( "%1 = '%2'" ).arg( r->classAttribute() ).arg( cat.value().toString() );
QString attr = QgsExpression::quotedColumnRef( r->classAttribute() );
QString value;
// not quoting numbers saves a type cast
if ( cat.value().type() == QVariant::Int )
value = cat.value().toString();
else if ( cat.value().type() == QVariant::Double )
// we loose precision here - so we may miss some categories :-(
// TODO: have a possibility to construct expressions directly as a parse tree to avoid loss of precision
value = QString::number( cat.value().toDouble(), 'f', 4 );
else
value = QgsExpression::quotedString( cat.value().toString() );
QString filter = QString( "%1 = %2" ).arg( attr ).arg( value );
QString label = filter;
initialRule->appendChild( new Rule( cat.symbol()->clone(), 0, 0, filter, label ) );
}
Expand All @@ -813,7 +824,12 @@ void QgsRuleBasedRendererV2::refineRuleRanges( QgsRuleBasedRendererV2::Rule* ini
{
foreach( const QgsRendererRangeV2& rng, r->ranges() )
{
QString filter = QString( "%1 >= '%2' AND %1 <= '%3'" ).arg( r->classAttribute() ).arg( rng.lowerValue() ).arg( rng.upperValue() );
// due to the loss of precision in double->string conversion we may miss out values at the limit of the range
// TODO: have a possibility to construct expressions directly as a parse tree to avoid loss of precision
QString attr = QgsExpression::quotedColumnRef( r->classAttribute() );
QString filter = QString( "%1 >= %2 AND %1 <= %3" ).arg( attr )
.arg( QString::number( rng.lowerValue(), 'f', 4 ) )
.arg( QString::number( rng.upperValue(), 'f', 4 ) );
QString label = filter;
initialRule->appendChild( new Rule( rng.symbol()->clone(), 0, 0, filter, label ) );
}
Expand Down
4 changes: 2 additions & 2 deletions src/gui/symbology-ng/qgsgraduatedsymbolrendererv2widget.cpp
Expand Up @@ -325,8 +325,8 @@ void QgsGraduatedSymbolRendererV2Widget::changeRange( int rangeIdx )
QgsLUDialog dialog( this );

const QgsRendererRangeV2& range = mRenderer->ranges()[rangeIdx];
dialog.setLowerValue( QString( "%1" ).arg( range.lowerValue() ) );
dialog.setUpperValue( QString( "%1" ).arg( range.upperValue() ) );
dialog.setLowerValue( QString::number( range.lowerValue(), 'f', 4 ) );
dialog.setUpperValue( QString::number( range.upperValue(), 'f', 4 ) );

if ( dialog.exec() == QDialog::Accepted )
{
Expand Down

0 comments on commit bf86521

Please sign in to comment.