Skip to content

Commit 0d88746

Browse files
committed
gdalwarp/gdal raster reproject: add a RESET_DEST_PIXELS=YES/NO warping option to reset the existing output dataset completely to dstnodata/0
1 parent 8c3cf3d commit 0d88746

File tree

4 files changed

+61
-1
lines changed

4 files changed

+61
-1
lines changed

alg/gdalwarper.cpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1081,8 +1081,14 @@ const char *GDALWarpGetOptionList(void)
10811081
"Numeric value or NO_DATA. This option forces the destination image "
10821082
"to be initialized to the indicated value (for all bands) "
10831083
"or indicates that it should be initialized to the NO_DATA value in "
1084-
"padfDstNoDataReal/padfDstNoDataImag. If this value is not set the "
1084+
"padfDstNoDataReal/padfDstNoDataImag. If this value is not set, the "
10851085
"destination image will be read and overlaid.'/>"
1086+
"<Option name='RESET_DEST_PIXELS' type='boolean' description='"
1087+
"Whether the whole destination image must be re-initialized to the "
1088+
"destination nodata value of padfDstNoDataReal/padfDstNoDataImag "
1089+
"if set, or 0 otherwise. The main difference with INIT_DEST is that "
1090+
"it also affects regions not related to the source dataset.' "
1091+
"default='NO'/>"
10861092
"<Option name='WRITE_FLUSH' type='boolean' description='"
10871093
"This option forces a flush to disk of data after "
10881094
"each chunk is processed. In some cases this helps ensure a serial "
@@ -1292,6 +1298,14 @@ const char *GDALWarpGetOptionList(void)
12921298
* padfDstNoDataReal/padfDstNoDataImag. If this value isn't set the
12931299
* destination image will be read and overlaid.</li>
12941300
*
1301+
* <li>RESET_DEST_PIXELS=YES/NO (since GDAL 3.13): Defaults to NO.
1302+
* Whether the whole destination image must be re-initialized to the
1303+
* destination nodata value of padfDstNoDataReal/padfDstNoDataImag if set,
1304+
* or 0 otherwise.
1305+
* The main difference with INIT_DEST is that it also affects regions
1306+
* not related to the source dataset.
1307+
* </li>
1308+
*
12951309
* <li>WRITE_FLUSH=YES/NO: This option forces a flush to disk of data after
12961310
* each chunk is processed. In some cases this helps ensure a serial
12971311
* writing of the output data otherwise a block of data may be written to disk

alg/gdalwarpoperation.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -723,6 +723,21 @@ GDALWarpOperation::Initialize(const GDALWarpOptions *psNewOptions,
723723
}
724724
}
725725

726+
if (eErr == CE_None && psOptions->hDstDS &&
727+
CPLTestBool(CSLFetchNameValueDef(psOptions->papszWarpOptions,
728+
"RESET_DEST_PIXELS", "NO")))
729+
{
730+
for (int i = 0; eErr == CE_None && i < psOptions->nBandCount; ++i)
731+
{
732+
eErr = GDALFillRaster(
733+
GDALGetRasterBand(psOptions->hDstDS, psOptions->panDstBands[i]),
734+
psOptions->padfDstNoDataReal ? psOptions->padfDstNoDataReal[i]
735+
: 0.0,
736+
psOptions->padfDstNoDataImag ? psOptions->padfDstNoDataImag[i]
737+
: 0.0);
738+
}
739+
}
740+
726741
return eErr;
727742
}
728743

apps/gdalwarp_lib.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2792,6 +2792,10 @@ GDALWarpDirect(const char *pszDest, GDALDatasetH hDstDS, int nSrcCount,
27922792
}
27932793
}
27942794

2795+
if (iSrc > 0)
2796+
psOptions->aosWarpOptions.SetNameValue("RESET_DEST_PIXELS",
2797+
nullptr);
2798+
27952799
/* --------------------------------------------------------------------
27962800
*/
27972801
/* Create a transformation object from the source to */

autotest/utilities/test_gdalwarp_lib.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4771,3 +4771,30 @@ def test_gdalwarp_lib_mask_band_and_src_nodata():
47714771
):
47724772
out_ds = gdal.Warp("", src_ds, options="-f MEM -dstnodata 5")
47734773
assert out_ds.GetRasterBand(1).ReadRaster() == b"\x05"
4774+
4775+
4776+
###############################################################################
4777+
# Test RESET_DEST_PIXELS warping option
4778+
4779+
4780+
def test_gdalwarp_lib_RESET_DEST_PIXELS():
4781+
4782+
src_ds = gdal.GetDriverByName("MEM").Create("", 3, 3)
4783+
src_ds.SetGeoTransform([0, 1, 0, 0, 0, -1])
4784+
src_ds.GetRasterBand(1).Fill(1)
4785+
4786+
out_ds = gdal.Warp("", src_ds, format="MEM", dstNodata=0)
4787+
4788+
assert out_ds.ReadRaster() == b"\x01" * (3 * 3)
4789+
4790+
src_ds = gdal.GetDriverByName("MEM").Create("", 1, 1)
4791+
src_ds.SetGeoTransform([1, 1, 0, -1, 0, -1])
4792+
src_ds.GetRasterBand(1).Fill(2)
4793+
4794+
src2_ds = gdal.GetDriverByName("MEM").Create("", 1, 1)
4795+
src2_ds.SetGeoTransform([2, 1, 0, -1, 0, -1])
4796+
src2_ds.GetRasterBand(1).Fill(3)
4797+
4798+
gdal.Warp(out_ds, [src_ds, src2_ds], warpOptions={"RESET_DEST_PIXELS": "YES"})
4799+
4800+
assert out_ds.ReadRaster() == b"\x00\x00\x00\x00\x02\x03\x00\x00\x00"

0 commit comments

Comments
 (0)