From f16ca89c62c31f395a149a06ef6bf5bf623be151 Mon Sep 17 00:00:00 2001 From: Jacky Volpes Date: Thu, 18 Jul 2024 15:30:40 +0200 Subject: [PATCH] Processing: Add Fix Geometry algorithm and test: Area Porting area fix from geometry checker to processing --- src/analysis/CMakeLists.txt | 1 + .../qgsalgorithmfixgeometryarea.cpp | 292 ++++++++++++++++++ .../processing/qgsalgorithmfixgeometryarea.h | 55 ++++ .../processing/qgsnativealgorithms.cpp | 2 + .../analysis/testqgsprocessingfixgeometry.cpp | 97 ++++++ .../testdata/geometry_fix/merge_polygons.gpkg | Bin 0 -> 118784 bytes 6 files changed, 447 insertions(+) create mode 100644 src/analysis/processing/qgsalgorithmfixgeometryarea.cpp create mode 100644 src/analysis/processing/qgsalgorithmfixgeometryarea.h create mode 100644 tests/testdata/geometry_fix/merge_polygons.gpkg diff --git a/src/analysis/CMakeLists.txt b/src/analysis/CMakeLists.txt index a1609045a46..9000e699854 100644 --- a/src/analysis/CMakeLists.txt +++ b/src/analysis/CMakeLists.txt @@ -64,6 +64,7 @@ set(QGIS_ANALYSIS_SRCS processing/qgsalgorithmcheckgeometryangle.cpp processing/qgsalgorithmfixgeometryangle.cpp processing/qgsalgorithmcheckgeometryarea.cpp + processing/qgsalgorithmfixgeometryarea.cpp processing/qgsalgorithmclip.cpp processing/qgsalgorithmconcavehull.cpp processing/qgsalgorithmconditionalbranch.cpp diff --git a/src/analysis/processing/qgsalgorithmfixgeometryarea.cpp b/src/analysis/processing/qgsalgorithmfixgeometryarea.cpp new file mode 100644 index 00000000000..1f8c1d56159 --- /dev/null +++ b/src/analysis/processing/qgsalgorithmfixgeometryarea.cpp @@ -0,0 +1,292 @@ +/*************************************************************************** + qgsalgorithmfixgeometryarea.cpp + --------------------- + begin : June 2024 + copyright : (C) 2024 by Jacky Volpes + email : jacky dot volpes at oslandia dot com +***************************************************************************/ + +/*************************************************************************** + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + ***************************************************************************/ + +#include "qgsalgorithmfixgeometryarea.h" +#include "qgsgeometryareacheck.h" +#include "qgsvectordataproviderfeaturepool.h" +#include "qgsgeometrycheckerror.h" +#include "qgsvectorfilewriter.h" + +///@cond PRIVATE + +auto QgsFixGeometryAreaAlgorithm::name() const -> QString +{ + return QStringLiteral( "fixgeometryarea" ); +} + +auto QgsFixGeometryAreaAlgorithm::displayName() const -> QString +{ + return QObject::tr( "Fix geometry (Area)" ); +} + +auto QgsFixGeometryAreaAlgorithm::tags() const -> QStringList +{ + return QObject::tr( "merge,polygons,neighbor,fix,area" ).split( ',' ); +} + +auto QgsFixGeometryAreaAlgorithm::group() const -> QString +{ + return QObject::tr( "Fix geometry" ); +} + +auto QgsFixGeometryAreaAlgorithm::groupId() const -> QString +{ + return QStringLiteral( "fixgeometry" ); +} + +auto QgsFixGeometryAreaAlgorithm::shortHelpString() const -> QString +{ + return QObject::tr( "This algorithm merges neighboring polygons according to the chosen method, " + "based on an error layer from the check area algorithm." ); +} + +auto QgsFixGeometryAreaAlgorithm::createInstance() const -> QgsFixGeometryAreaAlgorithm * +{ + return new QgsFixGeometryAreaAlgorithm(); +} + +void QgsFixGeometryAreaAlgorithm::initAlgorithm( const QVariantMap &configuration ) +{ + Q_UNUSED( configuration ) + + // Inputs + addParameter( new QgsProcessingParameterFeatureSource( QStringLiteral( "INPUT" ), QObject::tr( "Input layer" ), + QList< int >() << static_cast( Qgis::ProcessingSourceType::VectorPolygon ) ) + ); + addParameter( new QgsProcessingParameterFeatureSource( QStringLiteral( "ERRORS" ), QObject::tr( "Errors layer" ), + QList< int >() << static_cast( Qgis::ProcessingSourceType::VectorPoint ) ) + ); + + // Specific inputs for this check + QStringList methods; + { + // const QVariantMap config; + // QgsGeometryCheckContext *context; //!\ uninitialized context to retrieve resolution methods ontly (which should perhaps be a static method?) + QList checkMethods = QgsGeometryAreaCheck( nullptr, QVariantMap() ).availableResolutionMethods(); + std::transform( checkMethods.cbegin(), checkMethods.cend() - 2, std::inserter( methods, methods.begin() ), + []( const QgsGeometryCheckResolutionMethod & checkMethod ) { return checkMethod.name(); } ); + } + addParameter( new QgsProcessingParameterEnum( QStringLiteral( "METHOD" ), QObject::tr( "Method" ), methods ) ); + addParameter( new QgsProcessingParameterField( + QStringLiteral( "MERGE_ATTRIBUTE" ), QObject::tr( "Field to consider when merging polygons with the identical attribue method" ), + QString(), QStringLiteral( "INPUT" ), + Qgis::ProcessingFieldParameterDataType::Any, false, true ) + ); + + addParameter( new QgsProcessingParameterField( + QStringLiteral( "UNIQUE_ID" ), QObject::tr( "Field of original feature unique identifier" ), + QStringLiteral( "id" ), QStringLiteral( "ERRORS" ) ) + ); + addParameter( new QgsProcessingParameterField( + QStringLiteral( "PART_IDX" ), QObject::tr( "Field of part index" ), + QStringLiteral( "gc_partidx" ), QStringLiteral( "ERRORS" ), + Qgis::ProcessingFieldParameterDataType::Numeric ) + ); + addParameter( new QgsProcessingParameterField( + QStringLiteral( "RING_IDX" ), QObject::tr( "Field of ring index" ), + QStringLiteral( "gc_ringidx" ), QStringLiteral( "ERRORS" ), + Qgis::ProcessingFieldParameterDataType::Numeric ) + ); + addParameter( new QgsProcessingParameterField( + QStringLiteral( "VERTEX_IDX" ), QObject::tr( "Field of vertex index" ), + QStringLiteral( "gc_vertidx" ), QStringLiteral( "ERRORS" ), + Qgis::ProcessingFieldParameterDataType::Numeric ) + ); + + // Outputs + addParameter( new QgsProcessingParameterFeatureSink( QStringLiteral( "OUTPUT" ), QObject::tr( "Output layer" ), Qgis::ProcessingSourceType::VectorPolygon ) ); + addParameter( new QgsProcessingParameterFeatureSink( QStringLiteral( "REPORT" ), QObject::tr( "Report layer" ), Qgis::ProcessingSourceType::VectorPoint ) ); + + std::unique_ptr< QgsProcessingParameterNumber > tolerance = std::make_unique< QgsProcessingParameterNumber >( QStringLiteral( "TOLERANCE" ), + QObject::tr( "Tolerance" ), Qgis::ProcessingNumberParameterType::Integer, 8, false, 1, 13 ); + tolerance->setFlags( tolerance->flags() | Qgis::ProcessingParameterFlag::Advanced ); + addParameter( tolerance.release() ); +} + +auto QgsFixGeometryAreaAlgorithm::processAlgorithm( const QVariantMap ¶meters, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) -> QVariantMap +{ + const std::unique_ptr< QgsProcessingFeatureSource > input( parameterAsSource( parameters, QStringLiteral( "INPUT" ), context ) ); + if ( !input ) + throw QgsProcessingException( invalidSourceError( parameters, QStringLiteral( "INPUT" ) ) ); + + const std::unique_ptr< QgsProcessingFeatureSource > errors( parameterAsSource( parameters, QStringLiteral( "ERRORS" ), context ) ); + if ( !errors ) + throw QgsProcessingException( invalidSourceError( parameters, QStringLiteral( "ERRORS" ) ) ); + + QgsProcessingMultiStepFeedback multiStepFeedback( 2, feedback ); + + const QString featIdFieldName = parameterAsString( parameters, QStringLiteral( "UNIQUE_ID" ), context ); + const QString partIdxFieldName = parameterAsString( parameters, QStringLiteral( "PART_IDX" ), context ); + const QString ringIdxFieldName = parameterAsString( parameters, QStringLiteral( "RING_IDX" ), context ); + const QString vertexIdxFieldName = parameterAsString( parameters, QStringLiteral( "VERTEX_IDX" ), context ); + + // Specific inputs for this check + const QString mergeAttributeName = parameterAsString( parameters, QStringLiteral( "MERGE_ATTRIBUTE" ), context ); + const int method = parameterAsEnum( parameters, QStringLiteral( "METHOD" ), context ); + + // Verify that input fields exists + if ( errors->fields().indexFromName( featIdFieldName ) == -1 ) + throw QgsProcessingException( QObject::tr( "Field %1 does not exist in errors layer." ).arg( featIdFieldName ) ); + if ( errors->fields().indexFromName( partIdxFieldName ) == -1 ) + throw QgsProcessingException( QObject::tr( "Field %1 does not exist in errors layer." ).arg( partIdxFieldName ) ); + if ( errors->fields().indexFromName( ringIdxFieldName ) == -1 ) + throw QgsProcessingException( QObject::tr( "Field %1 does not exist in errors layer." ).arg( ringIdxFieldName ) ); + if ( errors->fields().indexFromName( vertexIdxFieldName ) == -1 ) + throw QgsProcessingException( QObject::tr( "Field %1 does not exist in errors layer." ).arg( vertexIdxFieldName ) ); + int inputIdFieldIndex = input->fields().indexFromName( featIdFieldName ); + if ( inputIdFieldIndex == -1 ) + throw QgsProcessingException( QObject::tr( "Field %1 does not exist in input layer." ).arg( featIdFieldName ) ); + + QgsField inputFeatIdField = input->fields().at( inputIdFieldIndex ); + if ( inputFeatIdField.type() != errors->fields().at( errors->fields().indexFromName( featIdFieldName ) ).type() ) + throw QgsProcessingException( QObject::tr( "Field %1 does not have the same type than in errors layer." ).arg( featIdFieldName ) ); + + QString dest_output; + const std::unique_ptr< QgsFeatureSink > sink_output( parameterAsSink( parameters, QStringLiteral( "OUTPUT" ), context, dest_output, input->fields(), input->wkbType(), input->sourceCrs() ) ); + if ( !sink_output ) + throw QgsProcessingException( invalidSinkError( parameters, QStringLiteral( "OUTPUT" ) ) ); + + QString dest_report; + QgsFields reportFields = errors->fields(); + reportFields.append( QgsField( QStringLiteral( "report" ), QMetaType::QString ) ); + reportFields.append( QgsField( QStringLiteral( "error_fixed" ), QMetaType::Bool ) ); + const std::unique_ptr< QgsFeatureSink > sink_report( parameterAsSink( parameters, QStringLiteral( "REPORT" ), context, dest_report, reportFields, errors->wkbType(), errors->sourceCrs() ) ); + if ( !sink_report ) + throw QgsProcessingException( invalidSinkError( parameters, QStringLiteral( "REPORT" ) ) ); + + const QgsProject *project = QgsProject::instance(); + std::unique_ptr checkContext = std::make_unique( mTolerance, input->sourceCrs(), project->transformContext(), project ); + QStringList messages; + QVariantMap configurationCheck; + + // maximum limit, we know that every feature to process is an error (otherwise it is not treated and marked as obsolete) + configurationCheck.insert( "areaThreshold", std::numeric_limits::max() ); + const QgsGeometryAreaCheck check( checkContext.get(), configurationCheck ); + + QgsVectorLayer *fixedLayer = input->materialize( QgsFeatureRequest() ); + std::unique_ptr featurePool = std::make_unique( fixedLayer, false ); + QMap featurePools; + featurePools.insert( fixedLayer->id(), featurePool.get() ); + + QMap attributeIndex; + if ( method == QgsGeometryAreaCheck::ResolutionMethod::MergeIdenticalAttribute ) + { + if ( mergeAttributeName.isEmpty() ) + throw QgsProcessingException( QObject::tr( "Merge field to merge polygons with identical attribute method is empty" ) ); + if ( !fixedLayer->fields().names().contains( mergeAttributeName ) ) + throw QgsProcessingException( QObject::tr( "Merge field %1 does not exist in input layer" ).arg( mergeAttributeName ) ); + attributeIndex.insert( fixedLayer->id(), fixedLayer->fields().indexOf( mergeAttributeName ) ); + } + + QgsFeature errorFeature, inputFeature, testDuplicateIdFeature; + QgsFeatureIterator errorFeaturesIt = errors->getFeatures(); + QList changesList; + QgsFeature reportFeature; + reportFeature.setFields( reportFields ); + long long progression = 0; + long long totalProgression = errors->featureCount(); + multiStepFeedback.setCurrentStep( 1 ); + multiStepFeedback.setProgressText( QObject::tr( "Fixing errors..." ) ); + while ( errorFeaturesIt.nextFeature( errorFeature ) ) + { + progression++; + multiStepFeedback.setProgress( static_cast( static_cast( progression ) / totalProgression ) * 100 ); + reportFeature.setGeometry( errorFeature.geometry() ); + + QVariant attr = errorFeature.attribute( featIdFieldName ); + if ( !attr.isValid() || attr.isNull() ) + throw QgsProcessingException( QObject::tr( "NULL or invalid value found in unique field %1" ).arg( featIdFieldName ) ); + + QString idValue = errorFeature.attribute( featIdFieldName ).toString(); + if ( inputFeatIdField.type() == QMetaType::QString ) + idValue = "'" + idValue + "'"; + + QgsFeatureIterator it = fixedLayer->getFeatures( QgsFeatureRequest().setFilterExpression( "\"" + featIdFieldName + "\" = " + idValue ) ); + if ( !it.nextFeature( inputFeature ) || !inputFeature.isValid() ) + reportFeature.setAttributes( errorFeature.attributes() << QObject::tr( "Source feature not found or invalid" ) << false ); + + else if ( it.nextFeature( testDuplicateIdFeature ) ) + throw QgsProcessingException( QObject::tr( "More than one feature found in input layer with value %1 in unique field %2" ).arg( idValue ).arg( featIdFieldName ) ); + + else if ( inputFeature.geometry().isNull() ) + reportFeature.setAttributes( errorFeature.attributes() << QObject::tr( "Feature geometry is null" ) << false ); + + else if ( QgsGeometryCheckerUtils::getGeomPart( inputFeature.geometry().constGet(), errorFeature.attribute( partIdxFieldName ).toInt() ) == nullptr ) + reportFeature.setAttributes( errorFeature.attributes() << QObject::tr( "Feature geometry part is null" ) << false ); + + else + { + QgsGeometryCheckError checkError = QgsGeometryCheckError( + &check, + QgsGeometryCheckerUtils::LayerFeature( featurePool.get(), inputFeature, checkContext.get(), false ), + errorFeature.geometry().asPoint(), + QgsVertexId( + errorFeature.attribute( partIdxFieldName ).toInt(), + errorFeature.attribute( ringIdxFieldName ).toInt(), + errorFeature.attribute( vertexIdxFieldName ).toInt() + ) + ); + for ( auto changes : changesList ) + checkError.handleChanges( changes ); + + QgsGeometryCheck::Changes changes; + check.fixError( featurePools, &checkError, method, attributeIndex, changes ); + changesList << changes; + reportFeature.setAttributes( errorFeature.attributes() << checkError.resolutionMessage() << ( checkError.status() == QgsGeometryCheckError::StatusFixed ) ); + } + + if ( !sink_report->addFeature( reportFeature, QgsFeatureSink::FastInsert ) ) + throw QgsProcessingException( writeFeatureError( sink_report.get(), parameters, QStringLiteral( "REPORT" ) ) ); + + } + multiStepFeedback.setProgress( 100 ); + + progression = 0; + totalProgression = fixedLayer->featureCount(); + multiStepFeedback.setCurrentStep( 2 ); + multiStepFeedback.setProgressText( QObject::tr( "Exporting fixed layer..." ) ); + QgsFeature fixedFeature; + QgsFeatureIterator fixedFeaturesIt = fixedLayer->getFeatures(); + while ( fixedFeaturesIt.nextFeature( fixedFeature ) ) + { + progression++; + multiStepFeedback.setProgress( static_cast( static_cast( progression ) / totalProgression ) * 100 ); + if ( !sink_output->addFeature( fixedFeature, QgsFeatureSink::FastInsert ) ) + throw QgsProcessingException( writeFeatureError( sink_output.get(), parameters, QStringLiteral( "OUTPUT" ) ) ); + } + multiStepFeedback.setProgress( 100 ); + + QVariantMap outputs; + outputs.insert( QStringLiteral( "OUTPUT" ), dest_output ); + outputs.insert( QStringLiteral( "REPORT" ), dest_report ); + + return outputs; +} + +auto QgsFixGeometryAreaAlgorithm::prepareAlgorithm( const QVariantMap ¶meters, QgsProcessingContext &context, QgsProcessingFeedback * ) -> bool +{ + mTolerance = parameterAsInt( parameters, QStringLiteral( "TOLERANCE" ), context ); + + return true; +} + +auto QgsFixGeometryAreaAlgorithm::flags() const -> Qgis::ProcessingAlgorithmFlags +{ + return QgsProcessingAlgorithm::flags() | Qgis::ProcessingAlgorithmFlag::NoThreading; +} + +///@endcond diff --git a/src/analysis/processing/qgsalgorithmfixgeometryarea.h b/src/analysis/processing/qgsalgorithmfixgeometryarea.h new file mode 100644 index 00000000000..0247c39be13 --- /dev/null +++ b/src/analysis/processing/qgsalgorithmfixgeometryarea.h @@ -0,0 +1,55 @@ +/*************************************************************************** + qgsalgorithmfixgeometryarea.h + --------------------- + begin : June 2024 + copyright : (C) 2024 by Jacky Volpes + email : jacky dot volpes at oslandia dot com +***************************************************************************/ + +/*************************************************************************** + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + ***************************************************************************/ + +#ifndef QGSALGORITHMFIXGEOMETRYAREA_H +#define QGSALGORITHMFIXGEOMETRYAREA_H + +#define SIP_NO_FILE + +#include "qgis_sip.h" +#include "qgsprocessingalgorithm.h" + +///@cond PRIVATE + +class QgsFixGeometryAreaAlgorithm : public QgsProcessingAlgorithm +{ + public: + + QgsFixGeometryAreaAlgorithm() = default; + void initAlgorithm( const QVariantMap &configuration = QVariantMap() ) override; + QString name() const override; + QString displayName() const override; + QStringList tags() const override; + QString group() const override; + QString groupId() const override; + QString shortHelpString() const override; + Qgis::ProcessingAlgorithmFlags flags() const override; + QgsFixGeometryAreaAlgorithm *createInstance() const override SIP_FACTORY; + + protected: + + bool prepareAlgorithm( const QVariantMap ¶meters, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) override; + QVariantMap processAlgorithm( const QVariantMap ¶meters, + QgsProcessingContext &context, QgsProcessingFeedback *feedback ) override; + private: + + int mTolerance{8}; +}; + +///@endcond PRIVATE + +#endif // QGSALGORITHMFIXGEOMETRYAREA_H diff --git a/src/analysis/processing/qgsnativealgorithms.cpp b/src/analysis/processing/qgsnativealgorithms.cpp index 8bc8bfd5291..184a771f078 100644 --- a/src/analysis/processing/qgsnativealgorithms.cpp +++ b/src/analysis/processing/qgsnativealgorithms.cpp @@ -45,6 +45,7 @@ #include "qgsalgorithmcentroid.h" #include "qgsalgorithmcheckgeometryangle.h" #include "qgsalgorithmcheckgeometryarea.h" +#include "qgsalgorithmfixgeometryarea.h" #include "qgsalgorithmclip.h" #include "qgsalgorithmconcavehull.h" #include "qgsalgorithmconditionalbranch.h" @@ -581,6 +582,7 @@ void QgsNativeAlgorithms::loadAlgorithms() addAlgorithm( new QgsDensifyGeometriesByIntervalAlgorithm() ); addAlgorithm( new QgsDensifyGeometriesByCountAlgorithm() ); addAlgorithm( new QgsFixGeometryAngleAlgorithm() ); + addAlgorithm( new QgsFixGeometryAreaAlgorithm() ); } ///@endcond diff --git a/tests/src/analysis/testqgsprocessingfixgeometry.cpp b/tests/src/analysis/testqgsprocessingfixgeometry.cpp index 75d69407ff1..5c7b97de94f 100644 --- a/tests/src/analysis/testqgsprocessingfixgeometry.cpp +++ b/tests/src/analysis/testqgsprocessingfixgeometry.cpp @@ -35,6 +35,9 @@ class TestQgsProcessingFixGeometry: public QgsTest void fixAngleAlg_data(); void fixAngleAlg(); + void fixAreaAlg_data(); + void fixAreaAlg(); + private: const QDir mDataDir{ QDir( TEST_DATA_DIR ).absoluteFilePath( QStringLiteral( "geometry_fix" ) ) }; @@ -175,5 +178,99 @@ void TestQgsProcessingFixGeometry::fixAngleAlg() } } +void TestQgsProcessingFixGeometry::fixAreaAlg_data() +{ + //create a line layer that will be used in tests + QTest::addColumn( "reportList" ); + QTest::addColumn( "method" ); + + QTest::newRow( "Merge with longest shared edge" ) + << ( QStringList() + << QStringLiteral( "Merge with neighboring polygon with longest shared edge" ) + << QStringLiteral( "Failed to merge with neighbor: " ) + << QStringLiteral( "Merge with neighboring polygon with longest shared edge" ) + << QStringLiteral( "Failed to merge with neighbor: " ) + << QStringLiteral( "Failed to merge with neighbor: " ) + << QStringLiteral( "Failed to merge with neighbor: " ) + << QStringLiteral( "Merge with neighboring polygon with longest shared edge" ) + << QStringLiteral( "Merge with neighboring polygon with longest shared edge" ) ) + << 0; + + QTest::newRow( "Merge with largest area" ) + << ( QStringList() + << QStringLiteral( "Merge with neighboring polygon with largest area" ) + << QStringLiteral( "Failed to merge with neighbor: " ) + << QStringLiteral( "Merge with neighboring polygon with largest area" ) + << QStringLiteral( "Failed to merge with neighbor: " ) + << QStringLiteral( "Failed to merge with neighbor: " ) + << QStringLiteral( "Failed to merge with neighbor: " ) + << QStringLiteral( "Merge with neighboring polygon with largest area" ) + << QStringLiteral( "Merge with neighboring polygon with largest area" ) ) + << 1; + + QTest::newRow( "Merge with identical attribute value" ) + << ( QStringList() + << QStringLiteral( "Merge with neighboring polygon with identical attribute value, if any, or leave as is" ) + << QStringLiteral( "Merge with neighboring polygon with identical attribute value, if any, or leave as is" ) + << QStringLiteral( "Merge with neighboring polygon with identical attribute value, if any, or leave as is" ) + << QStringLiteral( "Merge with neighboring polygon with identical attribute value, if any, or leave as is" ) + << QStringLiteral( "Merge with neighboring polygon with identical attribute value, if any, or leave as is" ) + << QStringLiteral( "Merge with neighboring polygon with identical attribute value, if any, or leave as is" ) + << QStringLiteral( "Merge with neighboring polygon with identical attribute value, if any, or leave as is" ) + << QStringLiteral( "Merge with neighboring polygon with identical attribute value, if any, or leave as is" ) ) + << 2; +} + +void TestQgsProcessingFixGeometry::fixAreaAlg() +{ + + const QDir testDataDir( QDir( TEST_DATA_DIR ).absoluteFilePath( "geometry_checker" ) ); + QgsVectorLayer sourceLayer = QgsVectorLayer( testDataDir.absoluteFilePath( "polygon_layer.shp" ), QStringLiteral( "polygons" ), QStringLiteral( "ogr" ) ); + QgsVectorLayer errorsLayer = QgsVectorLayer( mDataDir.absoluteFilePath( "merge_polygons.gpkg|layername=errors_layer" ), QStringLiteral( "" ), QStringLiteral( "ogr" ) ); + QFETCH( QStringList, reportList ); + QFETCH( int, method ); + + QVERIFY( sourceLayer.isValid() ); + QVERIFY( errorsLayer.isValid() ); + + const std::unique_ptr< QgsProcessingAlgorithm > alg( + QgsApplication::processingRegistry()->createAlgorithmById( QStringLiteral( "native:fixgeometryarea" ) ) + ); + QVERIFY( alg != nullptr ); + + QVariantMap parameters; + parameters.insert( QStringLiteral( "INPUT" ), QVariant::fromValue( &sourceLayer ) ); + parameters.insert( QStringLiteral( "UNIQUE_ID" ), "id" ); + parameters.insert( QStringLiteral( "ERRORS" ), QVariant::fromValue( &errorsLayer ) ); + parameters.insert( QStringLiteral( "METHOD" ), method ); + if ( method == 2 ) + parameters.insert( QStringLiteral( "MERGE_ATTRIBUTE" ), QStringLiteral( "attr" ) ); + parameters.insert( QStringLiteral( "OUTPUT" ), QgsProcessing::TEMPORARY_OUTPUT ); + parameters.insert( QStringLiteral( "REPORT" ), QgsProcessing::TEMPORARY_OUTPUT ); + + bool ok = false; + QgsProcessingFeedback feedback; + std::unique_ptr< QgsProcessingContext > context = std::make_unique< QgsProcessingContext >(); + + QVariantMap results; + results = alg->run( parameters, *context, &feedback, &ok ); + QVERIFY( ok ); + + const std::unique_ptr outputLayer( qobject_cast< QgsVectorLayer * >( context->getMapLayer( results.value( QStringLiteral( "OUTPUT" ) ).toString() ) ) ); + const std::unique_ptr reportLayer( qobject_cast< QgsVectorLayer * >( context->getMapLayer( results.value( QStringLiteral( "REPORT" ) ).toString() ) ) ); + QVERIFY( reportLayer->isValid() ); + QVERIFY( outputLayer->isValid() ); + + QCOMPARE( outputLayer->featureCount(), 21 ); + QCOMPARE( reportLayer->featureCount(), reportList.count() ); + int idx = 1; + for ( QString expectedReport : reportList ) + { + const QgsFeature reportFeature = reportLayer->getFeature( idx ); + QCOMPARE( reportFeature.attribute( "report" ), expectedReport ); + idx++; + } +} + QGSTEST_MAIN( TestQgsProcessingFixGeometry ) #include "testqgsprocessingfixgeometry.moc" diff --git a/tests/testdata/geometry_fix/merge_polygons.gpkg b/tests/testdata/geometry_fix/merge_polygons.gpkg new file mode 100644 index 0000000000000000000000000000000000000000..3caf57cfa37a1ec40fa7683dce7c853b09cbf095 GIT binary patch literal 118784 zcmeI*eQX=&eFyNP-blSrC&!lARB>`)s#3HmnJk}1-*;&}v3o}`=k zqL8OZOoDE-?4~XDm)9l8kOAAe0SoreX0%PSAxnS)#V|P790#oj!;Q0|9_qETvn=km z7WJNcA$i9aS*GMzTE4W8FV8*q-0wcmbFUQdnL0ei@hmfwN+%*bc4C zWY|H5FOs2=3@?+RfeiVFhN4WDg%6{X&y401OI{sR-|zghFmWAyTNt&S{uO;HmkCr|Bj_h(ip6#@`|00bZa z0SG_<0uX=z1Xd~VTaCd=FYbf$S1)me9j%k4NiI>ik23*p6Bo{M@Xr$#%jk9sFY4@@05N+XSy5l=8Q zE`AK#Ttodv%hco{Z(zbVQp_^wa1PlVPK(_&)Mp=X+U<^kL5IZ?3LcsW_=3~NjY7#r zi_vLySdAx6R2Vbpun!7jCIh~4FPYnDnq`xVTy)N8u~x`F>G+r=1;`N%?!MR<;1h>?l?6M^6%5}K5pO3upZUe8pp zQpEjI#PNL2-Ep}nrl_A`xFi!}qg)~qC$B=3877rv_&Ju5?KAUpY&ylog!6w5{TfBT zMxOA100bZa0SG_<0uX=z1Rwwb2teSo6<`}%yLWW!_iM?cj!mai=}b5t$+GDgHo`BY z*^DyHZngLKTAjVNp`fjQk9}ZI|6t!xfB&=ZynpH3MSka>x!*hc_JwOl_PcMNzv19K zq2bnd2X@h&|J8B5g}zD7|9?!;KPGSRfB*y_009U<00Izz00bZa0SG`~a|k@H46~!Y zTcf;AAp8=4i!uX+^M4fwK0p8h5P$##AOHafKmY;|fB*zGhrm6a|Lf^rknjJmkpJ|R z&9Q)p5&{r_00bZa0SG_<0uX=z1R(G^7kEOo{2h%Y-~Q{!=!aBUD1854;(?D4fB*y_ z009U<00Izz00b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