1

I am using QGIS Chugiak 2.4.0, and I have some code in which I want to:

  1. Add raster layer to map canvas
  2. Look for the minimum and maximum raster value
  3. Apply singleband pseudocolor render type for the min max values
  4. Use a nice color map (e.g. RdYlBl)
  5. Update the map canvas/legend with these settings

I found some code in the PyGis cookbook as shown below:

layer = QgsRasterLayer(file, layername)
rastermin = ?
rastermax = ?
layer.setDrawingStyle("SingleBandPseudoColor")
layer.setColorShadingAlgorithm(QgsRasterLayer.ColorRampShader)
lst = [ QgsColorRampShader.ColorRampItem(rastermin, QColor(0,255,0)), QgsColorRampShader.ColorRampItem(rastermax, QColor(255,255,0)) ]
fcn = rlayer.rasterShader().rasterShaderFunction()
fcn.setColorRampType(QgsColorRampShader.INTERPOLATED)
fcn.setColorRampItemList(lst)

First of all, I don't know how to retrieve the minimum and maximum values of the raster layer. Secondly, it seems that setColorShadingAlgorithm does not work in QGIS Chugiak 2.4.0. Thirdly, instead of using the QColor(0,255,0) I'd like to use one of the nice existing colormaps in QGIS, e.g.RdYlBl.

Does anyone have any clue how to solve this?

Best Regards, Wilco

1

I am new to PyQGIS and was actually looking for a solution to the same question: why is setColorShadingAlgorithm not working. However I do have an answer for your first question, how to calculate the min and the max values of a raster layer. There's for sure an easier and cleaner way to do it, but I've been doing it using the gdal library:

from osgeo import gdal

raster = "raster.tif"

# load raster
gdalData = gdal.Open(raster)
# get number of bands
bands = gdalData.RasterCount
# process the raster
for i in xrange(1, bands + 1):
    band = gdalData.GetRasterBand(i)
    # fetch max and min
    min = band.GetMinimum()
    max = band.GetMaximum()
    if min is None or max is None:
        (min,max) = band.ComputeRasterMinMax(1)
    print 'Min=%.3f, Max=%.3f' % (min,max)
  • +1 for working example. To simplify your script use .GetMinimum(), from gdal.org/gdal_tutorial.html search for min = band.GetMinimum() – matt wilkie Dec 4 '14 at 19:21
  • thanks a lot! I've edited the script and it looks much nicer now – fgiron Dec 4 '14 at 19:58
0

The second half of your question is answered in xunilk's answer to this GIS StackExchange post.

I have combined the two into the following Python script, which finds the min and max of the raster and applies the resulting color ramp:

from PyQt4.QtGui import *
from PyQt4.QtCore import *
from qgis.core import *
from osgeo import gdal

# use PyQT QFileInfo to get the base name of the raster file
rPath = '/path/to/raster.tif'
fileInfo = QFileInfo(rPath)
rLabel = fileInfo.baseName()

# use the file name as the label for raster
rLayer = QgsRasterLayer(rPath,rLabel)

# add the layer to the map
QgsMapLayerRegistry.instance().addMapLayer(rLayer)

# load raster
gdalData = gdal.Open(rPath)
# get number of bands
bands = gdalData.RasterCount
# process the raster
for i in xrange(1, bands + 1):
    band = gdalData.GetRasterBand(i)
    # fetch max and min
    min = band.GetMinimum()
    max = band.GetMaximum()
    if min is None or max is None:
        (min,max) = band.ComputeRasterMinMax(1)

range = max-min
add = range//2
int = min + add

colDic={'red':'#ff0000', 'yellow':'#ffff00','blue':'#0000ff'}

valueList =[min, int, max]

lst = [ QgsColorRampShader.ColorRampItem(valueList[0], QColor(colDic['blue'])), \
    QgsColorRampShader.ColorRampItem(valueList[1], QColor(colDic['yellow'])), \
    QgsColorRampShader.ColorRampItem(valueList[2], QColor(colDic['red']))]

myRasterShader = QgsRasterShader()
myColorRamp = QgsColorRampShader()

myColorRamp.setColorRampItemList(lst)
myColorRamp.setColorRampType(QgsColorRampShader.INTERPOLATED)
myRasterShader.setRasterShaderFunction(myColorRamp)

myPseudoRenderer = QgsSingleBandPseudoColorRenderer(\
    rLayer.dataProvider(), rLayer.type(),  myRasterShader)

rLayer.setRenderer(myPseudoRenderer)

# Refresh the canvas and the legend
rLayer.triggerRepaint()
iface.legendInterface().refreshLayerSymbology(rLayer)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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