Skip to content

Commit 80c251d

Browse files
committed
cumulative cut tools, raster stretching moved from qgisapp to raster layer, unfortunately one control image has also be changed because stretching changed from GDALComputeRasterMinMax which gives for tests/testdata/landsat.tif 3. band min/max 57/157 to GDALComputeRasterStatistics which gives 51/172
1 parent ad9cb9d commit 80c251d

File tree

13 files changed

+193
-178
lines changed

13 files changed

+193
-178
lines changed

images/images.qrc

+2
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@
7979
<file>themes/default/mActionFormAnnotation.png</file>
8080
<file>themes/default/mActionFreezeLabels.png</file>
8181
<file>themes/default/mActionFromSelectedFeature.png</file>
82+
<file>themes/default/mActionFullCumulativeCutStretch.png</file>
8283
<file>themes/default/mActionFullHistogramStretch.png</file>
8384
<file>themes/default/mActionGroupItems.png</file>
8485
<file>themes/default/mActionHelpAbout.png</file>
@@ -91,6 +92,7 @@
9192
<file>themes/default/mActionInvertSelection.png</file>
9293
<file>themes/default/mActionLabeling.png</file>
9394
<file>themes/default/mActionLabel.png</file>
95+
<file>themes/default/mActionLocalCumulativeCutStretch.png</file>
9496
<file>themes/default/mActionLocalHistogramStretch.png</file>
9597
<file>themes/default/mActionLowerItems.png</file>
9698
<file>themes/default/mActionMapTips.png</file>
Loading
Loading

src/app/qgisapp.cpp

+26-113
Original file line numberDiff line numberDiff line change
@@ -963,6 +963,8 @@ void QgisApp::createActions()
963963
// Raster toolbar items
964964
connect( mActionLocalHistogramStretch, SIGNAL( triggered() ), this, SLOT( localHistogramStretch() ) );
965965
connect( mActionFullHistogramStretch, SIGNAL( triggered() ), this, SLOT( fullHistogramStretch() ) );
966+
connect( mActionLocalCumulativeCutStretch, SIGNAL( triggered() ), this, SLOT( localCumulativeCutStretch() ) );
967+
connect( mActionFullCumulativeCutStretch, SIGNAL( triggered() ), this, SLOT( fullCumulativeCutStretch() ) );
966968

967969
// Help Menu Items
968970

@@ -5461,142 +5463,53 @@ void QgisApp::options()
54615463

54625464
void QgisApp::fullHistogramStretch()
54635465
{
5464-
histogramStretch( false );
5466+
histogramStretch( false, QgsRasterLayer::ContrastEnhancementMinMax );
54655467
}
54665468

5467-
void QgisApp::histogramStretch( bool visibleAreaOnly )
5469+
void QgisApp::localHistogramStretch()
54685470
{
5469-
QgsMapLayer * layer = mMapLegend->currentLayer();
5471+
histogramStretch( true, QgsRasterLayer::ContrastEnhancementMinMax );
5472+
}
54705473

