From 5b3f5b9a9c781cc35b0e6ab90895e5495b59e0f2 Mon Sep 17 00:00:00 2001 From: Alessandro Pasotti Date: Thu, 18 Sep 2025 18:21:32 +0200 Subject: [PATCH 1/2] [pg][raster] Fix nodata ignored Fix #63171 TODO: test --- src/core/qgsgdalutils.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/core/qgsgdalutils.cpp b/src/core/qgsgdalutils.cpp index 5441fe688cd..419a7d9ae0a 100644 --- a/src/core/qgsgdalutils.cpp +++ b/src/core/qgsgdalutils.cpp @@ -386,6 +386,8 @@ static bool resampleSingleBandRasterStatic( GDALDatasetH hSrcDS, GDALDatasetH hD double noDataValue = GDALGetRasterNoDataValue( GDALGetRasterBand( hDstDS, 1 ), nullptr ); psWarpOptions->padfDstNoDataReal = reinterpret_cast< double * >( CPLMalloc( sizeof( double ) * 1 ) ); psWarpOptions->padfDstNoDataReal[0] = noDataValue; + psWarpOptions->padfSrcNoDataReal = reinterpret_cast< double * >( CPLMalloc( sizeof( double ) * 1 ) ); + psWarpOptions->padfSrcNoDataReal[0] = noDataValue; psWarpOptions->eResampleAlg = resampleAlg; // Establish reprojection transformer. From 9ba381bc0325154c33d8eae725ec2c989262d094 Mon Sep 17 00:00:00 2001 From: Alessandro Pasotti Date: Fri, 19 Sep 2025 09:06:25 +0200 Subject: [PATCH 2/2] Tests for issue #63171 --- tests/src/core/testqgsgdalutils.cpp | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/tests/src/core/testqgsgdalutils.cpp b/tests/src/core/testqgsgdalutils.cpp index b008214855e..2d3a4a9616b 100644 --- a/tests/src/core/testqgsgdalutils.cpp +++ b/tests/src/core/testqgsgdalutils.cpp @@ -44,6 +44,7 @@ class TestQgsGdalUtils : public QObject void testPathIsCheapToOpen(); void testVrtMatchesLayerType(); void testMultilayerExtensions(); + void testResampleSingleBandRasterNoData(); private: double identify( GDALDatasetH dataset, int band, int px, int py ); @@ -322,5 +323,32 @@ double TestQgsGdalUtils::identify( GDALDatasetH dataset, int band, int px, int p return value; } +void TestQgsGdalUtils::testResampleSingleBandRasterNoData() +{ + // Create 2x2 integer raster with values 1, 65535, 65535, 65535 and nodata=65535 + QTemporaryDir tempDir; + const QString tempPath = tempDir.path(); + const QString inputFilename = tempPath + "/input.tif"; + + gdal::dataset_unique_ptr ds1 = QgsGdalUtils::createSingleBandTiffDataset( inputFilename, GDT_UInt16, QgsRectangle( 1, 1, 3, 3 ), 2, 2, QgsCoordinateReferenceSystem( "EPSG:4326" ) ); + QVERIFY( ds1 ); + + GDALRasterBandH band = GDALGetRasterBand( ds1.get(), 1 ); + GDALSetRasterNoDataValue( band, 65535 ); + GUInt16 buffer[4] = { 1, 65535, 65535, 65535 }; + const CPLErr err = GDALRasterIO( band, GF_Write, 0, 0, 2, 2, buffer, 2, 2, GDT_UInt16, 0, 0 ); + QCOMPARE( err, CE_None ); + + // Create destination GDAL DS + std::unique_ptr dstBlock = std::make_unique( Qgis::DataType::UInt16, 4, 4 ); + dstBlock->setNoDataValue( 65535.0 ); + gdal::dataset_unique_ptr gdalDsOutput = QgsGdalUtils::blockToSingleBandMemoryDataset( QgsRectangle( 1, 1, 3, 3 ), dstBlock.get() ); + + QVERIFY( QgsGdalUtils::resampleSingleBandRaster( ds1.get(), gdalDsOutput.get(), GRA_NearestNeighbour, nullptr ) ); + QCOMPARE( identify( gdalDsOutput.get(), 1, 0, 0 ), 1.0 ); + QCOMPARE( identify( gdalDsOutput.get(), 1, 2, 2 ), 65535.0 ); +} + + QGSTEST_MAIN( TestQgsGdalUtils ) #include "testqgsgdalutils.moc"