Skip to content

Commit 624b06b

Browse files
pierstitusnyalldawson
authored andcommitted
Different color map classification handling in discrete mode
1 parent c84dc8d commit 624b06b

File tree

1 file changed

+79
-42
lines changed

1 file changed

+79
-42
lines changed

src/gui/raster/qgssinglebandpseudocolorrendererwidget.cpp

+79-42
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,10 @@ void QgsSingleBandPseudoColorRendererWidget::autoLabel()
227227
{
228228
label = "<= " + currentItem->text( ValueColumn ) + unit;
229229
}
230+
else if ( currentItem->text( ValueColumn ).toDouble() == std::numeric_limits<double>::infinity() )
231+
{
232+
label = "> " + mColormapTreeWidget->topLevelItem( i - 1 )->text( ValueColumn );
233+
}
230234
else
231235
{
232236
label = mColormapTreeWidget->topLevelItem( i - 1 )->text( ValueColumn ) + " - " + currentItem->text( ValueColumn ) + unit;
@@ -345,6 +349,8 @@ void QgsSingleBandPseudoColorRendererWidget::on_mClassifyButton_clicked()
345349
//QgsRasterBandStats myRasterBandStats = mRasterLayer->dataProvider()->bandStatistics( bandNr );
346350
int numberOfEntries = 0;
347351

352+
bool discrete = mColorInterpolationComboBox->currentText() == tr( "Discrete" );
353+
348354
QList<double> entryValues;
349355
QVector<QColor> entryColors;
350356

@@ -358,18 +364,29 @@ void QgsSingleBandPseudoColorRendererWidget::on_mClassifyButton_clicked()
358364
if ( colorRamp )
359365
{
360366
numberOfEntries = colorRamp->count();
361-
entryValues.reserve( colorRamp->count() );
362-
for ( int i = 0; i < colorRamp->count(); ++i )
367+
entryValues.reserve( numberOfEntries );
368+
if ( discrete )
363369
{
364-
double value = colorRamp->value( i );
365-
entryValues.push_back( min + value * ( max - min ) );
370+
double intervalDiff = ( max - min ) * ( numberOfEntries - 1 ) / numberOfEntries;
371+
for ( int i = 1; i < numberOfEntries; ++i )
372+
{
373+
double value = colorRamp->value( i );
374+
entryValues.push_back( min + value * intervalDiff );
375+
}
376+
entryValues.push_back( std::numeric_limits<double>::infinity() );
377+
}
378+
else
379+
{
380+
for ( int i = 0; i < numberOfEntries; ++i )
381+
{
382+
double value = colorRamp->value( i );
383+
entryValues.push_back( min + value * ( max - min ) );
384+
}
366385
}
367386
}
368387
}
369388
else if ( mClassificationModeComboBox->itemData( mClassificationModeComboBox->currentIndex() ).toInt() == Quantile )
370389
{ // Quantile
371-
mMinMaxWidget->load();
372-
373390
numberOfEntries = mNumberOfEntriesSpinBox->value();
374391

375392
int bandNr = mBandComboBox->itemData( bandComboIndex ).toInt();
@@ -381,15 +398,31 @@ void QgsSingleBandPseudoColorRendererWidget::on_mClassifyButton_clicked()
381398
QgsRectangle extent = mMinMaxWidget->extent();
382399
int sampleSize = mMinMaxWidget->sampleSize();
383400

401+
// set min and max from histogram, used later to calculate number of decimals to display
402+
mRasterLayer->dataProvider()->cumulativeCut( bandNr, 0.0, 1.0, min, max, extent, sampleSize );
403+
384404
double intervalDiff;
385405
if ( numberOfEntries > 1 )
386406
{
387-
intervalDiff = 1.0 / ( numberOfEntries - 1 );
388407
entryValues.reserve( numberOfEntries );
389-
for ( int i = 0; i < numberOfEntries; ++i )
408+
if ( discrete )
390409
{
391-
mRasterLayer->dataProvider()->cumulativeCut( bandNr, 0.0, i * intervalDiff, cut1, cut2, extent, sampleSize );
392-
entryValues.push_back( cut2 );
410+
intervalDiff = 1.0 / ( numberOfEntries );
411+
for ( int i = 1; i < numberOfEntries; ++i )
412+
{
413+
mRasterLayer->dataProvider()->cumulativeCut( bandNr, 0.0, i * intervalDiff, cut1, cut2, extent, sampleSize );
414+
entryValues.push_back( cut2 );
415+
}
416+
entryValues.push_back( std::numeric_limits<double>::infinity() );
417+
}
418+
else
419+
{
420+
intervalDiff = 1.0 / ( numberOfEntries - 1 );
421+
for ( int i = 0; i < numberOfEntries; ++i )
422+
{
423+
mRasterLayer->dataProvider()->cumulativeCut( bandNr, 0.0, i * intervalDiff, cut1, cut2, extent, sampleSize );
424+
entryValues.push_back( cut2 );
425+
}
393426
}
394427
}
395428
else if ( numberOfEntries == 1 )
@@ -401,45 +434,47 @@ void QgsSingleBandPseudoColorRendererWidget::on_mClassifyButton_clicked()
401434
else // EqualInterval
402435
{
403436
numberOfEntries = mNumberOfEntriesSpinBox->value();
404-
//double currentValue = myRasterBandStats.minimumValue;
405-
double currentValue = min;
406-
double intervalDiff;
437+
407438
if ( numberOfEntries > 1 )
408439
{
409-
//because the highest value is also an entry, there are (numberOfEntries - 1)
410-
//intervals
411-
//intervalDiff = ( myRasterBandStats.maximumValue - myRasterBandStats.minimumValue ) /
412-
intervalDiff = ( max - min ) / ( numberOfEntries - 1 );
413-
}
414-
else
415-
{
416-
//intervalDiff = myRasterBandStats.maximumValue - myRasterBandStats.minimumValue;
417-
intervalDiff = max - min;
418-
}
440+
entryValues.reserve( numberOfEntries );
441+
if ( discrete )
442+
{
443+
// in discrete mode the lowest value is not an entry and the highest
444+
// value is inf, there are ( numberOfEntries ) of which the first
445+
// and last are not used.
446+
double intervalDiff = ( max - min ) / ( numberOfEntries );
419447

420-
entryValues.reserve( numberOfEntries );
421-
for ( int i = 0; i < numberOfEntries; ++i )
448+
for ( int i = 1; i < numberOfEntries; ++i )
449+
{
450+
entryValues.push_back( min + i * intervalDiff );
451+
}
452+
entryValues.push_back( std::numeric_limits<double>::infinity() );
453+
}
454+
else
455+
{
456+
//because the highest value is also an entry, there are (numberOfEntries - 1) intervals
457+
double intervalDiff = ( max - min ) / ( numberOfEntries - 1 );
458+
459+
for ( int i = 0; i < numberOfEntries; ++i )
460+
{
461+
entryValues.push_back( min + i * intervalDiff );
462+
}
463+
}
464+
}
465+
else if ( numberOfEntries == 1 )
422466
{
423-
entryValues.push_back( currentValue );
424-
currentValue += intervalDiff;
467+
if ( discrete )
468+
{
469+
entryValues.push_back( std::numeric_limits<double>::infinity() );
470+
}
471+
else
472+
{
473+
entryValues.push_back(( max + min ) / 2 );
474+
}
425475
}
426476
}
427477

