1

I've created a raster object x in R, which is a categorical classification. I've used levels(x) <- RAT with a properly formatted raster attribute table to give the categories names, and then used writeRaster to export to GeoTIFF.

Running gdalinfo on the tiff, I can see the categories in the band information. Looks like:

Band 1 Block=799x5 Type=Int16, ColorInterp=Gray
  Min=1.000 Max=5.000
  Minimum=1.000, Maximum=5.000, Mean=2.364, StdDev=2.147
  NoData Value=-9999
  Categories:
    0: 
    1: typeA
    2: typeB
    3: typeC
    4: typeD
    5: typeE
Metadata:
  STATISTICS_MAXIMUM=5
  STATISTICS_MEAN=2.364214323665
  STATISTICS_MINIMUM=1
  STATISTICS_STDDEV=2.1472654314354

So that's great, but QGIS can't seem to access that category info when I'm styling the layer. Is that even possible? They should act as legend labels.

3

Although QGIS reads the category names, I don't think using them in symbology directly is currently possible.

For example (using python), I set some category names:

rb.SetCategoryNames(['a','b','c'])

GDAL shows the categories:

$ gdalinfo test.tif
Driver: GTiff/GeoTIFF
Files: test.tif
       test.tif.aux.xml
Size is 512, 512
Coordinate System is `'
Image Structure Metadata:
  INTERLEAVE=BAND
Corner Coordinates:
Upper Left  (    0.0,    0.0)
Lower Left  (    0.0,  512.0)
Upper Right (  512.0,    0.0)
Lower Right (  512.0,  512.0)
Center      (  256.0,  256.0)
Band 1 Block=512x16 Type=Byte, ColorInterp=Gray
  Categories:
      0: a
      1: b
      2: c

In the QGIS layer properties, the category names show in the metadata section, but aren't available in the symbology section.

enter image description here

However...

If I set a Colour Table (again using python rather than r, but concept is the same):

ct=gdal.ColorTable()
ct.CreateColorRamp(0,(0,50,200),2,(0,255,200))
rb.SetCategoryNames(['a','b','c'])
rb.SetColorTable(ct)

I can see the category names in the symbology:

enter image description here

Note: I haven't tried getting rid of/not showing the unused colour table entries, I don't know if it's possible.

  • Looks like you've replicated exactly what I did in Python, cool. I don't think setting categories either way we've tried counts as a true RAT though - ArcGIS can't use them in its Build Attribute Table tool. – obrl_soil Oct 27 '16 at 23:48
  • Not exactly. I also set the colour table, which then uses the category names in the symbology and the legend which is what I thought you were trying to do. – user2856 Oct 27 '16 at 23:50
  • @obrl_soil If you build a RAT (even just 2 columns, value and category) using GDAL, ArcGIS will use the entries in a unique values renderer by default. QGIS though can't read RATs at all, so you'd need to use a colour table like I suggest in my answer. – user2856 Oct 28 '16 at 0:20
  • Looks like I'd have to move away from GeoTIFF to get support for RATs anyway. – obrl_soil Oct 28 '16 at 0:53
  • @obrl_soil lack of RAT support is a QGIS issue not a geotiff issue. – user2856 Oct 28 '16 at 0:58
0

You could also do that by creating a virtual raster file (.vrt) in QGIS and editing it manually in any text editor.

All classes and colors will be interpreted from 0 on, thus I include NoData here as a hint to that.

<VRTDataset rasterXSize="109300" rasterYSize="89999">
  <VRTRasterBand dataType="Byte" band="1">
  ...
    <ColorInterp>Palette</ColorInterp>
    <ColorTable>
      <Entry c1="255" c2="255" c3="255" c4="0"/>
      <Entry c1="204" c2="11" c3="30" c4="255"/>
      <Entry c1="187" c2="174" c3="108" c4="255"/>
      <Entry c1="21" c2="182" c3="103" c4="255"/>
      ...
    </ColorTable>
    <CategoryNames>
      <Category>NoData</Category>
      <Category>typeA</Category>
      <Category>typeB</Category>
      <Category>typeC</Category>
      ...
    </CategoryNames>
  ...
  </VRTRasterBand>
</VRTDataset>

If you export this VRT to a new GeoTiff (Right Click-> Export -> Save As...) you will end up with the colors encoded in the .tif and the classes in the associated .aux.xml

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.