Skip to content

Commit

Permalink
Load raster category names by GDAL provider, fixes #8886
Browse files Browse the repository at this point in the history
  • Loading branch information
blazek committed Nov 3, 2013
1 parent 37d365c commit 8bdaf42
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
12 changes: 10 additions & 2 deletions src/core/raster/qgsrasterrendererregistry.cpp
Expand Up @@ -132,15 +132,23 @@ QgsRasterRenderer* QgsRasterRendererRegistry::defaultRendererForDrawingStyle( co
colorArraySize += 1; //usually starts at 0
QColor* colorArray = new QColor[ colorArraySize ];
colorIt = colorEntries.constBegin();
QVector<QString> labels;
for ( ; colorIt != colorEntries.constEnd(); ++colorIt )
{
colorArray[( int )( colorIt->value )] = colorIt->color;
int idx = ( int )( colorIt->value );
colorArray[idx] = colorIt->color;
if ( !colorIt->label.isEmpty() )
{
if ( labels.size() <= idx ) labels.resize( idx + 1 );
labels[idx] = colorIt->label;
}
}

renderer = new QgsPalettedRasterRenderer( provider,
grayBand,
colorArray,
colorArraySize );
colorArraySize,
labels );
}
break;
case QgsRaster::MultiBandSingleBandGray:
Expand Down
21 changes: 16 additions & 5 deletions src/providers/gdal/qgsgdalproviderbase.cpp
Expand Up @@ -60,9 +60,15 @@ QList<QgsColorRampShader::ColorRampItem> QgsGdalProviderBase::colorTable( GDALDa
{
QgsDebugMsg( "Color table found" );

// TODO: load category labels and use them for ColorRampItem.label once
// GDALGetCategoryNames C function is added to GDAL, see #8886
//char ** categoryNames = GDALGetCategoryNames( myGdalBand );
// load category labels
char ** categoryNames = GDALGetRasterCategoryNames( myGdalBand );
QVector<QString> labels;
int i = 0;
while ( categoryNames[i] )
{
labels.append( QString( categoryNames[i] ) );
i++;
}

int myEntryCount = GDALGetColorEntryCount( myGdalColorTable );
GDALColorInterp myColorInterpretation = GDALGetRasterColorInterpretation( myGdalBand );
Expand All @@ -81,20 +87,25 @@ QList<QgsColorRampShader::ColorRampItem> QgsGdalProviderBase::colorTable( GDALDa
}
else
{
QString label = labels.value( myIterator );
if ( label.isEmpty() )
{
label = QString::number( myIterator );
}
//Branch on the color interpretation type
if ( myColorInterpretation == GCI_GrayIndex )
{
QgsColorRampShader::ColorRampItem myColorRampItem;
myColorRampItem.label = "";
myColorRampItem.value = ( double )myIterator;
myColorRampItem.label = label;
myColorRampItem.color = QColor::fromRgb( myColorEntry->c1, myColorEntry->c1, myColorEntry->c1, myColorEntry->c4 );
ct.append( myColorRampItem );
}
else if ( myColorInterpretation == GCI_PaletteIndex )
{
QgsColorRampShader::ColorRampItem myColorRampItem;
myColorRampItem.value = ( double )myIterator;
myColorRampItem.label = QString::number( myColorRampItem.value );
myColorRampItem.label = label;
//Branch on palette interpretation
if ( myPaletteInterpretation == GPI_RGB )
{
Expand Down

3 comments on commit 8bdaf42

@dakcarto
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi Radim,

Something about this commit is causing QGIS to crash on Mac (though I think it is unrelated to the platform). See the following crash report.

@blazek
Copy link
Member Author

@blazek blazek commented on 8bdaf42 Nov 4, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be fixed in c59cd92, sorry.

@blazek
Copy link
Member Author

@blazek blazek commented on 8bdaf42 Nov 4, 2013 via email

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.