Skip to content
Permalink
Browse files

rasterize test

  • Loading branch information
vcloarec authored and PeterPetrik committed Nov 10, 2020
1 parent da3c312 commit d9fa1a6331e3a6f0d4d750798ec275ec2f8ae16e
@@ -1,5 +1,5 @@
/***************************************************************************
qgsalgorithmtinmeshcreation.h
qgsalgorithmexportmesh.cpp
---------------------------
begin : October 2020
copyright : (C) 2020 by Vincent Cloarec
@@ -670,7 +670,35 @@ QSet<int> QgsExportMeshOnGridAlgorithm::supportedDataType()
QgsMeshDatasetGroupMetadata::DataOnVolumes} );
}

///@endcond PRIVATE
QString QgsMeshRasterizeAlgorithm::name() const
{
return QStringLiteral( "meshrasterize" );
}

QString QgsMeshRasterizeAlgorithm::displayName() const
{
return QObject::tr( "Rasterize mesh dataset" );
}

QString QgsMeshRasterizeAlgorithm::group() const
{
return QObject::tr( "Mesh" );
}

QString QgsMeshRasterizeAlgorithm::groupId() const
{
return QStringLiteral( "mesh" );
}

QString QgsMeshRasterizeAlgorithm::shortHelpString() const
{
return QObject::tr( "Create a raster layer from a mesh dataset" );
}

QgsProcessingAlgorithm *QgsMeshRasterizeAlgorithm::createInstance() const
{
return new QgsMeshRasterizeAlgorithm();
}

void QgsMeshRasterizeAlgorithm::initAlgorithm( const QVariantMap &configuration )
{
@@ -773,6 +801,8 @@ bool QgsMeshRasterizeAlgorithm::prepareAlgorithm( const QVariantMap &parameters,
feedback->setProgress( 100 * i / datasetGroups.count() );
}

mLayerRendererSettings = meshLayer->rendererSettings();

return true;
}

@@ -797,11 +827,13 @@ QVariantMap QgsMeshRasterizeAlgorithm::processAlgorithm( const QVariantMap &para
// create raster
double pixelSize = parameterAsDouble( parameters, QStringLiteral( "PIXEL_SIZE" ), context );
QgsRectangle extent = parameterAsExtent( parameters, QStringLiteral( "EXTENT" ), context );
if ( extent.isEmpty() )
extent = mTriangularMesh.extent();

int width = extent.width() / pixelSize;
int height = extent.height() / pixelSize;

QString fileName = parameterAsFileOutput( parameters, QStringLiteral( "OUTPUT" ), context );
QString fileName = parameterAsOutputLayer( parameters, QStringLiteral( "OUTPUT" ), context );
QFileInfo fileInfo( fileName );
QString outputFormat = QgsRasterFileWriter::driverForExtension( fileInfo.suffix() );
QgsRasterFileWriter rasterFileWriter( fileName );
@@ -861,3 +893,5 @@ QSet<int> QgsMeshRasterizeAlgorithm::supportedDataType()
QgsMeshDatasetGroupMetadata::DataOnFaces,
QgsMeshDatasetGroupMetadata::DataOnVolumes} );
}

///@endcond PRIVATE
@@ -1,5 +1,5 @@
/***************************************************************************
qgsalgorithmtinmeshcreation.h
qgsalgorithmexportmesh.h
---------------------------
begin : October 2020
copyright : (C) 2020 by Vincent Cloarec
@@ -15,8 +15,8 @@
* *
***************************************************************************/

#ifndef QGSALGORITHMEXPORTMESHVERTICES_H
#define QGSALGORITHMEXPORTMESHVERTICES_H
#ifndef QGSALGORITHMEXPORTMESH_H
#define QGSALGORITHMEXPORTMESH_H

#define SIP_NO_FILE

@@ -164,32 +164,14 @@ class QgsMeshRasterizeAlgorithm : public QgsProcessingAlgorithm
{

public:
QString name() const override
{
return QStringLiteral( "meshrasterize" );
}
QString displayName() const override
{
return QObject::tr( "Rasterize mesh dataset" );
}
QString group() const override
{
return QObject::tr( "Mesh" );
}
QString groupId() const override
{
return QStringLiteral( "mesh" );
}
QString shortHelpString() const override
{
return QObject::tr( "Create a raster layer from a mesh dataset" );
}
QString name() const override;
QString displayName() const override;
QString group() const override;
QString groupId() const override;
QString shortHelpString() const override;

protected:
QgsProcessingAlgorithm *createInstance() const override
{
return new QgsMeshRasterizeAlgorithm();
}
QgsProcessingAlgorithm *createInstance() const override;
void initAlgorithm( const QVariantMap &configuration = QVariantMap() ) override;
bool prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) override;
QVariantMap processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) override;
@@ -214,4 +196,4 @@ class QgsMeshRasterizeAlgorithm : public QgsProcessingAlgorithm

