[Qgis-user] PyQGIS code on raster image - What am I doing wrong?

C Hamilton adenaculture at gmail.com
Wed Jun 22 08:36:45 PDT 2022


I am trying to automatically style a singlebandgray raster image with one
of the singlebandpseudocolor color ramps. When I run this code in the QGIS
Python console the raster image disappears. If I look at the image
"Properties" everything seems to be set correctly and it does appear when
Apply or OK buttons are clicked on. I must be missing something.

layer = iface.activeLayer()

style = QgsStyle.defaultStyle()
# Get the Reds default colorramp
ramp = style.colorRamp('Reds')
color_ramp = QgsColorRampShader()
color_ramp.setSourceColorRamp(ramp)
color_ramp.setColorRampType(QgsColorRampShader.Interpolated)

raster_shader = QgsRasterShader()
raster_shader.setRasterShaderFunction(color_ramp)
# Create a new single band pseudocolor renderer
renderer = QgsSingleBandPseudoColorRenderer(layer.dataProvider(),
layer.type(), raster_shader)

layer.setRenderer(renderer)
layer.triggerRepaint()
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-user/attachments/20220622/eae21c95/attachment.htm>


More information about the Qgis-user mailing list