Skip to content

Commit

Permalink
qgsrasterlayer: Remove refresh logic
Browse files Browse the repository at this point in the history
This is not used anymore.
  • Loading branch information
ptitjano authored and nyalldawson committed Nov 28, 2024
1 parent e123726 commit 9d372b6
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 131 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,6 @@ Set contrast enhancement algorithm
%End



virtual QString subsetString() const;
%Docstring
Returns the string (typically sql) used to define a subset of the layer.
Expand Down
1 change: 0 additions & 1 deletion python/core/auto_generated/raster/qgsrasterlayer.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,6 @@ Set contrast enhancement algorithm
%End



virtual QString subsetString() const;
%Docstring
Returns the string (typically sql) used to define a subset of the layer.
Expand Down
109 changes: 0 additions & 109 deletions src/core/raster/qgsrasterlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1543,115 +1543,6 @@ void QgsRasterLayer::refreshContrastEnhancement( const QgsRectangle &extent )
}
}

void QgsRasterLayer::refreshRendererIfNeeded( QgsRasterRenderer *rasterRenderer,
const QgsRectangle &extent )
{
QGIS_PROTECT_QOBJECT_THREAD_ACCESS

if ( mDataProvider &&
mLastRectangleUsedByRefreshContrastEnhancementIfNeeded != extent &&
rasterRenderer->minMaxOrigin().limits() != QgsRasterMinMaxOrigin::None &&
rasterRenderer->minMaxOrigin().extent() == QgsRasterMinMaxOrigin::UpdatedCanvas )
{
refreshRenderer( rasterRenderer, extent );
}
}

void QgsRasterLayer::refreshRenderer( QgsRasterRenderer *rasterRenderer, const QgsRectangle &extent )
{
QGIS_PROTECT_QOBJECT_THREAD_ACCESS

if ( mDataProvider )
{
QgsSingleBandGrayRenderer *singleBandRenderer = nullptr;
QgsMultiBandColorRenderer *multiBandRenderer = nullptr;
QgsSingleBandPseudoColorRenderer *sbpcr = nullptr;
const QgsContrastEnhancement *ce = nullptr;
if ( ( singleBandRenderer = dynamic_cast<QgsSingleBandGrayRenderer *>( rasterRenderer ) ) )
{
ce = singleBandRenderer->contrastEnhancement();
}
else if ( ( multiBandRenderer = dynamic_cast<QgsMultiBandColorRenderer *>( rasterRenderer ) ) )
{
ce = multiBandRenderer->redContrastEnhancement();
}
else if ( ( sbpcr = dynamic_cast<QgsSingleBandPseudoColorRenderer *>( rasterRenderer ) ) )
{
mLastRectangleUsedByRefreshContrastEnhancementIfNeeded = extent;
double min;
double max;
computeMinMax( sbpcr->inputBand(),
rasterRenderer->minMaxOrigin(),
rasterRenderer->minMaxOrigin().limits(), extent,
static_cast<int>( SAMPLE_SIZE ), min, max );
sbpcr->setClassificationMin( min );
sbpcr->setClassificationMax( max );

if ( sbpcr->shader() )
{
QgsColorRampShader *colorRampShader = dynamic_cast<QgsColorRampShader *>( sbpcr->shader()->rasterShaderFunction() );
if ( colorRampShader )
{
colorRampShader->classifyColorRamp( sbpcr->inputBand(), extent, rasterRenderer->input() );
}
}

QgsSingleBandPseudoColorRenderer *r = dynamic_cast<QgsSingleBandPseudoColorRenderer *>( renderer() );
r->setClassificationMin( min );
r->setClassificationMax( max );

if ( r->shader() )
{
QgsColorRampShader *colorRampShader = dynamic_cast<QgsColorRampShader *>( r->shader()->rasterShaderFunction() );
if ( colorRampShader )
{
colorRampShader->classifyColorRamp( sbpcr->inputBand(), extent, rasterRenderer->input() );
}
}

emit repaintRequested();
emitStyleChanged();
emit rendererChanged();
return;
}

if ( ce &&
ce->contrastEnhancementAlgorithm() != QgsContrastEnhancement::NoEnhancement )
{
mLastRectangleUsedByRefreshContrastEnhancementIfNeeded = extent;

setContrastEnhancement( ce->contrastEnhancementAlgorithm(),
rasterRenderer->minMaxOrigin().limits(),
extent,
static_cast<int>( SAMPLE_SIZE ),
true,
rasterRenderer );

// Update main renderer so that the legends get updated
if ( singleBandRenderer )
static_cast<QgsSingleBandGrayRenderer *>( renderer() )->setContrastEnhancement( new QgsContrastEnhancement( * singleBandRenderer->contrastEnhancement() ) );
else if ( multiBandRenderer )
{
if ( multiBandRenderer->redContrastEnhancement() )
{
static_cast<QgsMultiBandColorRenderer *>( renderer() )->setRedContrastEnhancement( new QgsContrastEnhancement( *multiBandRenderer->redContrastEnhancement() ) );
}
if ( multiBandRenderer->greenContrastEnhancement() )
{
static_cast<QgsMultiBandColorRenderer *>( renderer() )->setGreenContrastEnhancement( new QgsContrastEnhancement( *multiBandRenderer->greenContrastEnhancement() ) );
}
if ( multiBandRenderer->blueContrastEnhancement() )
{
static_cast<QgsMultiBandColorRenderer *>( renderer() )->setBlueContrastEnhancement( new QgsContrastEnhancement( *multiBandRenderer->blueContrastEnhancement() ) );
}
}

emitStyleChanged();
emit rendererChanged();
}
}
}