5471-
if ( !layer )
5474+
void QgisApp::fullCumulativeCutStretch()
5475+
{
5476+
histogramStretch( false, QgsRasterLayer::ContrastEnhancementCumulativeCut );
5477+
}
5478+
5479+
void QgisApp::localCumulativeCutStretch()
5480+
{
5481+
histogramStretch( true, QgsRasterLayer::ContrastEnhancementCumulativeCut );
5482+
}
5483+
5484+
void QgisApp::histogramStretch( bool visibleAreaOnly, QgsRasterLayer::ContrastEnhancementLimits theLimits )
5485+
{
5486+
QgsMapLayer * myLayer = mMapLegend->currentLayer();
5487+
5488+
if ( !myLayer )
54725489
{
54735490
QMessageBox::information( this,
54745491
tr( "No Layer Selected" ),
54755492
tr( "To perform a full histogram stretch, you need to have a raster layer selected." ) );
54765493
return;
54775494
}
54785495

5479-
QgsRasterLayer* rlayer = qobject_cast<QgsRasterLayer *>( layer );
5480-
if ( !rlayer )
5496+
QgsRasterLayer* myRasterLayer = qobject_cast<QgsRasterLayer *>( myLayer );
5497+
if ( !myRasterLayer )
54815498
{
54825499
QMessageBox::information( this,
54835500
tr( "No Raster Layer Selected" ),
54845501
tr( "To perform a full histogram stretch, you need to have a raster layer selected." ) );
54855502
return;
54865503
}
54875504

5488-
QgsRasterDataProvider* provider = rlayer->dataProvider();
5489-
if ( !provider )
5490-
{
5491-
return;
5492-
}
5493-
5494-
//get renderer
5495-
QgsRasterRenderer* renderer = rlayer->renderer();
5496-
if ( !renderer )
5497-
{
5498-
return;
5499-
}
5500-
5501-
//singleband gray <-> multiband color
5502-
if ( renderer->type() == "singlebandgray" )
5503-
{
5504-
QgsSingleBandGrayRenderer* grayRenderer = static_cast<QgsSingleBandGrayRenderer*>( renderer );
5505-
if ( !grayRenderer )
5506-
{
5507-
return;
5508-
}
5509-
5510-
//create new contrast enhancements
5511-
int grayBand = grayRenderer->grayBand();
5512-
if ( grayBand == -1 )
5513-
{
5514-
return;
5515-
}
5516-
5517-
QgsContrastEnhancement* e = rasterContrastEnhancement( rlayer, grayBand, visibleAreaOnly );
5518-
if ( !e )
5519-
{
5520-
return;
5521-
}
5522-
grayRenderer->setContrastEnhancement( e );
5523-
}
5524-
else if ( renderer->type() == "multibandcolor" )
5525-
{
5526-
QgsMultiBandColorRenderer* colorRenderer = static_cast<QgsMultiBandColorRenderer*>( renderer );
5527-
if ( !colorRenderer )
5528-
{
5529-
return;
5530-
}
5505+
QgsRectangle myRectangle;
5506+
if ( visibleAreaOnly ) myRectangle = mMapCanvas->mapRenderer()->outputExtentToLayerExtent( myRasterLayer, mMapCanvas->extent() );
55315507

5532-
QgsContrastEnhancement* redEnhancement = rasterContrastEnhancement( rlayer, colorRenderer->redBand(), visibleAreaOnly );
5533-
if ( redEnhancement )
5534-
{
5535-
colorRenderer->setRedContrastEnhancement( redEnhancement );
5536-
}
5537-
QgsContrastEnhancement* greenEnhancement = rasterContrastEnhancement( rlayer, colorRenderer->greenBand(), visibleAreaOnly );
5538-
if ( greenEnhancement )
5539-
{
5540-
colorRenderer->setGreenContrastEnhancement( greenEnhancement );
5541-
}
5542-
QgsContrastEnhancement* blueEnhancement = rasterContrastEnhancement( rlayer, colorRenderer->blueBand(), visibleAreaOnly );
5543-
if ( blueEnhancement )
5544-
{
5545-
colorRenderer->setBlueContrastEnhancement( blueEnhancement );
5546-
}
5547-
}
5548-
else
5549-
{
5550-
return;
5551-
}
5508+
myRasterLayer->setContrastEnhancementAlgorithm( QgsContrastEnhancement::StretchToMinimumMaximum, theLimits, myRectangle );
55525509

55535510
mMapCanvas->refresh();
55545511
}
55555512

