<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  </head>
  <body>
    <p>Hi,</p>
    <p>The TIFF format only supports only single color table per TIFF
      image, and it only makes sense for a single-sample TIFF. An
      alternative would be for you to create multiple TIFF image file
      directories in the same file (mapped as GDAL subdatasets), with
      one band per subdataset, by using the APPEND_SUBDATASET=YES
      creation option.</p>
    <p>Another option is to use the KEA format (specialization of HDF5)
      which supports all the possibilities of the GDAL abstract data
      model.</p>
    <p>Even<br>
    </p>
    <div class="moz-cite-prefix">Le 14/02/2023 à 17:25, afernandez a
      écrit :<br>
    </div>
    <blockquote type="cite"
      cite="mid:cf35d819b16e3.4ed0e9f08beef@odyhpc.com">
      <div>
        <div id="compose-body-wrapper" dir="auto">
          <div dir="auto">
            <div dir="auto">Hello,</div>
            <div dir="auto">I'm using the GDAL Tools plugin within QGIS
              to plot some raster layers. The snippet reads:</div>
            <div dir="auto"><i>        driver_name = 'GTIFF'</i></div>
            <div dir="auto"><i>        driver =
                gdal.GetDriverByName(driver_name) </i></div>
            <div dir="auto"><i>        ...</i></div>
            <div dir="auto"><i>        gdal_ds =
                driver.Create("my_arguments") </i></div>
            <div dir="auto"><i>       
                gdal_ds.SetProjection("my_projection")</i></div>
            <div dir="auto"><i>       
                gdal_ds.SetGeoTransform("my_geo_transform")</i></div>
            <div dir="auto"><i>        for band_idx in range(1, times +
                1):</i></div>
            <div dir="auto"><i>            band =
                gdal_ds.GetRasterBand(band_idx) </i></div>
            <div dir="auto"><i>            band.SetNoDataValue(no_data)</i></div>
            <div dir="auto"><i>           
                band.SetDescription(time_step)</i></div>
            <div dir="auto"><i>            data = var[band_idx - 1]</i></div>
            <div dir="auto"><i>            data = ma.getdata(data)</i></div>
            <div dir="auto"><i>           
                band.WriteArray(data.astype(np_dtype, copy=False))</i></div>
            <div dir="auto"><i>       gdal_ds.FlushCache()</i></div>
            <div dir="auto">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:  </div>
            <div dir="auto"><i>        gdal_ds = driver.Create(out_path,
                cols, rows, times, gdal.GDT_UInt16) </i></div>
            <div dir="auto"><i>       
                gdal_ds.SetProjection("my_projection")</i></div>
            <div dir="auto"><i>       
                gdal_ds.SetGeoTransform("my_geo_transform")</i></div>
            <div dir="auto"><i>        for band_idx in range(1, times +
                1):</i></div>
            <div dir="auto"><i>            band =
                gdal_ds.GetRasterBand(band_idx) </i></div>
            <div dir="auto"><i>            band.SetNoDataValue(no_data)</i></div>
            <div dir="auto"><i>           
                band.SetDescription(time_step)</i></div>
            <div dir="auto"><i>            data = var[band_idx - 1]</i></div>
            <div dir="auto"><i>            data = ma.getdata(data)</i></div>
            <div dir="auto"><i>            data_scaled = 
                np.interp(data, (data.min(), data.max()), (0, 255))</i></div>
            <div dir="auto"><i>            data_scaled2 =
                data_scaled.astype(int)</i></div>
            <div dir="auto"><i>            colors = gdal.ColorTable()</i></div>
            <div dir="auto"><i>            colors.CreateColorRamp(0,
                (229, 245, 249), 127, (153, 216, 201))</i></div>
            <div dir="auto"><i>            colors.CreateColorRamp(127,
                (153, 216, 201), 254, (44, 162, 95))</i></div>
            <div dir="auto"><i>           
                band.SetRasterColorTable(colors)</i></div>
            <div dir="auto"><i>           
                band.SetRasterColorInterpretation(gdal.GCI_PaletteIndex)</i></div>
            <div dir="auto"><i>            band.WriteArray(data_scaled2)</i></div>
            <div dir="auto"><i>       gdal_ds.FlushCache()</i></div>
            <div dir="auto">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.</div>
            <div dir="auto">Thank you in advance.</div>
          </div>
          <div dir="auto"><br>
          </div>
        </div>
      </div>
      <br>
      <fieldset class="moz-mime-attachment-header"></fieldset>
      <pre class="moz-quote-pre" wrap="">_______________________________________________
gdal-dev mailing list
<a class="moz-txt-link-abbreviated" href="mailto:gdal-dev@lists.osgeo.org">gdal-dev@lists.osgeo.org</a>
<a class="moz-txt-link-freetext" href="https://lists.osgeo.org/mailman/listinfo/gdal-dev">https://lists.osgeo.org/mailman/listinfo/gdal-dev</a>
</pre>
    </blockquote>
    <pre class="moz-signature" cols="72">-- 
<a class="moz-txt-link-freetext" href="http://www.spatialys.com">http://www.spatialys.com</a>
My software is free, but my time generally not.</pre>
  </body>
</html>