Skip to content

Commit

Permalink
Use Even's code suggestions + add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wonder-sk committed Feb 14, 2022
1 parent 49f0059 commit dda5389
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 6 deletions.
15 changes: 9 additions & 6 deletions src/core/providers/gdal/qgsgdalprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2999,20 +2999,23 @@ void QgsGdalProvider::initBaseDataset()
{
QgsLogger::warning( QStringLiteral( "Creating Warped VRT." ) );

// Add alpha band to the output VRT dataset if it does not exist already
// so that pixels in empty regions (e.g. when the raster is rotated) will be transparent
gdal::warp_options_unique_ptr psWarpOptions( GDALCreateWarpOptions() );
if ( GDALGetRasterColorInterpretation( GDALGetRasterBand( mGdalBaseDataset, GDALGetRasterCount( mGdalBaseDataset ) ) ) != GCI_AlphaBand )
{
psWarpOptions->nDstAlphaBand = GDALGetRasterCount( mGdalBaseDataset ) + 1;
}

if ( GDALGetMetadata( mGdalBaseDataset, "RPC" ) )
{
mGdalDataset =
QgsGdalUtils::rpcAwareAutoCreateWarpedVrt( mGdalBaseDataset, nullptr, nullptr,
GRA_NearestNeighbour, 0.2, nullptr );
GRA_NearestNeighbour, 0.2, psWarpOptions.get() );
mGdalTransformerArg = QgsGdalUtils::rpcAwareCreateTransformer( mGdalBaseDataset );
}
else
{
// Add alpha band to the output VRT dataset so that pixels in empty regions
// (e.g. when the raster is rotated) will be transparent
gdal::warp_options_unique_ptr psWarpOptions( GDALCreateWarpOptions() );
psWarpOptions->nDstAlphaBand = GDALGetRasterCount( mGdalBaseDataset ) + 1;

mGdalDataset =
GDALAutoCreateWarpedVRT( mGdalBaseDataset, nullptr, nullptr,
GRA_NearestNeighbour, 0.2, psWarpOptions.get() );
Expand Down
20 changes: 20 additions & 0 deletions tests/src/core/testqgsrasterlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ class TestQgsRasterLayer : public QObject
void testRefreshRendererIfNeeded();
void sample();
void testTemporalProperties();
void rotatedRaster();


private:
Expand Down Expand Up @@ -1040,5 +1041,24 @@ void TestQgsRasterLayer::testTemporalProperties()
QCOMPARE( temporalProperties->fixedTemporalRange().end(), dateTimeRange.end() );
}

void TestQgsRasterLayer::rotatedRaster()
{
mMapSettings->setExtent( QgsRectangle( 994, 922, 1174, 1102 ) );

std::unique_ptr< QgsRasterLayer> rgb = std::make_unique< QgsRasterLayer >( mTestDataDir + "raster/rotated_rgb.png",
QStringLiteral( "rgb" ) );
QVERIFY( rgb->isValid() );

mMapSettings->setLayers( QList<QgsMapLayer *>() << rgb.get() );
QVERIFY( render( QStringLiteral( "raster_rotated_rgb" ) ) );

std::unique_ptr< QgsRasterLayer> rgba = std::make_unique< QgsRasterLayer >( mTestDataDir + "raster/rotated_rgba.png",
QStringLiteral( "rgba" ) );
QVERIFY( rgba->isValid() );

mMapSettings->setLayers( QList<QgsMapLayer *>() << rgba.get() );
QVERIFY( render( QStringLiteral( "raster_rotated_rgba" ) ) );
}

QGSTEST_MAIN( TestQgsRasterLayer )
#include "testqgsrasterlayer.moc"
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/testdata/raster/rotated_rgb.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions tests/testdata/raster/rotated_rgb.pngw
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
0.72744
0.97092
0.97092
-0.7274
1000
1000
Binary file added tests/testdata/raster/rotated_rgba.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions tests/testdata/raster/rotated_rgba.pngw
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
0.72744
0.97092
0.97092
-0.7274
1000
1000

0 comments on commit dda5389

Please sign in to comment.