Skip to content

ERROR 5: test.tif: GDALDataset::GetRasterBand - Illegal band #64642

Description

@jakimowb

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 version3.44.2-Solothurn
QGIS code branchRelease 3.44
 
Libraries
Qt version5.15.15
Python version3.13.5 (Compiled)
3.13.11 (Running)
GDAL version3.11.3 (Compiled)
3.11.4 (Running) — Eganville
PROJ version9.6.2
EPSG Registry database versionv12.013 (2025-05-26)
GEOS version3.14.0-CAPI-1.20.4
SQLite version3.50.4 (Compiled)
3.51.2 (Running)
PDAL version2.9.2
PostgreSQL client version17.6
SpatiaLite version5.1.0
QWT version6.3.0
QScintilla2 version2.14.1
OS versionFedora Linux 43 (Workstation Edition)
 
Active Python plugins
processing2.12.99

Supported QGIS version

  • I'm running a supported QGIS version according to the roadmap.

New profile

Additional context

No response

Metadata

Metadata

Assignees

Labels

BugEither a bug report, or a bug fix. Let's hope for the latter!RastersRelated to general raster layer handling (not specific data formats)

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions