Skip to content

Commit

Permalink
limit number of classes to minimum 2 and remove special cases for < 2
Browse files Browse the repository at this point in the history
  • Loading branch information
pierstitus authored and nyalldawson committed Jun 2, 2016
1 parent cf72b59 commit 02e7d7a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 54 deletions.
84 changes: 31 additions & 53 deletions src/gui/raster/qgssinglebandpseudocolorrendererwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ void QgsSingleBandPseudoColorRendererWidget::on_mClassifyButton_clicked()

//int bandNr = mBandComboBox->itemData( bandComboIndex ).toInt();
//QgsRasterBandStats myRasterBandStats = mRasterLayer->dataProvider()->bandStatistics( bandNr );
int numberOfEntries = 0;
int numberOfEntries;

bool discrete = mColorInterpolationComboBox->currentText() == tr( "Discrete" );

Expand Down Expand Up @@ -408,6 +408,7 @@ void QgsSingleBandPseudoColorRendererWidget::on_mClassifyButton_clicked()
else // for other classification modes interpolate colors linearly
{
numberOfEntries = mNumberOfEntriesSpinBox->value();
if ( numberOfEntries < 2 ) return; // < 2 classes is not useful, shouldn't happen, but if it happens save it from crashing

if ( mClassificationModeComboBox->itemData( mClassificationModeComboBox->currentIndex() ).toInt() == Quantile )
{ // Quantile
Expand All @@ -423,74 +424,51 @@ void QgsSingleBandPseudoColorRendererWidget::on_mClassifyButton_clicked()
// set min and max from histogram, used later to calculate number of decimals to display
mRasterLayer->dataProvider()->cumulativeCut( bandNr, 0.0, 1.0, min, max, extent, sampleSize );

double intervalDiff;
if ( numberOfEntries > 1 )
entryValues.reserve( numberOfEntries );
if ( discrete )
{
entryValues.reserve( numberOfEntries );
if ( discrete )
double intervalDiff = 1.0 / ( numberOfEntries );
for ( int i = 1; i < numberOfEntries; ++i )
{
intervalDiff = 1.0 / ( numberOfEntries );
for ( int i = 1; i < numberOfEntries; ++i )
{
mRasterLayer->dataProvider()->cumulativeCut( bandNr, 0.0, i * intervalDiff, cut1, cut2, extent, sampleSize );
entryValues.push_back( cut2 );
}
entryValues.push_back( std::numeric_limits<double>::infinity() );
}
else
{
intervalDiff = 1.0 / ( numberOfEntries - 1 );
for ( int i = 0; i < numberOfEntries; ++i )
{
mRasterLayer->dataProvider()->cumulativeCut( bandNr, 0.0, i * intervalDiff, cut1, cut2, extent, sampleSize );
entryValues.push_back( cut2 );
}
mRasterLayer->dataProvider()->cumulativeCut( bandNr, 0.0, i * intervalDiff, cut1, cut2, extent, sampleSize );
entryValues.push_back( cut2 );
}
entryValues.push_back( std::numeric_limits<double>::infinity() );
}
else if ( numberOfEntries == 1 )
else
{
mRasterLayer->dataProvider()->cumulativeCut( bandNr, 0.0, 0.5, cut1, cut2, extent, sampleSize );
entryValues.push_back( cut2 );
double intervalDiff = 1.0 / ( numberOfEntries - 1 );
for ( int i = 0; i < numberOfEntries; ++i )
{
mRasterLayer->dataProvider()->cumulativeCut( bandNr, 0.0, i * intervalDiff, cut1, cut2, extent, sampleSize );
entryValues.push_back( cut2 );
}
}
}
else // EqualInterval
{
if ( numberOfEntries > 1 )
entryValues.reserve( numberOfEntries );
if ( discrete )
{
entryValues.reserve( numberOfEntries );
if ( discrete )
{
// in discrete mode the lowest value is not an entry and the highest
// value is inf, there are ( numberOfEntries ) of which the first
// and last are not used.
double intervalDiff = ( max - min ) / ( numberOfEntries );
// in discrete mode the lowest value is not an entry and the highest
// value is inf, there are ( numberOfEntries ) of which the first
// and last are not used.
double intervalDiff = ( max - min ) / ( numberOfEntries );

for ( int i = 1; i < numberOfEntries; ++i )
{
entryValues.push_back( min + i * intervalDiff );
}
entryValues.push_back( std::numeric_limits<double>::infinity() );
}
else
for ( int i = 1; i < numberOfEntries; ++i )
{
//because the highest value is also an entry, there are (numberOfEntries - 1) intervals
double intervalDiff = ( max - min ) / ( numberOfEntries - 1 );

for ( int i = 0; i < numberOfEntries; ++i )
{
entryValues.push_back( min + i * intervalDiff );
}
entryValues.push_back( min + i * intervalDiff );
}
entryValues.push_back( std::numeric_limits<double>::infinity() );
}
else if ( numberOfEntries == 1 )
else
{
if ( discrete )
{
entryValues.push_back( std::numeric_limits<double>::infinity() );
}
else
//because the highest value is also an entry, there are (numberOfEntries - 1) intervals
double intervalDiff = ( max - min ) / ( numberOfEntries - 1 );

for ( int i = 0; i < numberOfEntries; ++i )
{
entryValues.push_back(( max + min ) / 2 );
entryValues.push_back( min + i * intervalDiff );
}
}
}
Expand Down
5 changes: 4 additions & 1 deletion src/ui/qgssinglebandpseudocolorrendererwidgetbase.ui
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,11 @@
<property name="maximum">
<number>255</number>
</property>
<property name="minimum">
<number>2</number>
</property>
<property name="value">
<number>0</number>
<number>5</number>
</property>
</widget>
</item>
Expand Down

0 comments on commit 02e7d7a

Please sign in to comment.