From 64c6c217002a9c61b74e610164fdc58f9234370e Mon Sep 17 00:00:00 2001 From: Mathieu Pellerin Date: Wed, 22 Jan 2025 17:37:57 +0700 Subject: [PATCH 1/3] [raster calculator] Fix output CRS not taken from first raster entry, safeguard from crash (fixes #59898) --- src/analysis/raster/qgsrastercalculator.cpp | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/analysis/raster/qgsrastercalculator.cpp b/src/analysis/raster/qgsrastercalculator.cpp index 0769c82e7695..1203d3608574 100644 --- a/src/analysis/raster/qgsrastercalculator.cpp +++ b/src/analysis/raster/qgsrastercalculator.cpp @@ -79,6 +79,11 @@ QgsRasterCalculator::QgsRasterCalculator( const QString &formulaString, const QS , mRasterEntries( rasterEntries ) , mTransformContext( transformContext ) { + //default to first layer's crs + if ( !mRasterEntries.isEmpty() ) + { + mOutputCrs = mRasterEntries.at( 0 ).raster->crs(); + } } QgsRasterCalculator::QgsRasterCalculator( const QString &formulaString, const QString &outputFile, const QString &outputFormat, const QgsRectangle &outputExtent, const QgsCoordinateReferenceSystem &outputCrs, int nOutputColumns, int nOutputRows, const QVector &rasterEntries, const QgsCoordinateTransformContext &transformContext ) @@ -105,7 +110,10 @@ QgsRasterCalculator::QgsRasterCalculator( const QString &formulaString, const QS , mRasterEntries( rasterEntries ) { //default to first layer's crs - mOutputCrs = mRasterEntries.at( 0 ).raster->crs(); + if ( !mRasterEntries.isEmpty() ) + { + mOutputCrs = mRasterEntries.at( 0 ).raster->crs(); + } mTransformContext = QgsProject::instance()->transformContext(); } From a9ff4ab3880b227e1d9b0caf9dcc9adb1564deb1 Mon Sep 17 00:00:00 2001 From: Mathieu Pellerin Date: Wed, 22 Jan 2025 17:58:10 +0700 Subject: [PATCH 2/3] Add test coverage --- .../src/analysis/testqgsrastercalculator.cpp | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/tests/src/analysis/testqgsrastercalculator.cpp b/tests/src/analysis/testqgsrastercalculator.cpp index 6cfc959da153..244adeff1b76 100644 --- a/tests/src/analysis/testqgsrastercalculator.cpp +++ b/tests/src/analysis/testqgsrastercalculator.cpp @@ -67,6 +67,7 @@ class TestQgsRasterCalculator : public QgsTest void findNodes(); void testRasterEntries(); + void testOutputCrsFromRasterEntries(); void calcFormulasWithReprojectedLayers(); void testStatistics(); @@ -717,6 +718,30 @@ void TestQgsRasterCalculator::toString() QVERIFY( calcNode == nullptr ); } +void TestQgsRasterCalculator::testOutputCrsFromRasterEntries() +{ + QgsRasterCalculatorEntry entry1; + entry1.bandNumber = 1; + entry1.raster = mpLandsatRasterLayer; + entry1.ref = QStringLiteral( "landsat@0" ); + + QVector entries; + entries << entry1; + + QgsRectangle extent( 783235, 3348110, 783350, 3347960 ); + + QTemporaryFile tmpFile; + tmpFile.open(); // fileName is not available until open + QString tmpName = tmpFile.fileName(); + tmpFile.close(); + + QgsRasterCalculator rc( QStringLiteral( "\"landsat@0\"" ), tmpName, QStringLiteral( "GTiff" ), extent, 2, 3, entries, QgsProject::instance()->transformContext() ); + QCOMPARE( static_cast( rc.processCalculation() ), 0 ); + //open output file and check results + QgsRasterLayer *result = new QgsRasterLayer( tmpName, QStringLiteral( "result" ) ); + QCOMPARE( result->crs(), mpLandsatRasterLayer->crs() ); +} + void TestQgsRasterCalculator::calcFormulasWithReprojectedLayers() { QgsRasterCalculatorEntry entry1; From d68a404b39fe1aa020532d0f41b1d7b3ff4503fc Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 22 Jan 2025 11:04:25 +0000 Subject: [PATCH 3/3] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- tests/src/analysis/testqgsrastercalculator.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/src/analysis/testqgsrastercalculator.cpp b/tests/src/analysis/testqgsrastercalculator.cpp index 244adeff1b76..1dee37ea0922 100644 --- a/tests/src/analysis/testqgsrastercalculator.cpp +++ b/tests/src/analysis/testqgsrastercalculator.cpp @@ -727,14 +727,14 @@ void TestQgsRasterCalculator::testOutputCrsFromRasterEntries() QVector entries; entries << entry1; - + QgsRectangle extent( 783235, 3348110, 783350, 3347960 ); - + QTemporaryFile tmpFile; tmpFile.open(); // fileName is not available until open QString tmpName = tmpFile.fileName(); tmpFile.close(); - + QgsRasterCalculator rc( QStringLiteral( "\"landsat@0\"" ), tmpName, QStringLiteral( "GTiff" ), extent, 2, 3, entries, QgsProject::instance()->transformContext() ); QCOMPARE( static_cast( rc.processCalculation() ), 0 ); //open output file and check results