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.
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:
pyqgis
tag is misleading.