@@ -163,6 +163,12 @@ rt_raster rt_raster_gdal_warp(
163163) {
164164 CPLErr cplerr ;
165165 char * dst_options [] = {"SUBCLASS=VRTWarpedDataset" , NULL };
166+ const char * init_dest_nodata_option = "INIT_DEST=NO_DATA" ;
167+ #if POSTGIS_GDAL_VERSION >= 30302
168+ const char * unified_src_nodata_option = "UNIFIED_SRC_NODATA=PARTIAL" ;
169+ #else
170+ const char * unified_src_nodata_option = "UNIFIED_SRC_NODATA=NO" ;
171+ #endif
166172 _rti_warp_arg arg = NULL ;
167173
168174 int nodata_count = 0 ;
@@ -197,6 +203,7 @@ rt_raster rt_raster_gdal_warp(
197203 int numBands = 0 ;
198204 int nodata_pos = 0 ;
199205 int data_pos = 0 ;
206+ int use_init_dest_nodata = 0 ;
200207
201208 int subspatial = 0 ;
202209
@@ -865,17 +872,21 @@ rt_raster rt_raster_gdal_warp(
865872 arg -> wopts -> hDstDS = arg -> dst .ds ;
866873 arg -> wopts -> pfnTransformer = arg -> transform .func ;
867874 arg -> wopts -> pTransformerArg = arg -> transform .arg .transform ;
868- arg -> wopts -> papszWarpOptions = (char * * ) CPLMalloc (sizeof (char * ) * 3 );
869- arg -> wopts -> papszWarpOptions [0 ] = (char * ) CPLMalloc (sizeof (char ) * (strlen ("INIT_DEST=NO_DATA" ) + 1 ));
870- strcpy (arg -> wopts -> papszWarpOptions [0 ], "INIT_DEST=NO_DATA" );
871- #if POSTGIS_GDAL_VERSION >= 30302
872- arg -> wopts -> papszWarpOptions [1 ] = (char * ) CPLMalloc (sizeof (char ) * (strlen ("UNIFIED_SRC_NODATA=PARTIAL" ) + 1 ));
873- strcpy (arg -> wopts -> papszWarpOptions [1 ], "UNIFIED_SRC_NODATA=PARTIAL" );
874- #else
875- arg -> wopts -> papszWarpOptions [1 ] = (char * ) CPLMalloc (sizeof (char ) * (strlen ("UNIFIED_SRC_NODATA=NO" ) + 1 ));
876- strcpy (arg -> wopts -> papszWarpOptions [1 ], "UNIFIED_SRC_NODATA=NO" );
877- #endif
878- arg -> wopts -> papszWarpOptions [2 ] = NULL ;
875+ /*
876+ * GDAL 3.11 warns, and later treats as an error, when INIT_DEST=NO_DATA
877+ * is requested without destination nodata values for every band.
878+ */
879+ use_init_dest_nodata = numBands > 0 && nodata_count == numBands ;
880+ arg -> wopts -> papszWarpOptions =
881+ (char * * ) CPLMalloc (sizeof (char * ) * (use_init_dest_nodata ? 3 : 2 ));
882+ i = 0 ;
883+ if (use_init_dest_nodata ) {
884+ arg -> wopts -> papszWarpOptions [i ] = (char * ) CPLMalloc (sizeof (char ) * (strlen (init_dest_nodata_option ) + 1 ));
885+ strcpy (arg -> wopts -> papszWarpOptions [i ++ ], init_dest_nodata_option );
886+ }
887+ arg -> wopts -> papszWarpOptions [i ] = (char * ) CPLMalloc (sizeof (char ) * (strlen (unified_src_nodata_option ) + 1 ));
888+ strcpy (arg -> wopts -> papszWarpOptions [i ++ ], unified_src_nodata_option );
889+ arg -> wopts -> papszWarpOptions [i ] = NULL ;
879890
880891 /* band mapping */
881892 arg -> wopts -> nBandCount = numBands ;
0 commit comments