Skip to content

Commit 962a7d9

Browse files
committed
avoid creating a histogram without range (fixes #10832)
1 parent c90d810 commit 962a7d9

File tree

5 files changed

+54
-42
lines changed

5 files changed

+54
-42
lines changed

src/app/qgsrasterlayerproperties.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -512,7 +512,7 @@ void QgsRasterLayerProperties::setRendererWidget( const QString& rendererName )
512512
QgsDebugMsg( "renderer has widgetCreateFunction" );
513513
// Current canvas extent (used to calc min/max) in layer CRS
514514
QgsRectangle myExtent = mMapCanvas->mapSettings().outputExtentToLayerExtent( mRasterLayer, mMapCanvas->extent() );
515-
mRendererWidget = ( *rendererEntry.widgetCreateFunction )( mRasterLayer, myExtent );
515+
mRendererWidget = rendererEntry.widgetCreateFunction( mRasterLayer, myExtent );
516516
mRendererStackedWidget->addWidget( mRendererWidget );
517517
if ( oldWidget )
518518
{

src/core/raster/qgsrasterbandstats.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class CORE_EXPORT QgsRasterBandStats
5050
{
5151
statsGathered = None;
5252
minimumValue = std::numeric_limits<double>::max();
53-
maximumValue = std::numeric_limits<double>::min();
53+
maximumValue = -std::numeric_limits<double>::max();
5454
range = 0.0;
5555
mean = 0.0;
5656
sumOfSquares = 0.0;

src/core/raster/qgsrasterinterface.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ void QgsRasterInterface::initHistogram( QgsRasterHistogram &theHistogram,
273273
// We need statistics -> avoid histogramDefaults in hasHistogram if possible
274274
// TODO: use approximated statistics if aproximated histogram is requested
275275
// (theSampleSize > 0)
276-
QgsRasterBandStats stats = bandStatistics( theBandNo, QgsRasterBandStats::Min, theExtent, theSampleSize );
276+
QgsRasterBandStats stats = bandStatistics( theBandNo, QgsRasterBandStats::Min, theExtent, theSampleSize );
277277
theHistogram.minimum = stats.minimumValue;
278278
}
279279
}
@@ -285,7 +285,7 @@ void QgsRasterInterface::initHistogram( QgsRasterHistogram &theHistogram,
285285
}
286286
else
287287
{
288-
QgsRasterBandStats stats = bandStatistics( theBandNo, QgsRasterBandStats::Max, theExtent, theSampleSize );
288+
QgsRasterBandStats stats = bandStatistics( theBandNo, QgsRasterBandStats::Max, theExtent, theSampleSize );
289289
theHistogram.maximum = stats.maximumValue;
290290
}
291291
}
@@ -518,7 +518,7 @@ void QgsRasterInterface::cumulativeCut( int theBandNo,
518518
int mySrcDataType = srcDataType( theBandNo );
519519

520520
//get band stats to specify real histogram min/max (fix #9793 Byte bands)
521-
QgsRasterBandStats stats = bandStatistics( theBandNo, QgsRasterBandStats::Min, theExtent, theSampleSize );
521+
QgsRasterBandStats stats = bandStatistics( theBandNo, QgsRasterBandStats::Min, theExtent, theSampleSize );
522522
// for byte bands make sure bin count == actual range
523523
int myBinCount = ( mySrcDataType == QGis::Byte ) ? int( ceil( stats.maximumValue - stats.minimumValue + 1 ) ) : 0;
524524
QgsRasterHistogram myHistogram = histogram( theBandNo, myBinCount, stats.minimumValue, stats.maximumValue, theExtent, theSampleSize );

src/gui/raster/qgsrasterhistogramwidget.cpp

Lines changed: 47 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -606,53 +606,65 @@ void QgsRasterHistogramWidget::refreshHistogram()
606606
QgsDebugMsg( QString( "computed histo min = %1 max = %2" ).arg( mHistoMin ).arg( mHistoMax ) );
607607
myFirstIteration = false;
608608
}
609-
// for x axis use band pixel values rather than gdal hist. bin values
610-
// subtract -0.5 to prevent rounding errors
611-
// see http://www.gdal.org/classGDALRasterBand.html#3f8889607d3b2294f7e0f11181c201c8
612-
// fix x range for non-Byte data
613-
mpPlot->setAxisScale( QwtPlot::xBottom,
614-
mHistoMin - myBinXStep / 2,
615-
mHistoMax + myBinXStep / 2 );
616609

617-
mpPlot->replot();
610+
if ( mHistoMin < mHistoMax )
611+
{
612+
// for x axis use band pixel values rather than gdal hist. bin values
613+
// subtract -0.5 to prevent rounding errors
614+
// see http://www.gdal.org/classGDALRasterBand.html#3f8889607d3b2294f7e0f11181c201c8
615+
// fix x range for non-Byte data
616+
mpPlot->setAxisScale( QwtPlot::xBottom,
617+
mHistoMin - myBinXStep / 2,
618+
mHistoMax + myBinXStep / 2 );
619+
mpPlot->setEnabled( true );
620+
mpPlot->replot();
618621

619-
// histo plot markers
620-
// memory leak?
621-
mHistoMarkerMin = new QwtPlotMarker();
622-
mHistoMarkerMin->attach( mpPlot );
623-
mHistoMarkerMax = new QwtPlotMarker();
624-
mHistoMarkerMax->attach( mpPlot );
625-
updateHistoMarkers();
622+
// histo plot markers
623+
// memory leak?
624+
mHistoMarkerMin = new QwtPlotMarker();
625+
mHistoMarkerMin->attach( mpPlot );
626+
mHistoMarkerMax = new QwtPlotMarker();
627+
mHistoMarkerMax->attach( mpPlot );
628+
updateHistoMarkers();
626629

627-
// histo picker
628-
if ( ! mHistoPicker )
629-
{
630-
mHistoPicker = new QwtPlotPicker( mpPlot->canvas() );
631-
// mHistoPicker->setTrackerMode( QwtPicker::ActiveOnly );
632-
mHistoPicker->setTrackerMode( QwtPicker::AlwaysOff );
633-
mHistoPicker->setRubberBand( QwtPicker::VLineRubberBand );
634-
mHistoPicker->setEnabled( false );
630+
// histo picker
631+
if ( !mHistoPicker )
632+
{
633+
mHistoPicker = new QwtPlotPicker( mpPlot->canvas() );
634+
// mHistoPicker->setTrackerMode( QwtPicker::ActiveOnly );
635+
mHistoPicker->setTrackerMode( QwtPicker::AlwaysOff );
636+
mHistoPicker->setRubberBand( QwtPicker::VLineRubberBand );
635637
#if defined(QWT_VERSION) && QWT_VERSION>=0x060000
636-
mHistoPicker->setStateMachine( new QwtPickerDragPointMachine );
637-
connect( mHistoPicker, SIGNAL( selected( const QPointF & ) ), this, SLOT( histoPickerSelected( const QPointF & ) ) );
638+
mHistoPicker->setStateMachine( new QwtPickerDragPointMachine );
639+
connect( mHistoPicker, SIGNAL( selected( const QPointF & ) ), this, SLOT( histoPickerSelected( const QPointF & ) ) );
638640
#else
639-
mHistoPicker->setSelectionFlags( QwtPicker::PointSelection | QwtPicker::DragSelection );
640-
connect( mHistoPicker, SIGNAL( selected( const QwtDoublePoint & ) ), this, SLOT( histoPickerSelectedQwt5( const QwtDoublePoint & ) ) );
641+
mHistoPicker->setSelectionFlags( QwtPicker::PointSelection | QwtPicker::DragSelection );
642+
connect( mHistoPicker, SIGNAL( selected( const QwtDoublePoint & ) ), this, SLOT( histoPickerSelectedQwt5( const QwtDoublePoint & ) ) );
641643
#endif
642-
}
644+
}
645+
mHistoPicker->setEnabled( false );
643646

644-
// plot zoomer
645-
if ( ! mHistoZoomer )
646-
{
647-
mHistoZoomer = new QwtPlotZoomer( mpPlot->canvas() );
647+
// plot zoomer
648+
if ( !mHistoZoomer )
649+
{
650+
mHistoZoomer = new QwtPlotZoomer( mpPlot->canvas() );
648651
#if defined(QWT_VERSION) && QWT_VERSION>=0x060000
649-
mHistoZoomer->setStateMachine( new QwtPickerDragRectMachine );
652+
mHistoZoomer->setStateMachine( new QwtPickerDragRectMachine );
650653
#else
651-
mHistoZoomer->setSelectionFlags( QwtPicker::RectSelection | QwtPicker::DragSelection );
654+
mHistoZoomer->setSelectionFlags( QwtPicker::RectSelection | QwtPicker::DragSelection );
652655
#endif
653-
mHistoZoomer->setTrackerMode( QwtPicker::AlwaysOff );
656+
mHistoZoomer->setTrackerMode( QwtPicker::AlwaysOff );
657+
}
654658
mHistoZoomer->setEnabled( true );
655659
}
660+
else
661+
{
662+
mpPlot->setDisabled( true );
663+
if ( mHistoPicker )
664+
mHistoPicker->setEnabled( false );
665+
if ( mHistoZoomer )
666+
mHistoZoomer->setEnabled( false );
667+
}
656668

657669
disconnect( mRasterLayer, SIGNAL( progressUpdate( int ) ), mHistogramProgress, SLOT( setValue( int ) ) );
658670
stackedWidget2->setCurrentIndex( 0 );

src/providers/gdal/qgsgdalprovider.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1397,7 +1397,7 @@ QgsRasterHistogram QgsGdalProvider::histogram( int theBandNo,
13971397
myMinVal -= dfHalfBucket;
13981398
myMaxVal += dfHalfBucket;
13991399

1400-
/*
1400+
#if 0
14011401
const char* pszPixelType = GDALGetMetadataItem( myGdalBand, "PIXELTYPE", "IMAGE_STRUCTURE" );
14021402
int bSignedByte = ( pszPixelType != NULL && EQUAL( pszPixelType, "SIGNEDBYTE" ) );
14031403

@@ -1420,7 +1420,7 @@ QgsRasterHistogram QgsGdalProvider::histogram( int theBandNo,
14201420
myMinVal -= dfHalfBucket;
14211421
myMaxVal += dfHalfBucket;
14221422
}
1423-
*/
1423+
#endif
14241424

14251425
int *myHistogramArray = new int[myHistogram.binCount];
14261426
CPLErr myError = GDALGetRasterHistogram( myGdalBand, myMinVal, myMaxVal,

0 commit comments

Comments
 (0)