///@endcond PRIVATE

#endif // QGSALGORITHMEXPORTMESHVERTICES_H
#endif // QGSALGORITHMEXPORTMESH_H
@@ -153,6 +153,7 @@ class TestQgsProcessingAlgs: public QObject
void exportMeshFaces();
void exportMeshEdges();
void exportMeshOnGrid();
void rasterizeMesh();

private:

@@ -5118,6 +5119,72 @@ void TestQgsProcessingAlgs::exportMeshOnGrid()

}

void TestQgsProcessingAlgs::rasterizeMesh()
{
std::unique_ptr< QgsProcessingAlgorithm > alg( QgsApplication::processingRegistry()->createAlgorithmById( QStringLiteral( "native:meshrasterize" ) ) );
QVERIFY( alg != nullptr );

QVariantMap parameters;
parameters.insert( QStringLiteral( "INPUT" ), "mesh layer" );

QVariantList datasetGroup;
datasetGroup << 1 << 2 << 3;
parameters.insert( QStringLiteral( "DATASET_GROUPS" ), datasetGroup );

QVariantMap datasetTime;
datasetTime[QStringLiteral( "type" )] = QStringLiteral( "dataset-time-step" );
QVariantList datasetIndex;
datasetIndex << 1 << 1;
datasetTime[QStringLiteral( "value" )] = datasetIndex;
parameters.insert( QStringLiteral( "DATASET_TIME" ), datasetTime );

parameters.insert( QStringLiteral( "PIXEL_SIZE" ), 200.0 );

parameters.insert( QStringLiteral( "OUTPUT" ), QgsProcessing::TEMPORARY_OUTPUT );

std::unique_ptr< QgsProcessingContext > context = qgis::make_unique< QgsProcessingContext >();
context->setProject( QgsProject::instance() );
QgsProcessingFeedback feedback;
QVariantMap results;
bool ok = false;
results = alg->run( parameters, *context, &feedback, &ok );
QVERIFY( ok );

std::unique_ptr<QgsRasterLayer> outputRaster = qgis::make_unique< QgsRasterLayer >( results.value( QStringLiteral( "OUTPUT" ) ).toString(), "output", "gdal" );
QVERIFY( outputRaster );
QVERIFY( outputRaster->isValid() );
QgsRasterDataProvider *outputProvider = outputRaster->dataProvider();

QCOMPARE( outputProvider->bandCount(), 3 );
QCOMPARE( outputProvider->xSize(), 10 );
QCOMPARE( outputProvider->ySize(), 5 );

std::unique_ptr<QgsRasterBlock> outputBlock_1( outputProvider->block( 1, outputRaster->extent(), 10, 5 ) );
std::unique_ptr<QgsRasterBlock> outputBlock_2( outputProvider->block( 2, outputRaster->extent(), 10, 5 ) );
std::unique_ptr<QgsRasterBlock> outputBlock_3( outputProvider->block( 3, outputRaster->extent(), 10, 5 ) );

// load expected result
QString dataDir = QString( TEST_DATA_DIR ); //defined in CmakeLists.txt
std::unique_ptr<QgsRasterLayer> expectedRaster = qgis::make_unique< QgsRasterLayer >( dataDir + "/mesh/rasterized_mesh.tif", "expected", "gdal" );
QVERIFY( expectedRaster );
QVERIFY( expectedRaster->isValid() );
QgsRasterDataProvider *expectedProvider = outputRaster->dataProvider();
std::unique_ptr<QgsRasterBlock> expectedBlock_1( expectedProvider->block( 1, expectedRaster->extent(), 10, 5 ) );
std::unique_ptr<QgsRasterBlock> expectedBlock_2( expectedProvider->block( 2, expectedRaster->extent(), 10, 5 ) );
std::unique_ptr<QgsRasterBlock> expectedBlock_3( expectedProvider->block( 3, expectedRaster->extent(), 10, 5 ) );

for ( int ix = 0; ix < 10; ++ix )
{
for ( int iy = 0; iy < 5; ++iy )
{
QCOMPARE( outputBlock_1->value( iy, ix ), expectedBlock_1->value( iy, ix ) );
QCOMPARE( outputBlock_2->value( iy, ix ), expectedBlock_2->value( iy, ix ) );
QCOMPARE( outputBlock_3->value( iy, ix ), expectedBlock_3->value( iy, ix ) );
}
}
}


bool TestQgsProcessingAlgs::imageCheck( const QString &testName, const QString &renderedImage )
{
QgsRenderChecker checker;
Binary file not shown.

0 comments on commit d9fa1a6

Please sign in to comment.
You can’t perform that action at this time.