PyQGIS: Loading and symbolizing raster layers

More information can be found in the PyQGIS Developer Cookbook.


  • QGIS version 3.0, or greater


  • Load and symbolize a raster layer with pyqgis
  • Get statistics from a raster band

Open the QGIS Python Console

From the menu, select Plugins -> Python Console, or Ctrl + Alt + P (Windows)

You can type directly into the console, or select the pad and paper icon to write code in the editor. I prefer to write code in the editor because it allows me to save my work and run a compilation of commands.

Load a raster layer

Define the path to a raster

In the code editor, or console, create a variable containing the path to your raster file.

fn = 'c:/path/to/raster/file.tif'

Load raster into QGIS interface

Now we’ll simultaneously assign the raster layer to the rlayer variable and load it into the QGIS interface.

rlayer = iface.addRasterLayer(fn, 'layer name')

This adds the raster into the QGIS interface. In the legend, the raster is labeled ‘layer name’. My raster is symbolized with a grayscale color ramp. We’ll change the symbology next.

Change raster symbology

Get raster band statistics

First, we’ll get statistics for the raster band so we can properly stretch a color ramp. The number 1 is the raster band we are getting stats for.

stats = rlayer.dataProvider().bandStatistics(1, QgsRasterBandStats.All)

Then we’ll get the minimum and maximum values.

min = stats.minimumValue
max = stats.maximumValue

Create a color ramp shader

The color ramp shader will define the new color ramp we create to symbolize the raster. First we’ll create and empty color ramp shader. We also need to set type of color ramp we want to use. Options are InterpolatedDiscrete, or Exact.

  • Interpolated stretches colors across a range of values
  • Discrete gives all values in a range the same color
  • Exact gives each unique pixel value a unique colors

The data I’m using represent elevations, they are continuous data so I’ll use an Interpolated ramp.

fnc = QgsColorRampShader()

Define the colors for the QgsColorRampShader

Now we need to define a color scheme. For this example, I’m going to create a very simple color ramp that interpolates from green (low values) to yellow (high values).

lst = [QgsColorRampShader.ColorRampItem(min, QColor(0,255,0)),\
  QgsColorRampShader.ColorRampItem(max, QColor(255,255,0))]

Assign the color ramp to a QgsRasterShader

We’ll assign the color ramp to a QgsRasterShader so it can be used to symbolize a raster layer.

shader = QgsRasterShader()

Apply symbology to raster

Finally, we need to apply the symbology we’ve create to the raster layer. First, we’ll create a renderer using our raster shader. Then we’ll Assign the renderer to our raster layer.

renderer = QgsSingleBandPseudoColorRenderer(rlayer.dataProvider(), 1, shader)

There you have it. You can watch the video below to see the results of my raster symbolization. In a future tutorial we’ll cover how to use functions to make more complicated color ramps for rasters.

Video tutorial

Similar Posts