[gdal-dev] Problem with warping into existing TIF
Tim Harris
trharris78 at gmail.com
Wed Feb 12 14:47:43 PST 2025
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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/gdal-dev/attachments/20250212/dc22fa87/attachment.htm>
More information about the gdal-dev
mailing list