Skip to content

Commit 8bdaf42

Browse files
committedNov 3, 2013
Load raster category names by GDAL provider, fixes #8886
1 parent 37d365c commit 8bdaf42

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed
 

‎src/core/raster/qgsrasterrendererregistry.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,15 +132,23 @@ QgsRasterRenderer* QgsRasterRendererRegistry::defaultRendererForDrawingStyle( co
132132
colorArraySize += 1; //usually starts at 0
133133
QColor* colorArray = new QColor[ colorArraySize ];
134134
colorIt = colorEntries.constBegin();
135+
QVector<QString> labels;
135136
for ( ; colorIt != colorEntries.constEnd(); ++colorIt )
136137
{
137-
colorArray[( int )( colorIt->value )] = colorIt->color;
138+
int idx = ( int )( colorIt->value );
139+
colorArray[idx] = colorIt->color;
140+
if ( !colorIt->label.isEmpty() )
141+
{
142+
if ( labels.size() <= idx ) labels.resize( idx + 1 );
143+
labels[idx] = colorIt->label;
144+
}
138145
}
139146

140147
renderer = new QgsPalettedRasterRenderer( provider,
141148
grayBand,
142149
colorArray,
143-
colorArraySize );
150+
colorArraySize,
151+
labels );
144152
}
145153
break;
146154
case QgsRaster::MultiBandSingleBandGray:

‎src/providers/gdal/qgsgdalproviderbase.cpp

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,15 @@ QList<QgsColorRampShader::ColorRampItem> QgsGdalProviderBase::colorTable( GDALDa
6060
{
6161
QgsDebugMsg( "Color table found" );
6262

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

6773
int myEntryCount = GDALGetColorEntryCount( myGdalColorTable );
6874
GDALColorInterp myColorInterpretation = GDALGetRasterColorInterpretation( myGdalBand );
@@ -81,20 +87,25 @@ QList<QgsColorRampShader::ColorRampItem> QgsGdalProviderBase::colorTable( GDALDa
8187
}
8288
else
8389
{
90+
QString label = labels.value( myIterator );
91+
if ( label.isEmpty() )
92+
{
93+
label = QString::number( myIterator );
94+
}
8495
//Branch on the color interpretation type
8596
if ( myColorInterpretation == GCI_GrayIndex )
8697
{
8798
QgsColorRampShader::ColorRampItem myColorRampItem;
88-
myColorRampItem.label = "";
8999
myColorRampItem.value = ( double )myIterator;
100+
myColorRampItem.label = label;
90101
myColorRampItem.color = QColor::fromRgb( myColorEntry->c1, myColorEntry->c1, myColorEntry->c1, myColorEntry->c4 );
91102
ct.append( myColorRampItem );
92103
}
93104
else if ( myColorInterpretation == GCI_PaletteIndex )
94105
{
95106
QgsColorRampShader::ColorRampItem myColorRampItem;
96107
myColorRampItem.value = ( double )myIterator;
97-
myColorRampItem.label = QString::number( myColorRampItem.value );
108+
myColorRampItem.label = label;
98109
//Branch on palette interpretation
99110
if ( myPaletteInterpretation == GPI_RGB )
100111
{

3 commit comments

Comments
 (3)

dakcarto commented on Nov 3, 2013

@dakcarto
Member

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 commented on Nov 4, 2013

@blazek
MemberAuthor

It should be fixed in c59cd92, sorry.

blazek commented on Nov 4, 2013

@blazek
MemberAuthor
Please sign in to comment.