Skip to content
Permalink
Browse files

PG raster: reset renderer when filter changes

  • Loading branch information
elpaso committed Mar 4, 2020
1 parent 9815b2a commit 385acae48c4834a55cd9322d90faf5bc8d2fe95b
@@ -447,6 +447,7 @@ Emitted when the layer's subset string has changed.

virtual QString decodedSource( const QString &source, const QString &provider, const QgsReadWriteContext &context ) const;


};


@@ -815,6 +815,7 @@ void QgsRasterLayer::setDataProvider( QString const &provider, const QgsDataProv


QgsDebugMsgLevel( QStringLiteral( "exiting." ), 4 );

}

void QgsRasterLayer::setDataSource( const QString &dataSource, const QString &baseName, const QString &provider, const QgsDataProvider::ProviderOptions &options, bool loadDefaultStyleFlag )
@@ -1142,97 +1143,105 @@ void QgsRasterLayer::refreshContrastEnhancement( const QgsRectangle &extent )
void QgsRasterLayer::refreshRendererIfNeeded( QgsRasterRenderer *rasterRenderer,
const QgsRectangle &extent )
{
if ( !( mDataProvider &&
mLastRectangleUsedByRefreshContrastEnhancementIfNeeded != extent &&
rasterRenderer->minMaxOrigin().limits() != QgsRasterMinMaxOrigin::None &&
rasterRenderer->minMaxOrigin().extent() == QgsRasterMinMaxOrigin::UpdatedCanvas ) )
return;

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 ) ) )
if ( mDataProvider &&
mLastRectangleUsedByRefreshContrastEnhancementIfNeeded != extent &&
rasterRenderer->minMaxOrigin().limits() != QgsRasterMinMaxOrigin::None &&
rasterRenderer->minMaxOrigin().extent() == QgsRasterMinMaxOrigin::UpdatedCanvas )
{
ce = multiBandRenderer->redContrastEnhancement();
refreshRenderer( rasterRenderer, extent );
}
else if ( ( sbpcr = dynamic_cast<QgsSingleBandPseudoColorRenderer *>( rasterRenderer ) ) )
{
mLastRectangleUsedByRefreshContrastEnhancementIfNeeded = extent;
double min;
double max;
computeMinMax( sbpcr->band(),
rasterRenderer->minMaxOrigin(),
rasterRenderer->minMaxOrigin().limits(), extent,
SAMPLE_SIZE, min, max );
sbpcr->setClassificationMin( min );
sbpcr->setClassificationMax( max );
}

if ( sbpcr->shader() )
void QgsRasterLayer::refreshRenderer( QgsRasterRenderer *rasterRenderer, const QgsRectangle &extent )
{
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 ) ) )
{
QgsColorRampShader *colorRampShader = dynamic_cast<QgsColorRampShader *>( sbpcr->shader()->rasterShaderFunction() );
if ( colorRampShader )
mLastRectangleUsedByRefreshContrastEnhancementIfNeeded = extent;
double min;
double max;
computeMinMax( sbpcr->band(),
rasterRenderer->minMaxOrigin(),
rasterRenderer->minMaxOrigin().limits(), extent,
SAMPLE_SIZE, min, max );
sbpcr->setClassificationMin( min );
sbpcr->setClassificationMax( max );

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

QgsSingleBandPseudoColorRenderer *r = dynamic_cast<QgsSingleBandPseudoColorRenderer *>( renderer() );
r->setClassificationMin( min );
r->setClassificationMax( max );
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 )
if ( r->shader() )
{
colorRampShader->classifyColorRamp( sbpcr->band(), extent, rasterRenderer->input() );
QgsColorRampShader *colorRampShader = dynamic_cast<QgsColorRampShader *>( r->shader()->rasterShaderFunction() );
if ( colorRampShader )
{
colorRampShader->classifyColorRamp( sbpcr->band(), extent, rasterRenderer->input() );
}
}
}

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

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

setContrastEnhancement( ce->contrastEnhancementAlgorithm(),
rasterRenderer->minMaxOrigin().limits(),
extent,
SAMPLE_SIZE,
true,
rasterRenderer );
setContrastEnhancement( ce->contrastEnhancementAlgorithm(),
rasterRenderer->minMaxOrigin().limits(),
extent,
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() )
// Update main renderer so that the legends get updated
if ( singleBandRenderer )
static_cast<QgsSingleBandGrayRenderer *>( renderer() )->setContrastEnhancement( new QgsContrastEnhancement( * singleBandRenderer->contrastEnhancement() ) );
else if ( multiBandRenderer )
{
static_cast<QgsMultiBandColorRenderer *>( renderer() )->setBlueContrastEnhancement( new QgsContrastEnhancement( *multiBandRenderer->blueContrastEnhancement() ) );
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() ) );
}
}
}

emit styleChanged();
emit rendererChanged();
emit styleChanged();
emit rendererChanged();
}
}
}

@@ -1275,8 +1284,8 @@ bool QgsRasterLayer::setSubsetString( const QString &subset )
if ( res )
{
setExtent( mDataProvider->extent() );
refreshRenderer( renderer(), extent() );
emit subsetStringChanged();
triggerRepaint();
}

return res;
@@ -481,6 +481,7 @@ class CORE_EXPORT QgsRasterLayer : public QgsMapLayer
bool writeXml( QDomNode &layer_node, QDomDocument &doc, const QgsReadWriteContext &context ) const override;
QString encodedSource( const QString &source, const QgsReadWriteContext &context ) const override;
QString decodedSource( const QString &source, const QString &provider, const QgsReadWriteContext &context ) const override;

private:
//! \brief Initialize default values
void init();
@@ -501,6 +502,9 @@ class CORE_EXPORT QgsRasterLayer : public QgsMapLayer
bool generateLookupTableFlag,
QgsRasterRenderer *rasterRenderer );

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

void computeMinMax( int band,
const QgsRasterMinMaxOrigin &mmo,
QgsRasterMinMaxOrigin::Limits limits,
@@ -281,6 +281,11 @@ def testSetSubsetString(self):
self.assertEqual(int(stats.minimumValue), 136)
self.assertEqual(int(stats.maximumValue), 169)

ce = rl.renderer().contrastEnhancement()
min_max = int(ce.minimumValue()), int(ce.maximumValue())
self.assertEqual(min_max, (136, 169))

# Change filter:
self.assertTrue(rl.setSubsetString('"pk" = 1'))
block = rl.dataProvider().block(1, rl.extent(), 2, 2)
data = []
@@ -294,6 +299,12 @@ def testSetSubsetString(self):
self.assertEqual(int(stats.minimumValue), 136)
self.assertEqual(int(stats.maximumValue), 153)

# Check that the renderer has been updated
ce = rl.renderer().contrastEnhancement()
new_min_max = int(ce.minimumValue()), int(ce.maximumValue())
self.assertNotEqual(new_min_max, min_max)
self.assertEqual(new_min_max, (136, 153))

# Set invalid filter
self.assertFalse(rl.setSubsetString('"pk_wrong" = 1'))
block = rl.dataProvider().block(1, rl.extent(), 2, 2)

0 comments on commit 385acae

Please sign in to comment.
You can’t perform that action at this time.