Skip to content

Commit

Permalink
avoid creating a histogram without range (fixes #10832)
Browse files Browse the repository at this point in the history
  • Loading branch information
jef-n committed Oct 15, 2014
1 parent c90d810 commit 962a7d9
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 42 deletions.
2 changes: 1 addition & 1 deletion src/app/qgsrasterlayerproperties.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,7 @@ void QgsRasterLayerProperties::setRendererWidget( const QString& rendererName )
QgsDebugMsg( "renderer has widgetCreateFunction" );
// Current canvas extent (used to calc min/max) in layer CRS
QgsRectangle myExtent = mMapCanvas->mapSettings().outputExtentToLayerExtent( mRasterLayer, mMapCanvas->extent() );
mRendererWidget = ( *rendererEntry.widgetCreateFunction )( mRasterLayer, myExtent );
mRendererWidget = rendererEntry.widgetCreateFunction( mRasterLayer, myExtent );
mRendererStackedWidget->addWidget( mRendererWidget );
if ( oldWidget )
{
Expand Down
2 changes: 1 addition & 1 deletion src/core/raster/qgsrasterbandstats.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class CORE_EXPORT QgsRasterBandStats
{
statsGathered = None;
minimumValue = std::numeric_limits<double>::max();
maximumValue = std::numeric_limits<double>::min();
maximumValue = -std::numeric_limits<double>::max();
range = 0.0;
mean = 0.0;
sumOfSquares = 0.0;
Expand Down
6 changes: 3 additions & 3 deletions src/core/raster/qgsrasterinterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ void QgsRasterInterface::initHistogram( QgsRasterHistogram &theHistogram,
// We need statistics -> avoid histogramDefaults in hasHistogram if possible
// TODO: use approximated statistics if aproximated histogram is requested
// (theSampleSize > 0)
QgsRasterBandStats stats = bandStatistics( theBandNo, QgsRasterBandStats::Min, theExtent, theSampleSize );
QgsRasterBandStats stats = bandStatistics( theBandNo, QgsRasterBandStats::Min, theExtent, theSampleSize );
theHistogram.minimum = stats.minimumValue;
}
}
Expand All @@ -285,7 +285,7 @@ void QgsRasterInterface::initHistogram( QgsRasterHistogram &theHistogram,
}
else
{
QgsRasterBandStats stats = bandStatistics( theBandNo, QgsRasterBandStats::Max, theExtent, theSampleSize );
QgsRasterBandStats stats = bandStatistics( theBandNo, QgsRasterBandStats::Max, theExtent, theSampleSize );
theHistogram.maximum = stats.maximumValue;
}
}
Expand Down Expand Up @@ -518,7 +518,7 @@ void QgsRasterInterface::cumulativeCut( int theBandNo,
int mySrcDataType = srcDataType( theBandNo );

//get band stats to specify real histogram min/max (fix #9793 Byte bands)
QgsRasterBandStats stats = bandStatistics( theBandNo, QgsRasterBandStats::Min, theExtent, theSampleSize );
QgsRasterBandStats stats = bandStatistics( theBandNo, QgsRasterBandStats::Min, theExtent, theSampleSize );
// for byte bands make sure bin count == actual range
int myBinCount = ( mySrcDataType == QGis::Byte ) ? int( ceil( stats.maximumValue - stats.minimumValue + 1 ) ) : 0;
QgsRasterHistogram myHistogram = histogram( theBandNo, myBinCount, stats.minimumValue, stats.maximumValue, theExtent, theSampleSize );
Expand Down
82 changes: 47 additions & 35 deletions src/gui/raster/qgsrasterhistogramwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -606,53 +606,65 @@ void QgsRasterHistogramWidget::refreshHistogram()
QgsDebugMsg( QString( "computed histo min = %1 max = %2" ).arg( mHistoMin ).arg( mHistoMax ) );
myFirstIteration = false;
}
// for x axis use band pixel values rather than gdal hist. bin values
// subtract -0.5 to prevent rounding errors
// see http://www.gdal.org/classGDALRasterBand.html#3f8889607d3b2294f7e0f11181c201c8
// fix x range for non-Byte data
mpPlot->setAxisScale( QwtPlot::xBottom,
mHistoMin - myBinXStep / 2,
mHistoMax + myBinXStep / 2 );

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

// histo plot markers
// memory leak?
mHistoMarkerMin = new QwtPlotMarker();
mHistoMarkerMin->attach( mpPlot );
mHistoMarkerMax = new QwtPlotMarker();
mHistoMarkerMax->attach( mpPlot );
updateHistoMarkers();
// histo plot markers
// memory leak?
mHistoMarkerMin = new QwtPlotMarker();
mHistoMarkerMin->attach( mpPlot );
mHistoMarkerMax = new QwtPlotMarker();
mHistoMarkerMax->attach( mpPlot );
updateHistoMarkers();

// histo picker
if ( ! mHistoPicker )
{
mHistoPicker = new QwtPlotPicker( mpPlot->canvas() );
// mHistoPicker->setTrackerMode( QwtPicker::ActiveOnly );
mHistoPicker->setTrackerMode( QwtPicker::AlwaysOff );
mHistoPicker->setRubberBand( QwtPicker::VLineRubberBand );
mHistoPicker->setEnabled( false );
// histo picker
if ( !mHistoPicker )
{
mHistoPicker = new QwtPlotPicker( mpPlot->canvas() );
// mHistoPicker->setTrackerMode( QwtPicker::ActiveOnly );
mHistoPicker->setTrackerMode( QwtPicker::AlwaysOff );
mHistoPicker->setRubberBand( QwtPicker::VLineRubberBand );
#if defined(QWT_VERSION) && QWT_VERSION>=0x060000
mHistoPicker->setStateMachine( new QwtPickerDragPointMachine );
connect( mHistoPicker, SIGNAL( selected( const QPointF & ) ), this, SLOT( histoPickerSelected( const QPointF & ) ) );
mHistoPicker->setStateMachine( new QwtPickerDragPointMachine );
connect( mHistoPicker, SIGNAL( selected( const QPointF & ) ), this, SLOT( histoPickerSelected( const QPointF & ) ) );
#else
mHistoPicker->setSelectionFlags( QwtPicker::PointSelection | QwtPicker::DragSelection );
connect( mHistoPicker, SIGNAL( selected( const QwtDoublePoint & ) ), this, SLOT( histoPickerSelectedQwt5( const QwtDoublePoint & ) ) );
mHistoPicker->setSelectionFlags( QwtPicker::PointSelection | QwtPicker::DragSelection );
connect( mHistoPicker, SIGNAL( selected( const QwtDoublePoint & ) ), this, SLOT( histoPickerSelectedQwt5( const QwtDoublePoint & ) ) );
#endif
}
}
mHistoPicker->setEnabled( false );

// plot zoomer
if ( ! mHistoZoomer )
{
mHistoZoomer = new QwtPlotZoomer( mpPlot->canvas() );
// plot zoomer
if ( !mHistoZoomer )
{
mHistoZoomer = new QwtPlotZoomer( mpPlot->canvas() );
#if defined(QWT_VERSION) && QWT_VERSION>=0x060000
mHistoZoomer->setStateMachine( new QwtPickerDragRectMachine );
mHistoZoomer->setStateMachine( new QwtPickerDragRectMachine );
#else
mHistoZoomer->setSelectionFlags( QwtPicker::RectSelection | QwtPicker::DragSelection );
mHistoZoomer->setSelectionFlags( QwtPicker::RectSelection | QwtPicker::DragSelection );
#endif
mHistoZoomer->setTrackerMode( QwtPicker::AlwaysOff );
mHistoZoomer->setTrackerMode( QwtPicker::AlwaysOff );
}
mHistoZoomer->setEnabled( true );
}
else
{
mpPlot->setDisabled( true );
if ( mHistoPicker )
mHistoPicker->setEnabled( false );
if ( mHistoZoomer )
mHistoZoomer->setEnabled( false );
}

disconnect( mRasterLayer, SIGNAL( progressUpdate( int ) ), mHistogramProgress, SLOT( setValue( int ) ) );
stackedWidget2->setCurrentIndex( 0 );
Expand Down
4 changes: 2 additions & 2 deletions src/providers/gdal/qgsgdalprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1397,7 +1397,7 @@ QgsRasterHistogram QgsGdalProvider::histogram( int theBandNo,
myMinVal -= dfHalfBucket;
myMaxVal += dfHalfBucket;

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

Expand All @@ -1420,7 +1420,7 @@ QgsRasterHistogram QgsGdalProvider::histogram( int theBandNo,
myMinVal -= dfHalfBucket;
myMaxVal += dfHalfBucket;
}
*/
#endif

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

0 comments on commit 962a7d9

Please sign in to comment.