Skip to content

Commit

Permalink
Fix #8758 (graduated classes on expression broken)
Browse files Browse the repository at this point in the history
  • Loading branch information
wonder-sk committed Feb 12, 2014
1 parent 18b1bdb commit a646101
Showing 1 changed file with 23 additions and 7 deletions.
30 changes: 23 additions & 7 deletions src/core/symbology-ng/qgsgraduatedsymbolrendererv2.cpp
Expand Up @@ -641,6 +641,9 @@ static QList<double> _calcJenksBreaks( QList<double> values, int classes,
// Returns class breaks such that classes are internally homogeneous while
// assuring heterogeneity among classes.

if ( !values.count() )
return QList<double>();

if ( classes <= 1 )
{
return QList<double>() << maximum;
Expand Down Expand Up @@ -772,15 +775,22 @@ QgsGraduatedSymbolRendererV2* QgsGraduatedSymbolRendererV2::createRenderer(
int attrNum = vlayer->fieldNameIndex( attrName );
double minimum;
double maximum;

QScopedPointer<QgsExpression> expression;

if ( attrNum == -1 )
{
// try to use expression
expression.reset( new QgsExpression( attrName ) );
if ( expression->hasParserError() || !expression->prepare( vlayer->pendingFields() ) )
return 0; // should have a means to report errors

QList<double> values;
QgsFeatureIterator fit = vlayer->getFeatures();
QgsFeature feature;
QgsExpression expression( attrName );
while ( fit.nextFeature( feature ) )
{
values << expression.evaluate( feature ).toDouble();
values << expression->evaluate( feature ).toDouble();
}
qSort( values );
minimum = values.first();
Expand Down Expand Up @@ -808,15 +818,21 @@ QgsGraduatedSymbolRendererV2* QgsGraduatedSymbolRendererV2::createRenderer(
// get values from layer
QList<double> values;
QgsFeature f;
QgsAttributeList lst;
lst.append( attrNum );
QStringList lst;
if ( expression.isNull() )
lst.append( attrName );
else
lst = expression->referencedColumns();

QgsFeatureIterator fit = vlayer->getFeatures( QgsFeatureRequest().setFlags( QgsFeatureRequest::NoGeometry ).setSubsetOfAttributes( lst ) );
QgsFeatureIterator fit = vlayer->getFeatures( QgsFeatureRequest().setFlags( QgsFeatureRequest::NoGeometry ).setSubsetOfAttributes( lst, vlayer->pendingFields() ) );

// create list of non-null attribute values
while ( fit.nextFeature( f ) )
if ( !f.attribute( attrNum ).isNull() )
values.append( f.attribute( attrNum ).toDouble() );
{
QVariant v = expression.isNull() ? f.attribute( attrNum ) : expression->evaluate( f );
if ( !v.isNull() )
values.append( v.toDouble() );
}

// calculate the breaks
if ( mode == Quantile )
Expand Down

0 comments on commit a646101

Please sign in to comment.