3

I am trying to create a for loop for coloring up multiple rasters simultaneously. I am able to do it for one layer, but trying to implement the for loop to do it for more than one layer is proving difficult.

import glob,os

rast_path="/home/user/Desktop/data/gsi/output"
rasters=glob.glob(os.path.join(rast_path,"*le.tif"))
print(rasters)

for raster in rasters:
#    (rastdir,rastfile)=os.path.split(raster)
    lyr=QgsRasterLayer(rasters,rastfile)
    s=QgsRasterShader()
    c=QgsColorRampShader()
    c.setColorRampType(QgsColorRampShader.INTERPOLATED)
    i=[]
    i.append(QgsColorRampShader.ColorRampItem(-20,QtGui.QColor('#0c0101'),'-20'))
    i.append(QgsColorRampShader.ColorRampItem(-15.1,QtGui.QColor('#ff0000'),'-15'))
    i.append(QgsColorRampShader.ColorRampItem(-10,QtGui.QColor('#ff0000'),'-10'))
    i.append(QgsColorRampShader.ColorRampItem(-5,QtGui.QColor('#ff0000'),'-5'))
    i.append(QgsColorRampShader.ColorRampItem(0,QtGui.QColor('#ff0000'),'0'))
    i.append(QgsColorRampShader.ColorRampItem(5,QtGui.QColor('#ff0000'),'5'))
    i.append(QgsColorRampShader.ColorRampItem(10,QtGui.QColor('#ff0000'),'10'))
    i.append(QgsColorRampShader.ColorRampItem(15,QtGui.QColor('#ff0000'),'15'))
    i.append(QgsColorRampShader.ColorRampItem(15,QtGui.QColor('#0c0101'),'20'))
    c.setColorRampItemList(i)
    s.setRasterShaderFunction(c)

    ps=QgsSingleBandPseudoColorRenderer(lyr.dataProvider(),1,s)
    raster.setRenderer(ps)
    QgsMapLayerRegistry.instance().addMapLayer(lyr)
#    qgis.utils.iface.addRasterLayer(raster,rastfile)
2
  • Where's your for loop? What have you tried? Where exactly are you stuck?
    – Spacedman
    Jun 30, 2016 at 16:10
  • @Spacedman, Sorry code updated there now. Jul 1, 2016 at 8:46

1 Answer 1

3

I prefer adding rasters described from the QGIS docs which I've incorporated into your script in addition to importing a couple more required classes:

import glob, os
from PyQt4 import QtGui
from PyQt4.QtCore import QFileInfo

rast_path = "/home/user/Desktop/data/gsi/output"
rasters = glob.glob(os.path.join(rast_path, "*le.tif"))
print(rasters)

for raster in rasters:
    fileInfo = QFileInfo(raster)
    path = fileInfo.filePath()
    baseName = fileInfo.baseName()
    lyr = QgsRasterLayer(path, baseName)
    s = QgsRasterShader()
    c = QgsColorRampShader()
    c.setColorRampType(QgsColorRampShader.INTERPOLATED)
    i = []
    i.append(QgsColorRampShader.ColorRampItem(-20,QtGui.QColor('#0c0101'),'-20'))
    i.append(QgsColorRampShader.ColorRampItem(-15.1,QtGui.QColor('#ff0000'),'-15'))
    i.append(QgsColorRampShader.ColorRampItem(-10,QtGui.QColor('#ff0000'),'-10'))
    i.append(QgsColorRampShader.ColorRampItem(-5,QtGui.QColor('#ff0000'),'-5'))
    i.append(QgsColorRampShader.ColorRampItem(0,QtGui.QColor('#ff0000'),'0'))
    i.append(QgsColorRampShader.ColorRampItem(5,QtGui.QColor('#ff0000'),'5'))
    i.append(QgsColorRampShader.ColorRampItem(10,QtGui.QColor('#ff0000'),'10'))
    i.append(QgsColorRampShader.ColorRampItem(15,QtGui.QColor('#ff0000'),'15'))
    i.append(QgsColorRampShader.ColorRampItem(15,QtGui.QColor('#0c0101'),'20'))
    c.setColorRampItemList(i)
    s.setRasterShaderFunction(c)
    ps = QgsSingleBandPseudoColorRenderer(lyr.dataProvider(),1,s)
    lyr.setRenderer(ps)
    QgsMapLayerRegistry.instance().addMapLayer(lyr)

Result

2
  • 1
    Excellent. This works perfect Jul 1, 2016 at 13:59
  • @user1655130 - Awesome, glad it works :)
    – Joseph
    Jul 1, 2016 at 13:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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