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

C Hamilton adenaculture at gmail.com
Thu Jun 23 07:50:31 PDT 2022


Andrea,

Thanks so much. I'm not sure I would have figured that out on my own. I
also found that I needed to include the min and max values from the
statistics. Here is my final code if anyone else is trying to do this.

layer = iface.activeLayer()
provider = layer.dataProvider()
stats = provider.bandStatistics(1, QgsRasterBandStats.Min |
QgsRasterBandStats.Max)
min = stats.minimumValue
max = stats.maximumValue

# Get the Reds default colorramp
style = QgsStyle.defaultStyle()
ramp = style.colorRamp('Reds')
color_ramp = QgsColorRampShader(min, max, ramp,
QgsColorRampShader.Interpolated)
color_ramp.classifyColorRamp()

raster_shader = QgsRasterShader()
raster_shader.setRasterShaderFunction(color_ramp)

# Create a new single band pseudocolor renderer
renderer = QgsSingleBandPseudoColorRenderer(provider, layer.type(),
raster_shader)

layer.setRenderer(renderer)
layer.triggerRepaint()



Thanks,

Calvin

On Wed, Jun 22, 2022 at 9:47 PM Andrea Giudiceandrea <andreaerdna at libero.it>
wrote:

> *C Hamilton* adenaculture at gmail.com
> <qgis-user%40lists.osgeo.org?Subject=Re%3A%20%5BQgis-user%5D%20PyQGIS%20code%20on%20raster%20image%20-%20What%20am%20I%20doing%20wrong%3F&In-Reply-To=%3CCABPxTTq_rYo25hS2NdGZv4fzeQZQ%3DG%3D7NiJQK1cEyFAdGrSL9g%40mail.gmail.com%3E>
> *Wed Jun 22 08:36:45 PDT 2022*
> ------------------------------
>
> color_ramp.setColorRampType(QgsColorRampShader.Interpolated)
>
>
> Hi C Hamilton,
> I think you are missing to classify the color ramp here:
>
> color_ramp.classifyColorRamp()
>
>
> Best regards.
>
> Andrea Giudiceandrea
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-user/attachments/20220623/e4f3e2df/attachment.htm>


More information about the Qgis-user mailing list