What is the bug or the crash?
If a GDAL datasets contain a mask band with gdal.GMF_PER_DATASET, the band is exposed as alpha band, so that the corresponding QgsRasterLayer shows more bands than the GDAL dataset (see QgsGdalProvider::initBaseDataset()
QgsRasterLayer(path).bandCount() == gdal.Open(path).RasterCount + 1 # <-- Yes!
Adding such a raster layer to the QgsRasterBandComboBox print the following error:
ERROR 5: test.tif: GDALDataset::GetRasterBand(4) - Illegal band #
Steps to reproduce the issue
The following PyQGIS code reproduces the error:
from pathlib import Path
import numpy as np
from osgeo import gdal, osr
from qgis.core import QgsRasterLayer
from qgis.gui import QgsRasterBandComboBox
from qgis.testing import start_app
gdal.UseExceptions()
start_app()
path = str(Path(__file__).parent / r'test.tif')
# Raster size and bands
cols, rows, bands = 256, 256, 3
dtype = gdal.GDT_Int16
# Create GeoTIFF
driver = gdal.GetDriverByName("GTiff")
ds = driver.Create(path, cols, rows, bands, dtype)
# Optional georeferencing (not required for alpha to work)
ds.SetGeoTransform((0, 1, 0, 0, 0, -1))
srs = osr.SpatialReference()
srs.ImportFromEPSG(4326)
ds.SetProjection(srs.ExportToWkt())
# Write random data to the 3 bands
for b in range(1, bands + 1):
band = ds.GetRasterBand(b)
data = np.random.randint(0, 256, (rows, cols), dtype=np.uint8)
band.WriteArray(data)
band.SetNoDataValue(0)
band.FlushCache()
# Create a per-dataset mask (this is what QGIS shows as an alpha band)
ds.CreateMaskBand(gdal.GMF_PER_DATASET)
ds.FlushCache()
options = QgsRasterLayer.LayerOptions(loadDefaultStyle=False)
lyr = QgsRasterLayer(path, 'test')
# note the difference
# due to void QgsGdalProvider::initBaseDataset() in
# src/core/providers/gdal/qgsgdalprovider.cpp
assert ds.RasterCount == 3
assert lyr.bandCount() == 4
box = QgsRasterBandComboBox()
# this prints: ERROR 5: test.tif: GDALDataset::GetRasterBand(4) - Illegal band #
box.setLayer(lyr)
Versions
| QGIS version | 3.44.2-Solothurn |
| QGIS code branch | Release 3.44 |
| |
| Libraries |
| Qt version | 5.15.15 |
| Python version | 3.13.5 (Compiled) 3.13.11 (Running) |
| GDAL version | 3.11.3 (Compiled) 3.11.4 (Running) — Eganville |
| PROJ version | 9.6.2 |
| EPSG Registry database version | v12.013 (2025-05-26) |
| GEOS version | 3.14.0-CAPI-1.20.4 |
| SQLite version | 3.50.4 (Compiled) 3.51.2 (Running) |
| PDAL version | 2.9.2 |
| PostgreSQL client version | 17.6 |
| SpatiaLite version | 5.1.0 |
| QWT version | 6.3.0 |
| QScintilla2 version | 2.14.1 |
| OS version | Fedora Linux 43 (Workstation Edition) |
| |
| Active Python plugins |
| processing | 2.12.99 |
Supported QGIS version
New profile
Additional context
No response
What is the bug or the crash?
If a GDAL datasets contain a mask band with
gdal.GMF_PER_DATASET, the band is exposed as alpha band, so that the corresponding QgsRasterLayer shows more bands than the GDAL dataset (see QgsGdalProvider::initBaseDataset()Adding such a raster layer to the QgsRasterBandComboBox print the following error:
Steps to reproduce the issue
The following PyQGIS code reproduces the error:
Versions
3.13.11 (Running)
3.11.4 (Running) — Eganville
3.51.2 (Running)
Supported QGIS version
New profile
Additional context
No response