[gdal-dev] Issues with SetColorTable() and multi-sample TIFF
afernandez
afernandez at odyhpc.com
Tue Feb 14 08:25:36 PST 2023
Hello,
I'm using the GDAL Tools plugin within QGIS to plot some raster layers. The
snippet reads:
driver_name = 'GTIFF'
driver = gdal.GetDriverByName(driver_name)
...
gdal_ds = driver.Create("my_arguments")
gdal_ds.SetProjection("my_projection")
gdal_ds.SetGeoTransform("my_geo_transform")
for band_idx in range(1, times + 1):
band = gdal_ds.GetRasterBand(band_idx)
band.SetNoDataValue(no_data)
band.SetDescription(time_step)
data = var[band_idx - 1]
data = ma.getdata(data)
band.WriteArray(data.astype(np_dtype, copy=False))
gdal_ds.FlushCache()
This works without any problem and plots the raster in greys. However, I
need the plot to be colored so I modified the code based on some posts,
which now reads:
gdal_ds = driver.Create(out_path, cols, rows, times, gdal.GDT_UInt16)
gdal_ds.SetProjection("my_projection")
gdal_ds.SetGeoTransform("my_geo_transform")
for band_idx in range(1, times + 1):
band = gdal_ds.GetRasterBand(band_idx)
band.SetNoDataValue(no_data)
band.SetDescription(time_step)
data = var[band_idx - 1]
data = ma.getdata(data)
data_scaled = np.interp(data, (data.min(), data.max()), (0, 255))
data_scaled2 = data_scaled.astype(int)
colors = gdal.ColorTable()
colors.CreateColorRamp(0, (229, 245, 249), 127, (153, 216, 201))
colors.CreateColorRamp(127, (153, 216, 201), 254, (44, 162, 95))
band.SetRasterColorTable(colors)
band.SetRasterColorInterpretation(gdal.GCI_PaletteIndex)
band.WriteArray(data_scaled2)
gdal_ds.FlushCache()
The problem is that this produces a RunTimeError: SetColorTable() not
supported for multi-sample TIFF. I simplified this for a single band and
QGIS did plot the colored raster. However, this is not a solution because
production runs will generate dozens (or even hundreds) of time step bands.
I'm pretty new to GDAL so I'm unsure if this is a feature under current
development or if there are other alternatives so any suggestion on how to
proceed would be appreciated.
Thank you in advance.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/gdal-dev/attachments/20230214/cd419fc2/attachment.htm>
More information about the gdal-dev
mailing list