428-
#if 0
429-
//hard code color range from blue -> red for now. Allow choice of ramps in future
430-
int colorDiff = 0;
431-
if ( numberOfEntries != 0 )
432-
{
433-
colorDiff = ( int )( 255 / numberOfEntries );
434-
}
435-
for ( int i = 0; i < numberOfEntries; ++i )
436-
{
437-
QColor currentColor;
438-
currentColor.setRgb( colorDiff*i, 0, 255 - colorDiff * i );
439-
entryColors.push_back( currentColor );
440-
}
441-
#endif
442-
443478
if ( ! colorRamp )
444479
{
445480
//hard code color range from blue -> red (previous default)
@@ -494,6 +529,8 @@ void QgsSingleBandPseudoColorRendererWidget::on_mClassifyButton_clicked()
494529
void QgsSingleBandPseudoColorRendererWidget::on_mClassificationModeComboBox_currentIndexChanged( int index )
495530
{
496531
mNumberOfEntriesSpinBox->setEnabled( mClassificationModeComboBox->itemData( index ).toInt() != Continuous );
532+
mMinLineEdit->setEnabled( mClassificationModeComboBox->itemData( index ).toInt() != Quantile );
533+
mMaxLineEdit->setEnabled( mClassificationModeComboBox->itemData( index ).toInt() != Quantile );
497534
}
498535

499536
void QgsSingleBandPseudoColorRendererWidget::on_mColorRampComboBox_currentIndexChanged( int index )

0 commit comments

Comments
 (0)