5556-
QgsContrastEnhancement* QgisApp::rasterContrastEnhancement( QgsRasterLayer* rlayer, int band,
5557-
bool visibleAreaOnly ) const
5558-
{
5559-
if ( !rlayer || band == -1 )
5560-
{
5561-
return 0;
5562-
}
5563-
5564-
QgsRasterDataProvider* provider = rlayer->dataProvider();
5565-
if ( !provider )
5566-
{
5567-
return 0;
5568-
}
5569-
5570-
QgsContrastEnhancement* e = new QgsContrastEnhancement(( QgsContrastEnhancement::QgsRasterDataType )(
5571-
provider->dataType( band ) ) );
5572-
double minValue = 0;
5573-
double maxValue = 0;
5574-
5575-
if ( visibleAreaOnly )
5576-
{
5577-
double minMax[2];
5578-
rlayer->computeMinimumMaximumFromLastExtent( band, minMax );
5579-
minValue = minMax[0];
5580-
maxValue = minMax[1];
5581-
}
5582-
else
5583-
{
5584-
QgsRasterBandStats rasterBandStats = rlayer->dataProvider()->bandStatistics( band );
5585-
minValue = rasterBandStats.minimumValue;
5586-
maxValue = rasterBandStats.maximumValue;
5587-
}
5588-
5589-
e->setMinimumValue( minValue );
5590-
e->setMaximumValue( maxValue );
5591-
e->setContrastEnhancementAlgorithm( QgsContrastEnhancement::StretchToMinimumMaximum );
5592-
return e;
5593-
}
5594-
5595-
void QgisApp::localHistogramStretch()
5596-
{
5597-
histogramStretch( true );
5598-
}
5599-
56005513
void QgisApp::helpContents()
56015514
{
56025515
openURL( "index.html" );

src/app/qgisapp.h

+6-5
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ class QgsPalLabeling;
5555
class QgsPoint;
5656
class QgsProviderRegistry;
5757
class QgsPythonUtils;
58-
class QgsRasterLayer;
5958
class QgsRectangle;
6059
class QgsUndoWidget;
6160
class QgsVectorLayer;
@@ -87,6 +86,7 @@ class QgsTileScaleWidget;
8786
#include "qgsconfig.h"
8887
#include "qgsfeature.h"
8988
#include "qgspoint.h"
89+
#include "qgsrasterlayer.h"
9090
#include "qgssnappingdialog.h"
9191

9292
#include "ui_qgisapp.h"
@@ -548,6 +548,10 @@ class QgisApp : public QMainWindow, private Ui::MainWindow
548548
* Valid for non wms raster layers only.
549549
* @note Added in QGIS 1.7 */
550550
void fullHistogramStretch();
551+
/** Perform a local cumulative cut stretch */
552+
void localCumulativeCutStretch();
553+
/** Perform a full extent cumulative cut stretch */
554+
void fullCumulativeCutStretch();
551555
//! plugin manager
552556
void showPluginManager();
553557
//! load python support if possible
@@ -992,10 +996,7 @@ class QgisApp : public QMainWindow, private Ui::MainWindow
992996
void createDecorations();
993997

994998
/**Do histogram stretch for singleband gray / multiband color rasters*/
995-
void histogramStretch( bool visibleAreaOnly = false );
996-
/**Create raster enhancement object for a raster band*/
997-
QgsContrastEnhancement* rasterContrastEnhancement( QgsRasterLayer* rlayer, int band,
998-
bool visibleAreaOnly = false ) const;
999+
void histogramStretch( bool visibleAreaOnly = false, QgsRasterLayer::ContrastEnhancementLimits theLimits = QgsRasterLayer::ContrastEnhancementMinMax );
9991000

10001001
// actions for menus and toolbars -----------------
10011002

src/core/qgsrasterdataprovider.cpp

+18-2
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,15 @@ void QgsRasterDataProvider::initStatistics( QgsRasterBandStats &theStatistics,
435435
theStatistics.bandNumber = theBandNo;
436436
theStatistics.statsGathered = theStats;
437437

438-
QgsRectangle myExtent = theExtent.isEmpty() ? extent() : theExtent;
438+
QgsRectangle myExtent;
439+
if ( theExtent.isEmpty() )
440+
{
441+
myExtent = extent();
442+
}
443+
else
444+
{
445+
myExtent = extent().intersect( &theExtent );
446+
}
439447
theStatistics.extent = myExtent;
440448

441449
if ( theSampleSize > 0 )
@@ -674,7 +682,15 @@ void QgsRasterDataProvider::initHistogram( QgsRasterHistogram &theHistogram,
674682
}
675683
}
676684

677-
QgsRectangle myExtent = theExtent.isEmpty() ? extent() : theExtent;
685+
QgsRectangle myExtent;
686+
if ( theExtent.isEmpty() )
687+
{
688+
myExtent = extent();
689+
}
690+
else
691+
{
692+
myExtent = extent().intersect( &theExtent );
693+
}
678694
theHistogram.extent = myExtent;
679695

680696
if ( theSampleSize > 0 )

0 commit comments

Comments
 (0)