Bug report #8886

allow paletted rasters to have class labels

Added by Vincent Schut over 10 years ago. Updated over 10 years ago.

Status:Closed
Priority:Low
Assignee:Radim Blazek
Category:-
Affected QGIS version:2.0.1 Regression?:No
Operating System: Easy fix?:No
Pull Request or Patch supplied:No Resolution:
Crashes QGIS or corrupts data:No Copied to github as #:17565

Description

Since version 2 qgis does not seem to support paletted rasters (e.g. tiff's with colortable) with a label for each raster class. In version 1.8, in the colormap tab of the raster properties, the internal palette of the raster would automatically be used, and you could add (or qgis would load, if they were available in the raster metadata, e.g. ENVI classification files) a label/name for each of the colors/classes. In the new version, a raster is either of type 'palette' but that has no option to use/add labels (but does use the internal palette of the raster), or it is rendered as 'singleband pseudocolor', which has labels per class, but cannot use the raster internal colortable.

Attached is a tar.gz archive with sample data: a paletted raster as ENVI and the same one as geotiff, with qml files from qgis 1.8 showing the class labels.

Example_thematic_rasterdata_withlabels.tar.gz (10.8 KB) Vincent Schut, 2013-10-17 03:30 AM

Associated revisions

Revision 24bcec43
Added by Radim Blazek over 10 years ago

[FEATURE] labels support in QgsPalettedRasterRenderer, fixes #8886

Revision 8bdaf42d
Added by Radim Blazek over 10 years ago

Load raster category names by GDAL provider, fixes #8886

History

#1 Updated by Radim Blazek over 10 years ago

QGIS 1.8: raster with color table is rendered as QgsRasterLayer::PalettedColor by QgsColorRampShader which keeps color entries as ColorRampItem with value/color/label.

QGIS 2.0: raster with color table is rendered by QgsPalettedRasterRenderer which only keeps arrays of colors (no values, no labels), raster values are used as indexes to the array of colors.

Equivalent of QGIS 1.8 PalettedColor/QgsColorRampShader in QGIS 2.0 is QgsSingleBandPseudoColorRenderer which is using QgsColorRampShader and ColorRampItem with value/color/label.

IIRC, the QgsPalettedRasterRenderer in QGIS 2.0 was added to make rendering of rasters with large color tables (maybe up to 2 bytes = 65536 colors) fast. Marco, please comment.

In fact, at least in GDAL, there is not way to get any labels for colors, see http://www.gdal.org/structGDALColorEntry.html. I don't see any mechanism in 1.8 how it could load labels from original data through GDAL. And it does not, it can only load labels from QGIS *.qml files. Vincent, can you confirm that it does not load any labels if *.qml files are not present? I.e. there is no other way to get labels to QGIS 1.8 than to add them manualy (except using existing .qml files of course), right?

Possible solutions for your problem in 2.x:

1) Load rasters with color table with default QgsSingleBandPseudoColorRenderer. This would be very bad because it would slow down significantly rendering of rasters with large color tables.

2) Add optional labels to QgsPalettedRasterRenderer as array of labels.

3) Add a tool (button) to Singleband pseudocolor (QgsSingleBandPseudoColorRenderer) GUI options panel to load entries from raster color table if it exists.

#2 Updated by Radim Blazek over 10 years ago

  • Status changed from Open to Feedback

#3 Updated by Vincent Schut over 10 years ago

There is a way in gdal to get labels for paletted/categorized rasters, though I'm not sure how generic it is. At least for me, if I gdalinfo the envi raster that is in the sample data in the first report, I get both a list of 'categories' and a color table:

gdalinfo thematicraster-envi.bin                                                                                                                                                    
Driver: ENVI/ENVI .hdr Labelled                                                                                                                                                                                      
Files: thematicraster-envi.bin                                                                                                                                                                                       
       thematicraster-envi.bin.hdr
Size is 258, 320
Coordinate System is `'
Metadata:
  Band_1=ENVI Class image
Image Structure Metadata:
  INTERLEAVE=BAND
Corner Coordinates:
Upper Left  (    0.0,    0.0)
Lower Left  (    0.0,  320.0)
Upper Right (  258.0,    0.0)
Lower Right (  258.0,  320.0)
Center      (  129.0,  160.0)
Band 1 Block=258x1 Type=Byte, ColorInterp=Palette
  Description = ENVI Class image
  NoData Value=0
  Categories:
      0: Background
      1: Non-forest
      2: Forest
      3: Old changes
      4: Change 1
      5: Change 2
      6: Change 3
      7: Change 4
      8: Change 5
      9: Change 6
     10: Change 7
  Color Table (RGB with 11 entries)
    0: 0,0,0,255
    1: 200,200,200,255
    2: 0,110,0,255
    3: 110,65,0,255
    4: 0,0,255,255
    5: 0,89,255,255
    6: 0,174,255,255
    7: 0,255,246,255
    8: 238,255,0,255
    9: 255,182,0,255
   10: 255,93,0,255

You are correct, however, that qgis has never used these. It would be super nice if it did, of course :-)

Regarding what solution would be best, I'd go for 2: add a array of labels (or 'categories') to QgsPalettedRasterRenderer.
Most or even all features of Singleband Pseudocolor imho apply more to a continuous scale than to categories (though a button to load a raster colortable as a pseudocolor map would certainly have its uses).

Summarizing:
- solution 2 seems most appropriate, and would mostly fill our needs;
- there is a way in gdal to get to category labels, it would be great is qgis would load these when available;
- as a bonus there could be this button in the singleband pseudocolor to import a raster's colortable. Low prio, though, imho.

#4 Updated by Vincent Schut over 10 years ago

NB the categories support in gdal seems generic, see the GDALRasterBand:GetCategoryNames method (http://www.gdal.org/classGDALRasterBand.html#a9602a34799a8a9d72f0a7b99e06864a5). It just is not defined for geotiff, as this has never had categories metadata support.

#5 Updated by Radim Blazek over 10 years ago

  • Status changed from Feedback to Closed

#6 Updated by Radim Blazek over 10 years ago

  • Status changed from Closed to Reopened
  • Priority changed from Normal to Low

Vincent Schut wrote:

NB the categories support in gdal seems generic, see the GDALRasterBand:GetCategoryNames method (http://www.gdal.org/classGDALRasterBand.html#a9602a34799a8a9d72f0a7b99e06864a5). It just is not defined for geotiff, as this has never had categories metadata support.

You are right, I was not aware. Unfortunately for some strange reason there is no GDALGetCategoryNames C function (we are using C bindings). I asked in GDAL list:
http://lists.osgeo.org/pipermail/gdal-dev/2013-November/037411.html

With GDALGetCategoryNames it would be easy to load labels in QgsGdalProviderBase::colorTable()

#7 Updated by Radim Blazek over 10 years ago

  • Status changed from Reopened to Closed

#8 Updated by Radim Blazek over 10 years ago

Implemented also loading of category names by GDAL provider from original data. The function name is GDALGetRasterCategoryNames().

Also available in: Atom PDF