[gdal-dev] Problem with warping into existing TIF
Even Rouault
even.rouault at spatialys.com
Thu Feb 13 09:28:43 PST 2025
Tim,
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
Even
Le 12/02/2025 à 23:47, Tim Harris via gdal-dev a écrit :
> 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.
>
> 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.
>
> ---
> import numpy as np
> from osgeo import gdal
>
> gdal.UseExceptions()
> gdal.SetConfigOption("GDAL_TIFF_INTERNAL_MASK", "YES")
> drv = gdal.GetDriverByName("GTiff")
>
> zeros = np.zeros((3, 1024, 1024))
>
> red = np.copy(zeros)
> red[0, 100:200, 100:200] = 255
>
> green = np.copy(zeros)
> green[1, 100:200, 800:900] = 255
>
> ds = drv.Create("red.tif", 1024, 1024, 3, gdal.GDT_Byte)
> ds.SetGeoTransform((0, 1, 0, 0, 0, -1))
> ds.WriteArray(red)
> ds.CreateMaskBand(gdal.GMF_PER_DATASET)
> ds.GetRasterBand(1).GetMaskBand().WriteArray(red[0, :, :])
> ds = None
>
> ds = drv.Create("green.tif", 1024, 1024, 3, gdal.GDT_Byte)
> ds.SetGeoTransform((0, 1, 0, 0, 0, -1))
> ds.WriteArray(green)
> ds.CreateMaskBand(gdal.GMF_PER_DATASET)
> ds.GetRasterBand(1).GetMaskBand().WriteArray(green[1, :, :])
> ds = None
> ---
>
> Then if you copy red.tif to warp.tif, then warp green.tif into warp.tif:
> cp red.tif warp.tif
> gdalwarp green.tif warp.tif
>
> 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.
>
> 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.
>
> Thanks
>
> _______________________________________________
> gdal-dev mailing list
> gdal-dev at lists.osgeo.org
> https://lists.osgeo.org/mailman/listinfo/gdal-dev
--
http://www.spatialys.com
My software is free, but my time generally not.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/gdal-dev/attachments/20250213/613aeddf/attachment.htm>
More information about the gdal-dev
mailing list