[tests] add native test for Create constant raster algorithm

This commit is contained in:
root676 2020-04-12 15:32:15 +02:00 committed by Nyall Dawson
parent 8b1a94f827
commit cd610f9bb7
8 changed files with 453 additions and 0 deletions

View File

@ -83,6 +83,9 @@ class TestQgsProcessingAlgs: public QObject
void polygonsToLines_data();
void polygonsToLines();
void createConstantRaster_data();
void createConstantRaster();
void densifyGeometries_data();
void densifyGeometries();
@ -850,6 +853,456 @@ void TestQgsProcessingAlgs::polygonsToLines()
QVERIFY2( result.geometry().equals( expectedGeometry ), QStringLiteral( "Result: %1, Expected: %2" ).arg( result.geometry().asWkt(), expectedGeometry.asWkt() ).toUtf8().constData() );
}
Q_DECLARE_METATYPE( Qgis::DataType )
void TestQgsProcessingAlgs::createConstantRaster_data()
{
QTest::addColumn<QString>( "inputExtent" );
QTest::addColumn<QString>( "expectedRaster" );
QTest::addColumn<Qgis::DataType>( "expectedDataType" );
QTest::addColumn<QString>( "crs" );
QTest::addColumn<double>( "pixelSize" );
QTest::addColumn<double>( "constantValue" );
QTest::addColumn<int>( "typeId" );
/*
* Testcase 1
*
* inputExtent = from "/raster/band1_int16_noct_epsg4326.tif"
* crs = EPSG:4326
* pixelSize = 1.0
* constantValue = 12
* Byte Raster Layer
*
*/
QTest::newRow( "testcase 1" )
<< "-3.000000000,7.000000000,-4.000000000,6.000000000 [EPSG:4326]"
<< QStringLiteral( "/createConstantRaster_testcase1.tif" )
<< Qgis::Byte
<< "EPSG:4326"
<< 1.0
<< 12.0
<< 0;
/*
* Testcase 2
*
* inputExtent = from "/raster/band1_int16_noct_epsg4326.tif"
* crs = EPSG:4326
* pixelSize = 1.0
* constantValue = -1
* Byte Raster Layer
*
*/
QTest::newRow( "testcase 2" )
<< "-3.000000000,7.000000000,-4.000000000,6.000000000 [EPSG:4326]"
<< QStringLiteral( "" )
<< Qgis::Byte
<< "EPSG:4326"
<< 1.0
<< -1.0 //fails --> value too small for byte
<< 0;
/*
* Testcase 3
*
* inputExtent = from "/raster/band1_int16_noct_epsg4326.tif"
* crs = EPSG:4326
* pixelSize = 1.0
* constantValue = -1
* Byte Raster Layer
*
*/
QTest::newRow( "testcase 3" )
<< "-3.000000000,7.000000000,-4.000000000,6.000000000 [EPSG:4326]"
<< QStringLiteral( "" )
<< Qgis::Byte
<< "EPSG:4326"
<< 1.0
<< 256.0 //fails --> value too big for byte
<< 0;
/*
* Testcase 4
*
* inputExtent = from "/raster/band1_int16_noct_epsg4326.tif"
* crs = EPSG:4326
* pixelSize = 1.0
* constantValue = 12
* Int16 Raster Layer
*
*/
QTest::newRow( "testcase 4" )
<< "-3.000000000,7.000000000,-4.000000000,6.000000000 [EPSG:4326]"
<< QStringLiteral( "/createConstantRaster_testcase4.tif" )
<< Qgis::Int16
<< "EPSG:4326"
<< 1.0
<< 12.0
<< 1;
/*
* Testcase 5
*
* inputExtent = from "/raster/band1_int16_noct_epsg4326.tif"
* crs = EPSG:4326
* pixelSize = 1.0
* constantValue = 12
* Int16 Raster Layer
*
*/
QTest::newRow( "testcase 5" )
<< "-3.000000000,7.000000000,-4.000000000,6.000000000 [EPSG:4326]"
<< QStringLiteral( "" )
<< Qgis::Int16
<< "EPSG:4326"
<< 1.0
<< -32769.0
<< 1;
/*
* Testcase 6
*
* inputExtent = from "/raster/band1_int16_noct_epsg4326.tif"
* crs = EPSG:4326
* pixelSize = 1.0
* constantValue = 12
* Int16 Raster Layer
*
*/
QTest::newRow( "testcase 6" )
<< "-3.000000000,7.000000000,-4.000000000,6.000000000 [EPSG:4326]"
<< QStringLiteral( "" )
<< Qgis::Int16
<< "EPSG:4326"
<< 1.0
<< 32769.0
<< 1;
/*
* Testcase 7
*
* inputExtent = from "/raster/band1_int16_noct_epsg4326.tif"
* crs = EPSG:4326
* pixelSize = 1.0
* constantValue = 12
* UInt16 Raster Layer
*
*/
QTest::newRow( "testcase 7" )
<< "-3.000000000,7.000000000,-4.000000000,6.000000000 [EPSG:4326]"
<< QStringLiteral( "/createConstantRaster_testcase7.tif" )
<< Qgis::UInt16
<< "EPSG:4326"
<< 1.0
<< 12.0
<< 2;
/*
* Testcase 8
*
* inputExtent = from "/raster/band1_int16_noct_epsg4326.tif"
* crs = EPSG:4326
* pixelSize = 1.0
* constantValue = 12
* UInt16 Raster Layer
*
*/
QTest::newRow( "testcase 8" )
<< "-3.000000000,7.000000000,-4.000000000,6.000000000 [EPSG:4326]"
<< QStringLiteral( "" )
<< Qgis::UInt16
<< "EPSG:4326"
<< 1.0
<< -1.0
<< 2;
/*
* Testcase 9
*
* inputExtent = from "/raster/band1_int16_noct_epsg4326.tif"
* crs = EPSG:4326
* pixelSize = 1.0
* constantValue = 12
* UInt16 Raster Layer
*
*/
QTest::newRow( "testcase 9" )
<< "-3.000000000,7.000000000,-4.000000000,6.000000000 [EPSG:4326]"
<< QStringLiteral( "" )
<< Qgis::UInt16
<< "EPSG:4326"
<< 1.0
<< 65536.0
<< 2;
/*
* Testcase 10
*
* inputExtent = from "/raster/band1_int16_noct_epsg4326.tif"
* crs = EPSG:4326
* pixelSize = 1.0
* constantValue = 12
* UInt16 Raster Layer
*
*/
QTest::newRow( "testcase 10" )
<< "-3.000000000,7.000000000,-4.000000000,6.000000000 [EPSG:4326]"
<< QStringLiteral( "/createConstantRaster_testcase10.tif" )
<< Qgis::Int32
<< "EPSG:4326"
<< 1.0
<< 12.0
<< 3;
/*
* Testcase 10
*
* inputExtent = from "/raster/band1_int16_noct_epsg4326.tif"
* crs = EPSG:4326
* pixelSize = 1.0
* constantValue = 12
* Int32 Raster Layer
*
*/
QTest::newRow( "testcase 10" )
<< "-3.000000000,7.000000000,-4.000000000,6.000000000 [EPSG:4326]"
<< QStringLiteral( "/createConstantRaster_testcase10.tif" )
<< Qgis::Int32
<< "EPSG:4326"
<< 1.0
<< 12.0
<< 3;
/*
* Testcase 11
*
* inputExtent = from "/raster/band1_int16_noct_epsg4326.tif"
* crs = EPSG:4326
* pixelSize = 1.0
* constantValue = -2147483649.0
* Int32 Raster Layer
*
*/
QTest::newRow( "testcase 11" )
<< "-3.000000000,7.000000000,-4.000000000,6.000000000 [EPSG:4326]"
<< QStringLiteral( "" )
<< Qgis::Int32
<< "EPSG:4326"
<< 1.0
<< -2147483649.0
<< 3;
/*
* Testcase 12
*
* inputExtent = from "/raster/band1_int16_noct_epsg4326.tif"
* crs = EPSG:4326
* pixelSize = 1.0
* constantValue = 2147483649.0
* Int32 Raster Layer
*
*/
QTest::newRow( "testcase 12" )
<< "-3.000000000,7.000000000,-4.000000000,6.000000000 [EPSG:4326]"
<< QStringLiteral( "" )
<< Qgis::Int32
<< "EPSG:4326"
<< 1.0
<< 2147483649.0
<< 3;
/*
* Testcase 13
*
* inputExtent = from "/raster/band1_int16_noct_epsg4326.tif"
* crs = EPSG:4326
* pixelSize = 1.0
* constantValue = 12.0
* UInt32 Raster Layer
*
*/
QTest::newRow( "testcase 13" )
<< "-3.000000000,7.000000000,-4.000000000,6.000000000 [EPSG:4326]"
<< QStringLiteral( "/createConstantRaster_testcase13.tif" )
<< Qgis::UInt32
<< "EPSG:4326"
<< 1.0
<< 12.0
<< 4;
/*
* Testcase 14
*
* inputExtent = from "/raster/band1_int16_noct_epsg4326.tif"
* crs = EPSG:4326
* pixelSize = 1.0
* constantValue = 4294967296.0
* UInt32 Raster Layer
*
*/
QTest::newRow( "testcase 14" )
<< "-3.000000000,7.000000000,-4.000000000,6.000000000 [EPSG:4326]"
<< QStringLiteral( "" )
<< Qgis::UInt32
<< "EPSG:4326"
<< 1.0
<< 4294967296.0
<< 4;
/*
* Testcase 15
*
* inputExtent = from "/raster/band1_int16_noct_epsg4326.tif"
* crs = EPSG:4326
* pixelSize = 1.0
* constantValue = -1.0
* UInt32 Raster Layer
*
*/
QTest::newRow( "testcase 14" )
<< "-3.000000000,7.000000000,-4.000000000,6.000000000 [EPSG:4326]"
<< QStringLiteral( "" )
<< Qgis::UInt32
<< "EPSG:4326"
<< 1.0
<< -1.0
<< 4;
/*
* Testcase 16
*
* inputExtent = from "/raster/band1_int16_noct_epsg4326.tif"
* crs = EPSG:4326
* pixelSize = 1.0
* constantValue = 12.0
* Float32 Raster Layer
*
*/
QTest::newRow( "testcase 16" )
<< "-3.000000000,7.000000000,-4.000000000,6.000000000 [EPSG:4326]"
<< QStringLiteral( "/createConstantRaster_testcase16.tif" )
<< Qgis::Float32
<< "EPSG:4326"
<< 1.0
<< 12.12
<< 5;
/*
* Testcase 17
*
* inputExtent = from "/raster/band1_int16_noct_epsg4326.tif"
* crs = EPSG:4326
* pixelSize = 1.0
* constantValue = 12.0
* Float64 Raster Layer
*
*/
QTest::newRow( "testcase 17" )
<< "-3.000000000,7.000000000,-4.000000000,6.000000000 [EPSG:4326]"
<< QStringLiteral( "/createConstantRaster_testcase17.tif" )
<< Qgis::Float64
<< "EPSG:4326"
<< 1.0
<< 12.125789212532487
<< 6;
}
void TestQgsProcessingAlgs::createConstantRaster()
{
QFETCH( QString, inputExtent );
QFETCH( QString, expectedRaster );
QFETCH( Qgis::DataType, expectedDataType );
QFETCH( QString, crs );
QFETCH( double, pixelSize );
QFETCH( double, constantValue );
QFETCH( int, typeId );
//prepare input params
QgsProject p;
std::unique_ptr< QgsProcessingAlgorithm > alg( QgsApplication::processingRegistry()->createAlgorithmById( QStringLiteral( "native:createconstantrasterlayer" ) ) );
QString myDataPath( TEST_DATA_DIR ); //defined in CmakeLists.txt
//set project crs and ellipsoid from input layer
p.setCrs( QgsCoordinateReferenceSystem( crs ), true );
//set project after layer has been added so that transform context/ellipsoid from crs is also set
std::unique_ptr< QgsProcessingContext > context = qgis::make_unique< QgsProcessingContext >();
context->setProject( &p );
QVariantMap parameters;
parameters.insert( QStringLiteral( "EXTENT" ), inputExtent );
parameters.insert( QStringLiteral( "TARGET_CRS" ), QgsCoordinateReferenceSystem( crs ) );
parameters.insert( QStringLiteral( "PIXEL_SIZE" ), pixelSize );
parameters.insert( QStringLiteral( "NUMBER" ), constantValue );
parameters.insert( QStringLiteral( "OUTPUT_TYPE" ), typeId );
parameters.insert( QStringLiteral( "OUTPUT" ), QgsProcessing::TEMPORARY_OUTPUT );
bool ok = false;
QgsProcessingFeedback feedback;
QVariantMap results;
if ( expectedRaster.isEmpty() )
{
//verify if user feedback for unacceptable values are thrown
alg->run( parameters, *context, &feedback, &ok );
QVERIFY( !ok );
}
else
{
//prepare expectedRaster
std::unique_ptr<QgsRasterLayer> expectedRasterLayer = qgis::make_unique< QgsRasterLayer >( myDataPath + "/control_images/expected_constantRaster" + expectedRaster, "expectedDataset", "gdal" );
std::unique_ptr< QgsRasterInterface > expectedInterface( expectedRasterLayer->dataProvider()->clone() );
QgsRasterIterator expectedIter( expectedInterface.get() );
expectedIter.startRasterRead( 1, expectedRasterLayer->width(), expectedRasterLayer->height(), expectedInterface->extent() );
//run alg...
results = alg->run( parameters, *context, &feedback, &ok );
QVERIFY( ok );
//...and check results with expected datasets
std::unique_ptr<QgsRasterLayer> outputRaster = qgis::make_unique< QgsRasterLayer >( results.value( QStringLiteral( "OUTPUT" ) ).toString(), "output", "gdal" );
std::unique_ptr< QgsRasterInterface > outputInterface( outputRaster->dataProvider()->clone() );
QCOMPARE( outputRaster->width(), expectedRasterLayer->width() );
QCOMPARE( outputRaster->height(), expectedRasterLayer->height() );
QCOMPARE( outputInterface->dataType( 1 ), expectedDataType );
QgsRasterIterator outputIter( outputInterface.get() );
outputIter.startRasterRead( 1, outputRaster->width(), outputRaster->height(), outputInterface->extent() );
int outputIterLeft = 0;
int outputIterTop = 0;
int outputIterCols = 0;
int outputIterRows = 0;
int expectedIterLeft = 0;
int expectedIterTop = 0;
int expectedIterCols = 0;
int expectedIterRows = 0;
std::unique_ptr< QgsRasterBlock > outputRasterBlock;
std::unique_ptr< QgsRasterBlock > expectedRasterBlock;
while ( outputIter.readNextRasterPart( 1, outputIterCols, outputIterRows, outputRasterBlock, outputIterLeft, outputIterTop ) &&
expectedIter.readNextRasterPart( 1, expectedIterCols, expectedIterRows, expectedRasterBlock, expectedIterLeft, expectedIterTop ) )
{
for ( int row = 0; row < expectedIterRows; row++ )
{
for ( int column = 0; column < expectedIterCols; column++ )
{
double expectedValue = expectedRasterBlock->value( row, column );
double outputValue = outputRasterBlock->value( row, column );
QCOMPARE( outputValue, expectedValue );
}
}
}
}
}
void TestQgsProcessingAlgs::densifyGeometries_data()
{
QTest::addColumn<QgsGeometry>( "sourceGeometry" );