Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion alg/gdalwarper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1081,8 +1081,14 @@ const char *GDALWarpGetOptionList(void)
"Numeric value or NO_DATA. This option forces the destination image "
"to be initialized to the indicated value (for all bands) "
"or indicates that it should be initialized to the NO_DATA value in "
"padfDstNoDataReal/padfDstNoDataImag. If this value is not set the "
"padfDstNoDataReal/padfDstNoDataImag. If this value is not set, the "
"destination image will be read and overlaid.'/>"
"<Option name='RESET_DEST_PIXELS' type='boolean' description='"
"Whether the whole destination image must be re-initialized to the "
"destination nodata value of padfDstNoDataReal/padfDstNoDataImag "
"if set, or 0 otherwise. The main difference with INIT_DEST is that "
"it also affects regions not related to the source dataset.' "
"default='NO'/>"
"<Option name='WRITE_FLUSH' type='boolean' description='"
"This option forces a flush to disk of data after "
"each chunk is processed. In some cases this helps ensure a serial "
Expand Down Expand Up @@ -1292,6 +1298,14 @@ const char *GDALWarpGetOptionList(void)
* padfDstNoDataReal/padfDstNoDataImag. If this value isn't set the
* destination image will be read and overlaid.</li>
*
* <li>RESET_DEST_PIXELS=YES/NO (since GDAL 3.13): Defaults to NO.
* Whether the whole destination image must be re-initialized to the
* destination nodata value of padfDstNoDataReal/padfDstNoDataImag if set,
* or 0 otherwise.
* The main difference with INIT_DEST is that it also affects regions
* not related to the source dataset.
* </li>
*
* <li>WRITE_FLUSH=YES/NO: This option forces a flush to disk of data after
* each chunk is processed. In some cases this helps ensure a serial
* writing of the output data otherwise a block of data may be written to disk
Expand Down
15 changes: 15 additions & 0 deletions alg/gdalwarpoperation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -723,6 +723,21 @@ GDALWarpOperation::Initialize(const GDALWarpOptions *psNewOptions,
}
}

if (eErr == CE_None && psOptions->hDstDS &&
CPLTestBool(CSLFetchNameValueDef(psOptions->papszWarpOptions,
"RESET_DEST_PIXELS", "NO")))
{
for (int i = 0; eErr == CE_None && i < psOptions->nBandCount; ++i)
{
eErr = GDALFillRaster(
GDALGetRasterBand(psOptions->hDstDS, psOptions->panDstBands[i]),
psOptions->padfDstNoDataReal ? psOptions->padfDstNoDataReal[i]
: 0.0,
psOptions->padfDstNoDataImag ? psOptions->padfDstNoDataImag[i]
: 0.0);
}
}

return eErr;
}

Expand Down
4 changes: 4 additions & 0 deletions apps/gdalwarp_lib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2792,6 +2792,10 @@ GDALWarpDirect(const char *pszDest, GDALDatasetH hDstDS, int nSrcCount,
}
}

if (iSrc > 0)
psOptions->aosWarpOptions.SetNameValue("RESET_DEST_PIXELS",
nullptr);

/* --------------------------------------------------------------------
*/
/* Create a transformation object from the source to */
Expand Down
31 changes: 31 additions & 0 deletions autotest/utilities/test_gdalwarp_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -4771,3 +4771,34 @@ def test_gdalwarp_lib_mask_band_and_src_nodata():
):
out_ds = gdal.Warp("", src_ds, options="-f MEM -dstnodata 5")
assert out_ds.GetRasterBand(1).ReadRaster() == b"\x05"


###############################################################################
# Test RESET_DEST_PIXELS warping option


@pytest.mark.parametrize("dstNodata", [255, None])
def test_gdalwarp_lib_RESET_DEST_PIXELS(dstNodata):

src_ds = gdal.GetDriverByName("MEM").Create("", 3, 3)
src_ds.SetGeoTransform([0, 1, 0, 0, 0, -1])
src_ds.GetRasterBand(1).Fill(1)

out_ds = gdal.Warp("", src_ds, format="MEM", dstNodata=dstNodata)

assert out_ds.ReadRaster() == b"\x01" * (3 * 3)

src_ds = gdal.GetDriverByName("MEM").Create("", 1, 1)
src_ds.SetGeoTransform([1, 1, 0, -1, 0, -1])
src_ds.GetRasterBand(1).Fill(2)

src2_ds = gdal.GetDriverByName("MEM").Create("", 1, 1)
src2_ds.SetGeoTransform([2, 1, 0, -1, 0, -1])
src2_ds.GetRasterBand(1).Fill(3)

gdal.Warp(out_ds, [src_ds, src2_ds], warpOptions={"RESET_DEST_PIXELS": "YES"})

if dstNodata is None:
assert out_ds.ReadRaster() == b"\x00\x00\x00\x00\x02\x03\x00\x00\x00"
else:
assert out_ds.ReadRaster() == b"\xFF\xFF\xFF\xFF\x02\x03\xFF\xFF\xFF"
Loading