2

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 )
Daniele Piccolo
  • 909
  • 4
  • 9

1 Answers1

1

Next code exemplifies how to use QgsRasterTransparency class with a singleband gray raster (you can adapt it for your RGB raster). In this case, I desire that two groups of raster values have different level of transparency. The first one (1348-1500) 100 % and the second one (2000-3000) only 50 %.

layer = iface.activeLayer()

transparency  = layer.renderer().rasterTransparency()

n = 2

pixels = [ QgsRasterTransparency.TransparentSingleValuePixel()
           for i in range(n) ]

min = [ 1348, 2000 ]
max = [ 1500, 3000]
trpcy = [100, 50 ]

for i, item in enumerate(pixels):
    item.min = min[i]
    item.max = max[i]
    item.percentTransparent = trpcy[i]

transparency.setTransparentSingleValuePixelList( pixels )

layer.triggerRepaint()  

Original situation is:

enter image description here

After running the code at the Python Console of QGIS I got:

enter image description here

xunilk
  • 29,891
  • 4
  • 41
  • 80