Skip to main content
edited tags
Link
Joseph
  • 76.3k
  • 7
  • 172
  • 286
added 315 characters in body
Source Link
user1655130
  • 650
  • 5
  • 13

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.

from PyQt4 import QtGuiglob,os

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

for raster in rasters:
#    (rastdir,"Angle"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)
lyr    raster.setRenderer(ps)
    QgsMapLayerRegistry.instance().addMapLayer(lyr)
#    qgis.utils.iface.addRasterLayer(raster,rastfile)

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.

from PyQt4 import QtGui

lyr=QgsRasterLayer("/home/user/Desktop/data/gsi/output/569600_angle.tif","Angle")
s=QgsRasterShader()
c=QgsColorRampShader()
c.setColorRampType(QgsColorRampShader.INTERPOLATED)
i=[]
i.append(QgsColorRampShader.ColorRampItem(-20,QtGui.QColor('#0c0101'),'-20'))
i.append(QgsColorRampShader.ColorRampItem(-15,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)

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)
Removed tag prefix from question title as per Jeff Atwood style preference
Source Link
underdark
  • 84.5k
  • 22
  • 233
  • 415

QGIS-Python: Add multiple rasters using QgsRasterLayer with for loop

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.

from PyQt4 import QtGui

lyr=QgsRasterLayer("/home/user/Desktop/data/gsi/output/569600_angle.tif","Angle")
s=QgsRasterShader()
c=QgsColorRampShader()
c.setColorRampType(QgsColorRampShader.INTERPOLATED)
i=[]
i.append(QgsColorRampShader.ColorRampItem(-20,QtGui.QColor('#0c0101'),'-20'))
i.append(QgsColorRampShader.ColorRampItem(-15,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)

Thanks for your help

QGIS-Python: Add multiple rasters using QgsRasterLayer with for loop

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.

from PyQt4 import QtGui

lyr=QgsRasterLayer("/home/user/Desktop/data/gsi/output/569600_angle.tif","Angle")
s=QgsRasterShader()
c=QgsColorRampShader()
c.setColorRampType(QgsColorRampShader.INTERPOLATED)
i=[]
i.append(QgsColorRampShader.ColorRampItem(-20,QtGui.QColor('#0c0101'),'-20'))
i.append(QgsColorRampShader.ColorRampItem(-15,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)

Thanks for your help

Add multiple rasters using QgsRasterLayer with for loop

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.

from PyQt4 import QtGui

lyr=QgsRasterLayer("/home/user/Desktop/data/gsi/output/569600_angle.tif","Angle")
s=QgsRasterShader()
c=QgsColorRampShader()
c.setColorRampType(QgsColorRampShader.INTERPOLATED)
i=[]
i.append(QgsColorRampShader.ColorRampItem(-20,QtGui.QColor('#0c0101'),'-20'))
i.append(QgsColorRampShader.ColorRampItem(-15,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)
Source Link
user1655130
  • 650
  • 5
  • 13
Loading