<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  </head>
  <body>
    <p>Tim,</p>
    <p>Reviewing the warping logic, I see we have support for a *input*
      mask, but not creating/updating an *output* mask. AFAICS, there
      isn't any particular strong reason not to have support for that.
      Likely just a few hours of coding away. Your best workaround is to
      generate an output with an alpha band (possibly as a VRT) and use
      gdal.Translate(dst, src, options="-b 1 -b 2 -b 3 -mask 4") for
      example to transform a RGBA into a RGB+mask product</p>
    <p>Even<br>
    </p>
    <div class="moz-cite-prefix">Le 12/02/2025 à 23:47, Tim Harris via
      gdal-dev a écrit :<br>
    </div>
    <blockquote type="cite"
cite="mid:CAK3fJgUSvG5qdTZQFDoS==CMKpkp3C8sF81KKKFod=wMiBuWZQ@mail.gmail.com">
      <meta http-equiv="content-type" content="text/html; charset=UTF-8">
      <div dir="ltr">
        <div>Not sure if this is a bug, or expected behavior, or user
          error. If I warp one TIF into another, and both have nodata
          masks, it seems that gdalwarp isn't updating the destination
          TIF's nodata mask to unmask the new pixels.</div>
        <div><br>
        </div>
        <div>Here's a short script to generate two example TIFs. One has
          a red box in its upper left corner, the other has a green box
          in its upper right corner. Both have nodata masks so that only
          the colored boxes are visible.</div>
        <div><br>
        </div>
        <div><span style="font-family:monospace">---</span></div>
        <div><span style="font-family:monospace">import numpy as np<br>
            from osgeo import gdal<br>
            <br>
            gdal.UseExceptions()<br>
            gdal.SetConfigOption("GDAL_TIFF_INTERNAL_MASK", "YES")<br>
            drv = gdal.GetDriverByName("GTiff")<br>
            <br>
            zeros = np.zeros((3, 1024, 1024))<br>
            <br>
            red = np.copy(zeros)<br>
            red[0, 100:200, 100:200] = 255<br>
            <br>
            green = np.copy(zeros)<br>
            green[1, 100:200, 800:900] = 255<br>
            <br>
            ds = drv.Create("red.tif", 1024, 1024, 3, gdal.GDT_Byte)<br>
            ds.SetGeoTransform((0, 1, 0, 0, 0, -1))<br>
            ds.WriteArray(red)<br>
            ds.CreateMaskBand(gdal.GMF_PER_DATASET)<br>
            ds.GetRasterBand(1).GetMaskBand().WriteArray(red[0, :, :])<br>
            ds = None<br>
            <br>
            ds = drv.Create("green.tif", 1024, 1024, 3, gdal.GDT_Byte)<br>
            ds.SetGeoTransform((0, 1, 0, 0, 0, -1))<br>
            ds.WriteArray(green)<br>
            ds.CreateMaskBand(gdal.GMF_PER_DATASET)<br>
            ds.GetRasterBand(1).GetMaskBand().WriteArray(green[1, :, :])<br>
            ds = None</span></div>
        <div><span style="font-family:monospace">---</span></div>
        <div><span style="font-family:monospace"><br>
          </span></div>
        <div>Then if you copy red.tif to warp.tif, then warp green.tif
          into warp.tif:</div>
        <div><span style="font-family:monospace">cp red.tif warp.tif</span></div>
        <div><span style="font-family:monospace">gdalwarp green.tif
            warp.tif</span></div>
        <div><br>
        </div>
        <div>I would expect the result to be a combined image with both
          the red and green boxes. But when I load this result into
          QGIS, I only see the red box. If I change the layer in QGIS to
          ignore the mask band, I do see both boxes (and the remaining
          image is black, as expected). So the pixel data is there, but
          the mask didn't get updated.</div>
        <div><br>
        </div>
        <div>Is there a way to get gdalwarp to update the nodata mask
          automatically? Maybe some hidden setting I'm missing? I can
          work around this by separately warping the masks, then copying
          the result into the final TIF's mask band. But it would be
          nice if the warp took care of this for me.</div>
        <div><br>
        </div>
        <div>Thanks</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>