QString QgsRasterLayer::subsetString() const
{
QGIS_PROTECT_QOBJECT_THREAD_ACCESS
Expand Down
12 changes: 0 additions & 12 deletions src/core/raster/qgsrasterlayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -403,12 +403,6 @@ class CORE_EXPORT QgsRasterLayer : public QgsMapLayer, public QgsAbstractProfile
*/
void refreshContrastEnhancement( const QgsRectangle &extent ) SIP_SKIP;

/**
* \brief Refresh renderer with new extent, if needed
* \note not available in Python bindings
*/
void refreshRendererIfNeeded( QgsRasterRenderer *rasterRenderer, const QgsRectangle &extent ) SIP_SKIP;

/**
* Returns the string (typically sql) used to define a subset of the layer.
* \returns The subset string or null QString if not implemented by the provider
Expand Down Expand Up @@ -538,9 +532,6 @@ class CORE_EXPORT QgsRasterLayer : public QgsMapLayer, public QgsAbstractProfile
bool generateLookupTableFlag,
QgsRasterRenderer *rasterRenderer );

//! Refresh renderer
void refreshRenderer( QgsRasterRenderer *rasterRenderer, const QgsRectangle &extent );

/**
* Updates the data source of the layer. The layer's renderer and legend will be preserved only
* if the geometry type of the new data source matches the current geometry type of the layer.
Expand Down Expand Up @@ -592,9 +583,6 @@ class CORE_EXPORT QgsRasterLayer : public QgsMapLayer, public QgsAbstractProfile

std::unique_ptr< QgsRasterPipe > mPipe;

//! To save computations and possible infinite cycle of notifications
QgsRectangle mLastRectangleUsedByRefreshContrastEnhancementIfNeeded;

QDomDocument mOriginalStyleDocument;
QDomElement mOriginalStyleElement;

Expand Down
50 changes: 42 additions & 8 deletions tests/src/core/testqgsrasterlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1008,15 +1008,16 @@ void TestQgsRasterLayer::testRefreshRendererIfNeeded()
QVERIFY( dynamic_cast<QgsMultiBandColorRenderer *>( mpLandsatRasterLayer->renderer() ) );
mMapSettings->setLayers( QList<QgsMapLayer *>() << mpLandsatRasterLayer );
mMapSettings->setExtent( mpLandsatRasterLayer->extent() );
const double initMinVal = static_cast<QgsMultiBandColorRenderer *>( mpLandsatRasterLayer->renderer() )->redContrastEnhancement()->minimumValue();
const double initRedMinVal = static_cast<QgsMultiBandColorRenderer *>( mpLandsatRasterLayer->renderer() )->redContrastEnhancement()->minimumValue();
const double initRedMaxVal = static_cast<QgsMultiBandColorRenderer *>( mpLandsatRasterLayer->renderer() )->redContrastEnhancement()->maximumValue();
const double initGreenMinVal = static_cast<QgsMultiBandColorRenderer *>( mpLandsatRasterLayer->renderer() )->greenContrastEnhancement()->minimumValue();
const double initGreenMaxVal = static_cast<QgsMultiBandColorRenderer *>( mpLandsatRasterLayer->renderer() )->greenContrastEnhancement()->maximumValue();
const double initBlueMinVal = static_cast<QgsMultiBandColorRenderer *>( mpLandsatRasterLayer->renderer() )->blueContrastEnhancement()->minimumValue();
const double initBlueMaxVal = static_cast<QgsMultiBandColorRenderer *>( mpLandsatRasterLayer->renderer() )->blueContrastEnhancement()->maximumValue();

// Should do nothing
const QgsRectangle newExtent = QgsRectangle( 785000, 3340000, 785100, 3340100 );
QVERIFY( !mpLandsatRasterLayer->renderer()->needsRefresh( newExtent ) );
mpLandsatRasterLayer->refreshRendererIfNeeded( mpLandsatRasterLayer->renderer(), newExtent );
QCOMPARE( mpLandsatRasterLayer->renderer()->minMaxOrigin().limits(), QgsRasterMinMaxOrigin::MinMax );
const double minVal = static_cast<QgsMultiBandColorRenderer *>( mpLandsatRasterLayer->renderer() )->redContrastEnhancement()->minimumValue();
QGSCOMPARENEAR( initMinVal, minVal, 1e-5 );

// Change to UpdatedCanvas
QgsRasterMinMaxOrigin mmo = mpLandsatRasterLayer->renderer()->minMaxOrigin();
Expand All @@ -1025,9 +1026,42 @@ void TestQgsRasterLayer::testRefreshRendererIfNeeded()
mpLandsatRasterLayer->renderer()->setMinMaxOrigin( mmo );
QCOMPARE( mpLandsatRasterLayer->renderer()->minMaxOrigin().extent(), QgsRasterMinMaxOrigin::UpdatedCanvas );
QVERIFY( mpLandsatRasterLayer->renderer()->needsRefresh( newExtent ) );
mpLandsatRasterLayer->refreshRendererIfNeeded( mpLandsatRasterLayer->renderer(), newExtent );
const double newMinVal = static_cast<QgsMultiBandColorRenderer *>( mpLandsatRasterLayer->renderer() )->redContrastEnhancement()->minimumValue();
QGSCOMPARENOTNEAR( initMinVal, newMinVal, 1e-5 );

QList<double> minValues;
QList<double> maxValues;
for ( const int bandIdx : mpLandsatRasterLayer->renderer()->usesBands() )
{
double min;
double max;
mpLandsatRasterLayer->computeMinMax( bandIdx, mmo, mmo.limits(),
newExtent, static_cast<int>( QgsRasterLayer::SAMPLE_SIZE ),
min, max );
minValues.append( min );
maxValues.append( max );
}

const bool refreshed = mpLandsatRasterLayer->renderer()->refresh( newExtent, minValues, maxValues );
QVERIFY( refreshed );
QVERIFY( !mpLandsatRasterLayer->renderer()->needsRefresh( newExtent ) );

const double newRedMinVal = static_cast<QgsMultiBandColorRenderer *>( mpLandsatRasterLayer->renderer() )->redContrastEnhancement()->minimumValue();
const double newRedMaxVal = static_cast<QgsMultiBandColorRenderer *>( mpLandsatRasterLayer->renderer() )->redContrastEnhancement()->maximumValue();
const double newGreenMinVal = static_cast<QgsMultiBandColorRenderer *>( mpLandsatRasterLayer->renderer() )->greenContrastEnhancement()->minimumValue();
const double newGreenMaxVal = static_cast<QgsMultiBandColorRenderer *>( mpLandsatRasterLayer->renderer() )->greenContrastEnhancement()->maximumValue();
const double newBlueMinVal = static_cast<QgsMultiBandColorRenderer *>( mpLandsatRasterLayer->renderer() )->blueContrastEnhancement()->minimumValue();
const double newBlueMaxVal = static_cast<QgsMultiBandColorRenderer *>( mpLandsatRasterLayer->renderer() )->blueContrastEnhancement()->maximumValue();
QGSCOMPARENOTNEAR( initRedMinVal, newRedMinVal, 1e-5 );
QGSCOMPARENOTNEAR( initRedMaxVal, newRedMaxVal, 1e-5 );
QGSCOMPARENOTNEAR( initGreenMinVal, newGreenMinVal, 1e-5 );
QGSCOMPARENOTNEAR( initGreenMaxVal, newGreenMaxVal, 1e-5 );
QGSCOMPARENOTNEAR( initBlueMinVal, newBlueMinVal, 1e-5 );
QGSCOMPARENOTNEAR( initBlueMaxVal, newBlueMaxVal, 1e-5 );
QGSCOMPARENEAR( newRedMinVal, 208.0, 1e-5 );
QGSCOMPARENEAR( newRedMaxVal, 225.0, 1e-5 );
QGSCOMPARENEAR( newGreenMinVal, 162.0, 1e-5 );
QGSCOMPARENEAR( newGreenMaxVal, 172.0, 1e-5 );
QGSCOMPARENEAR( newBlueMinVal, 90.0, 1e-5 );
QGSCOMPARENEAR( newBlueMaxVal, 94.0, 1e-5 );
}

void TestQgsRasterLayer::sample()
Expand Down

0 comments on commit 9d372b6

Please sign in to comment.