3

I want to load a TIFF and classify its Rastervalues

        QList < QgsColorRampShader::ColorRampItem > lst = QList<QgsColorRampShader::ColorRampItem >();    
     
        // ... filling the list with values, labels and colors
    
        int cnt = lst.count()-1;
        QColor startCol = lst.at(0).color;
        QColor stopCol = lst.at(cnt).color;
        
        qDebug() << QString::number(0) << " - " << startCol.name();
        qDebug() << QString::number(cnt) << " - " << stopCol.name();
        
        QgsGradientStopsList stops = QgsGradientStopsList();
        for (int i = 1;i < cnt;i++) {
          double fraction = static_cast<double>(i) / cnt;
          stops.append(QgsGradientStop(fraction, lst.at(i).color));
        
          qDebug() << QString::number(fraction) + " - " + lst.at(i).color.name()+" ClassBreak: " + lst.at(i).label + " " + QString::number(lst.at(i).value);
        }
        QgsGradientColorRamp* colRamp = new QgsGradientColorRamp(startCol, stopCol, false, stops);
        
        QgsColorRampShader* fcn = new QgsColorRampShader(0,255,colRamp, QgsColorRampShader::Discrete, QgsColorRampShader::ClassificationMode::Continuous);
        
        fcn->setColorRampItemList(lst);
        
        
        QgsRasterShader * shader = new QgsRasterShader();
        shader->setRasterShaderFunction(fcn);
        QgsRasterLayer* rLyr=static_cast<QgsRasterLayer*>(aktLayer->qgsLayer);
        QgsSingleBandPseudoColorRenderer *renderer = new QgsSingleBandPseudoColorRenderer(rLyr->renderer(), 1, shader);
        
        rLyr->setRenderer(renderer);

This is the output of debugging:

"0"  -  "#4f6e31"
"7"  -  "#0048ff"
"0.142857 - #86ff4a ClassBreak: 5 - 10 5"
"0.285714 - #ffd900 ClassBreak: 10 - 25 10"
"0.428571 - #c77042 ClassBreak: 25 - 50 25"
"0.571429 - #a63808 ClassBreak: 50 - 75 50"
"0.714286 - #f824ff ClassBreak: 75 - 100 75"
"0.857143 - #c300ff ClassBreak: 100 - 150 100"

The output is ok. Values, labels and colors are correct.

wrong walues in layer properties

The layer is loaded in QGIS, legend is shown correct, but symbolizing in layerproperties shows different values. Where they come from?

Also the rendering in QGIS is wrong.

What am I missing?

If I don't set up a colorramp, the layer will be rendered correctly but then the legend isn't shown allright.

EDIT: ported the code to python and tested in python console:

    from qgis.core import (
    QgsColorRampShader,
    QgsRasterShader,
    QgsSingleBandPseudoColorRenderer
)
from PyQt5.QtGui import QColor

lst = [
    QgsColorRampShader.ColorRampItem(0, QColor("#4f6e31"), "<= 5"),
    QgsColorRampShader.ColorRampItem(5, QColor("#86ff4a"), "5 - 10"),
    QgsColorRampShader.ColorRampItem(10, QColor("#ffd900"), "10 - 25"),
    QgsColorRampShader.ColorRampItem(25, QColor("#c77042"), "25 - 50"),
    QgsColorRampShader.ColorRampItem(50, QColor("#a63808"), "50 - 75"),
    QgsColorRampShader.ColorRampItem(75, QColor("#f824ff"), "75 - 100"),
    QgsColorRampShader.ColorRampItem(100, QColor("#c300ff"), "100 - 150"),
    QgsColorRampShader.ColorRampItem(150, QColor("#0048ff"), "> 150")
]

cnt = len(lst) - 1
startCol = lst[0].color
stopCol = lst[cnt].color

print(f"{0} - {startCol.name()}")
print(f"{cnt} - {stopCol.name()}")

stops = []
for i in range(1, cnt):
    fraction = float(i) / cnt
    stops.append(QgsGradientStop(fraction, lst[i].color))
    print(f"{fraction} - {lst[i].color.name()} ClassBreak: {lst[i].label} {lst[i].value}")


colRamp = QgsGradientColorRamp(startCol, stopCol, False, stops)

fcn = QgsColorRampShader(0,255,colRamp,QgsColorRampShader.Discrete,QgsColorRampShader.Continuous)
fcn.setColorRampItemList(lst);

shader = QgsRasterShader()
shader.setRasterShaderFunction(fcn)

rLyr = iface.activeLayer()
renderer = QgsSingleBandPseudoColorRenderer(rLyr.dataProvider(), 1, shader)

rLyr.setRenderer(renderer)
rLyr.triggerRepaint()

Result is bad: Layer is rendered wrong, legend is ok but in layerproperty only the colors are good: python example

Here is a picture of the correct rendering:correct rendering

4
  • Is this code snippet written in python? I don't recognize the syntax. Therefore, the pyqgis tag is misleading. Commented Sep 5 at 10:43
  • 2
    It is written in c++ but the used classes are very similar and so the code is easy to read for a pyqgis programmer Commented Sep 5 at 10:49
  • Can you share the raster?
    – Kartograaf
    Commented Sep 25 at 14:58
  • 1
    @Kartograaf gfi.themenbrowser.de/zips/N_Eintraege.aux.tif (18MB) Commented Sep 26 at 6:55

1 Answer 1

2
+50

To fix the problem with the rendering, simply change the numeric values in the list of ColorRampItem() objects to represent the max for each color rather than the min, as in:

lst = [
    QgsColorRampShader.ColorRampItem(5, QColor("#4f6e31"), "<= 5"),
    QgsColorRampShader.ColorRampItem(10, QColor("#86ff4a"), "5 - 10"),
    QgsColorRampShader.ColorRampItem(25, QColor("#ffd900"), "10 - 25"),
    QgsColorRampShader.ColorRampItem(50, QColor("#c77042"), "25 - 50"),
    QgsColorRampShader.ColorRampItem(75, QColor("#a63808"), "50 - 75"),
    QgsColorRampShader.ColorRampItem(100, QColor("#f824ff"), "75 - 100"),
    QgsColorRampShader.ColorRampItem(150, QColor("#c300ff"), "100 - 150"),
    QgsColorRampShader.ColorRampItem(255, QColor("#0048ff"), "> 150")
]

The issue with the symbology tab is reminiscent of a known bug from early QGIS 3 that was patched, but has apparently reemerged. When I made the change above and ran the code in QGIS 3.32.2, I also observed that there was a discrepancy between the layers tree view and the properties->symbology tab. Running the code in QGIS 3.16.1 gives the expected results. See images below and find the old bug described here and here.

Edit: The return of the bug has been reported here.

Map after code change: map_canvas Symbology tab in QGIS 3.32.2: new_version_symbology Symbology tab in QGIS 3.16.1: old_version_symbology

2
  • Thank yo so much for attending to the problem. But why in github the re-reported bug has been marked as 'stale'? Commented Sep 27 at 10:18
  • 1
    Thats a good catch, I think that happens automatically when an issue is left dormant. I opened a new issue for the problem so the QGIS devs will see it github.com/qgis/QGIS/issues/58892.
    – Kartograaf
    Commented Sep 27 at 14:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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