Skip to content

Commit 15fe4da

Browse files
committed
Better array generation for paletted raster renderer
1 parent f16ca52 commit 15fe4da

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

src/core/raster/qgsrasterlayer.cpp

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -597,23 +597,35 @@ void QgsRasterLayer::setRendererForDrawingStyle( const DrawingStyle & theDrawin
597597
{
598598
case PalettedColor:
599599
{
600-
//todo: go through list and take maximum value (it could be that entries don't start at 0 or indices are not contiguous
601600
int grayBand = bandNumber( grayBandName() );
602601
QgsColorRampShader* colorRampShader = dynamic_cast<QgsColorRampShader*>( rasterShader()->rasterShaderFunction() );
603602
if ( colorRampShader )
604603
{
605604
QList<QgsColorRampShader::ColorRampItem> colorEntries = colorRampShader->colorRampItemList();
606-
QColor* colorArray = new QColor[ colorEntries.size()];
605+
606+
//go through list and take maximum value (it could be that entries don't start at 0 or indices are not contiguous)
607+
int colorArraySize = 0;
607608
QList<QgsColorRampShader::ColorRampItem>::const_iterator colorIt = colorEntries.constBegin();
608609
for ( ; colorIt != colorEntries.constEnd(); ++colorIt )
610+
{
611+
if ( colorIt->value > colorArraySize )
612+
{
613+
colorArraySize = ( int )( colorIt->value );
614+
}
615+
}
616+
617+
colorArraySize += 1; //usually starts at 0
618+
QColor* colorArray = new QColor[ colorArraySize ];
619+
colorIt = colorEntries.constBegin();
620+
for ( ; colorIt != colorEntries.constEnd(); ++colorIt )
609621
{
610622
colorArray[( int )( colorIt->value )] = colorIt->color;
611623
}
612624

613625
renderer = new QgsPalettedRasterRenderer( mDataProvider,
614626
grayBand,
615627
colorArray,
616-
colorEntries.size() );
628+
colorArraySize );
617629
}
618630
else //try to get it from the color table
619631
{

0 commit comments

Comments
 (0)