Merge pull request #63282 from qgis/backport-63258-to-release-3_44

[Backport release-3_44] [pg][raster] Fix nodata ignored
This commit is contained in:
Alexander Bruy 2025-09-22 06:39:54 +01:00 committed by GitHub
commit ac0491d58f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 30 additions and 0 deletions

View File

@ -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.

View File

@ -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<QgsRasterBlock> dstBlock = std::make_unique<QgsRasterBlock>( 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"