Skip to content
Permalink
Browse files
more robust raster color map conversion from 1.8 project and style fi…
…le, fixes #7023
  • Loading branch information
blazek committed Jan 25, 2013
1 parent d818561 commit 3e33cf8d8613a182aa64857d5375526b1fb6444a
Showing with 52 additions and 8 deletions.
  1. +25 −0 src/core/qgsprojectfiletransform.cpp
  2. +27 −8 src/core/raster/qgspalettedrasterrenderer.cpp
@@ -617,6 +617,31 @@ void QgsProjectFileTransform::convertRasterProperties( QDomDocument& doc, QDomNo

//convert renderer specific properties
QString drawingStyle = rasterPropertiesElem.firstChildElement( "mDrawingStyle" ).text();

// While PalettedColor should normaly contain only integer values, usually
// color palette 0-255, it may happen (Tim, issue #7023) that it contains
// colormap classification with double values and text labels
// (which should normaly only appear in SingleBandPseudoColor drawingStyle)
// => we have to check first the values and change drawingStyle if necessary
if ( drawingStyle == "PalettedColor" )
{
QDomElement customColorRampElem = rasterPropertiesElem.firstChildElement( "customColorRamp" );
QDomNodeList colorRampEntryList = customColorRampElem.elementsByTagName( "colorRampEntry" );

for ( int i = 0; i < colorRampEntryList.size(); ++i )
{
QDomElement colorRampEntryElem = colorRampEntryList.at( i ).toElement();
QString strValue = colorRampEntryElem.attribute( "value" );
double value = strValue.toDouble();
if ( value < 0 || value > 10000 || value != ( int )value )
{
QgsDebugMsg( QString( "forcing SingleBandPseudoColor value = %1" ).arg( value ) );
drawingStyle = "SingleBandPseudoColor";
break;
}
}
}

if ( drawingStyle == "SingleBandGray" )
{
rasterRendererElem.setAttribute( "type", "singlebandgray" );
@@ -69,17 +69,36 @@ QgsRasterRenderer* QgsPalettedRasterRenderer::create( const QDomElement& elem, Q
if ( !paletteElem.isNull() )
{
QDomNodeList paletteEntries = paletteElem.elementsByTagName( "paletteEntry" );
nColors = paletteEntries.size();
colors = new QRgb[ nColors ];

int value = 0;
QDomElement entryElem;
int value;
nColors = 0;

// We cannot believe that data are correct, check first max value
for ( int i = 0; i < paletteEntries.size(); ++i )
{
entryElem = paletteEntries.at( i ).toElement();
// Could be written as doubles (with .0000) in old project files
value = ( int )entryElem.attribute( "value", "0" ).toDouble();
if ( value >= nColors && value <= 10000 ) nColors = value + 1;
}
QgsDebugMsg( QString( "nColors = %1" ).arg( nColors ) );

colors = new QRgb[ nColors ];

for ( int i = 0; i < nColors; ++i )
{
entryElem = paletteEntries.at( i ).toElement();
value = entryElem.attribute( "value", "0" ).toInt();
value = ( int )entryElem.attribute( "value", "0" ).toDouble();
QgsDebugMsg( entryElem.attribute( "color", "#000000" ) );
colors[value] = QColor( entryElem.attribute( "color", "#000000" ) ).rgba();
if ( value >= 0 && value < nColors )
{
colors[value] = QColor( entryElem.attribute( "color", "#000000" ) ).rgba();
}
else
{
QgsDebugMsg( QString( "value %1 out of range" ).arg( value ) );
}
}
}
QgsRasterRenderer* r = new QgsPalettedRasterRenderer( input, bandNumber, colors, nColors );
@@ -171,7 +190,7 @@ QgsRasterBlock * QgsPalettedRasterRenderer::block( int bandNo, QgsRectangle con
}

//create copy of color table with nodata values replaced by fully transparent color
QVector<QRgb> colorTable(mNColors);
QVector<QRgb> colorTable( mNColors );
for ( int i = 0; i < mNColors; ++i )
{
if ( inputBlock->isNoDataValue( i ) )
@@ -194,7 +213,7 @@ QgsRasterBlock * QgsPalettedRasterRenderer::block( int bandNo, QgsRectangle con
int val = ( int ) inputBlock->value( i );
if ( !hasTransparency )
{
outputData[i] = colorTable[ val ];
outputData[i] = colorTable.value( val );
}
else
{
@@ -207,7 +226,7 @@ QgsRasterBlock * QgsPalettedRasterRenderer::block( int bandNo, QgsRectangle con
{
currentOpacity *= alphaBlock->value( i ) / 255.0;
}
QColor currentColor = QColor( colorTable[val] );
QColor currentColor = QColor( colorTable.value( val ) );
outputData[i] = qRgba( currentOpacity * currentColor.red(), currentOpacity * currentColor.green(), currentOpacity * currentColor.blue(), currentOpacity * 255 );
}
}

0 comments on commit 3e33cf8

Please sign in to comment.