In the question: "GIS Action that loads a raster, creates pyramids and sets color" I have resolve the question for raster in B/W but now I need it to load RGB raster with white background that I would like transparent.
I think to need QgsRasterTransparency::alphaValue but I have some problem how to use it in code. Any help is welcome
from PyQt4.QtCore import QFileInfo,QSettings
from qgis.core import QgsRasterLayer, QgsCoordinateReferenceSystem
import gdal
import os
from subprocess import call
#Define gdaladdo
gdaladdoFile = 'C:\\Program Files\\QGIS 2.18\\bin\\gdaladdo.exe'
s = QSettings()
oldValidation = s.value( "/Projections/defaultBehaviour" )
s.setValue( "/Projections/defaultBehaviour", "useProject" )
vl = QgsMapLayerRegistry.instance().mapLayersByName('catalogo_CTR')[0]
qgis.utils.iface.setActiveLayer(vl)
root = QgsProject.instance().layerTreeRoot()
group_name = "CTR_10000"
group = root.findGroup(group_name)
if group == None:
group = root.addGroup("CTR_10000")
else:
pass
fileName = 'D:\\GIS\\Dati\\IDT_CTRR_Raster\\[% A_CODICE %].tif'
fileInfo = QFileInfo(fileName)
baseName = '[% A_CODICE %]'
rlayer = QgsRasterLayer(fileName, baseName)
call([gdaladdoFile, '-ro', '--config', 'USE_RRD', 'YES', fileName, '2 4 8 16'])
raster_transparency = rlayer.renderer().rasterTransparency()
ltr = QgsRasterTransparency.TransparentSingleValuePixel()
ltr2 = QgsRasterTransparency.TransparentSingleValuePixel()
tr_list = []
ltr.min = 0 # Or another value
ltr.max = 0 # Or another value
ltr.percentTransparent = 100 # Or another value
ltr2.min = 8 # Or another value
ltr2.max = 8 # Or another value
ltr2.percentTransparent = 100
tr_list.append(ltr)
tr_list.append(ltr2)
rlayer.triggerRepaint() # Tried with iface.mapCanvas().refresh(), but it didn't work
raster_transparency.setTransparentSingleValuePixelList(tr_list)
crs = QgsCoordinateReferenceSystem()
crs.createFromSrid(3003)
rlayer.setCrs(crs)
QgsMapLayerRegistry.instance().addMapLayer(rlayer, False)
group.insertChildNode(-1, QgsLayerTreeLayer(rlayer))
s.setValue( "/Projections/defaultBehaviour", oldValidation )

