symbol for categorized raster looks like a mask

Issue #674 resolved
Andreas Janz created an issue

Find test file attached. The default renderer is a PalettedRenderer because of the QML sidecar. In the Data Views panel category legend looks good. Not sure why it is concidered a mask (?)

Comments (8)

  1. Andreas Janz reporter

    Also, opening the raster via “Open in new map… / Default Colors” is not using the default style from the QML:

  2. Benjamin Jakimow

    resolves #674 - DataSourceRaster().createUnregistereMapLayer() loads default style - DataSourceTreeView().openInMap( ... DEFAULT) tries to get default style from map layer first, GDAL second.

    → <<cset e0eb121d141d>>

  3. Andreas Janz reporter

    @Benjamin Jakimow looks good now, thanks!

    Regarding GDAL Categories: I think you do not have to handle GDAL categories at all. If GDAL categories are available, QGIS will default to a paletted raster renderer. Do you see any case where checking the GDAL categories explicitely is required? At leased I’m fully relying only on QGIS inside my algorithms. Maybe I’m missing something(?)

  4. Benjamin Jakimow

    Nope, without a GDAL color table and categories only, a QgsRasterLayer will get a QgsSingleBandGrayRenderer. Try this:

    import numpy as np
    from osgeo import gdal_array, gdal
    from qgis.core import QgsRasterLayer, QgsPalettedRasterRenderer
    array = np.asarray([[1,2,3,2],
                        [0,1,1,2]]).astype(np.uint8)
    path = '/vsimem/myimage.tif'
    ds: gdal.Dataset = gdal_array.SaveArray(array, path)
    band: gdal.Band = ds.GetRasterBand(1)
    band.SetCategoryNames(['none', 'A', 'B', 'C', 'D'])
    
    # set True to add colors and get a QgsPalettedRasterRenderer instead
    if False:
        ct = gdal.ColorTable()
        ct.SetColorEntry(0, (0, 0, 0))
        ct.SetColorEntry(1, (0, 255, 0))
        ct.SetColorEntry(3, (255, 0, 0))
        band.SetColorTable(ct)
    
    ds.FlushCache()
    lyr = QgsRasterLayer(path)
    assert lyr.isValid()
    renderer = lyr.renderer()
    print(renderer)
    

  5. Log in to comment