Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Backport release-3_40] [raster calculator] Fix output CRS not taken from first raster entry, safeguard from crash #60253

Merged
merged 3 commits into from
Jan 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/analysis/raster/qgsrastercalculator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<QgsRasterCalculatorEntry> &rasterEntries, const QgsCoordinateTransformContext &transformContext )
Expand All @@ -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();
}

Expand Down
25 changes: 25 additions & 0 deletions tests/src/analysis/testqgsrastercalculator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ class TestQgsRasterCalculator : public QgsTest
void findNodes();

void testRasterEntries();
void testOutputCrsFromRasterEntries();
void calcFormulasWithReprojectedLayers();

void testStatistics();
Expand Down Expand Up @@ -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<QgsRasterCalculatorEntry> 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<int>( 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;
Expand Down
Loading