diff --git a/src/analysis/CMakeLists.txt b/src/analysis/CMakeLists.txt index 2f287c67463..bb785a08c33 100644 --- a/src/analysis/CMakeLists.txt +++ b/src/analysis/CMakeLists.txt @@ -63,6 +63,7 @@ set(QGIS_ANALYSIS_SRCS processing/qgsalgorithmcentroid.cpp processing/qgsalgorithmcheckgeometrysegmentlength.cpp processing/qgsalgorithmcheckgeometryangle.cpp + processing/qgsalgorithmfixgeometrymultipart.cpp processing/qgsalgorithmfixgeometryoverlap.cpp processing/qgsalgorithmfixgeometrydeletefeatures.cpp processing/qgsalgorithmfixgeometryangle.cpp diff --git a/src/analysis/processing/qgsalgorithmfixgeometrymultipart.cpp b/src/analysis/processing/qgsalgorithmfixgeometrymultipart.cpp new file mode 100644 index 00000000000..40ef21c9250 --- /dev/null +++ b/src/analysis/processing/qgsalgorithmfixgeometrymultipart.cpp @@ -0,0 +1,236 @@ +/*************************************************************************** + qgsalgorithmfixgeometrymultipart.cpp + --------------------- + begin : April 2025 + copyright : (C) 2025 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 "qgsalgorithmfixgeometrymultipart.h" +#include "qgsgeometrycheckerror.h" +#include "qgsgeometrycheckerutils.h" +#include "qgsgeometrymultipartcheck.h" +#include "qgsvectordataproviderfeaturepool.h" +#include "qgsvectorlayer.h" +#include "qgsvectorfilewriter.h" + +///@cond PRIVATE + +QString QgsFixGeometryMultipartAlgorithm::name() const +{ + return QStringLiteral( "fixgeometrymultipart" ); +} + +QString QgsFixGeometryMultipartAlgorithm::displayName() const +{ + return QObject::tr( "Fix geometry (strict multipart)" ); +} + +QStringList QgsFixGeometryMultipartAlgorithm::tags() const +{ + return QObject::tr( "fix,multipart,singlepart" ).split( ',' ); +} + +QString QgsFixGeometryMultipartAlgorithm::group() const +{ + return QObject::tr( "Fix geometry" ); +} + +QString QgsFixGeometryMultipartAlgorithm::groupId() const +{ + return QStringLiteral( "fixgeometry" ); +} + +QString QgsFixGeometryMultipartAlgorithm::shortHelpString() const +{ + return QObject::tr( "This algorithm converts multipart geometries that consists of only one geometry " + "into singlepart geometries, based on an error layer from the check strict multipart algorithm." ); +} + +QgsFixGeometryMultipartAlgorithm *QgsFixGeometryMultipartAlgorithm::createInstance() const +{ + return new QgsFixGeometryMultipartAlgorithm(); +} + +void QgsFixGeometryMultipartAlgorithm::initAlgorithm( const QVariantMap &configuration ) +{ + Q_UNUSED( configuration ) + + // Inputs + addParameter( new QgsProcessingParameterFeatureSource( + QStringLiteral( "INPUT" ), QObject::tr( "Input layer" ), QList() << static_cast( Qgis::ProcessingSourceType::VectorPolygon ) << static_cast( Qgis::ProcessingSourceType::VectorLine ) + ) ); + addParameter( new QgsProcessingParameterFeatureSource( + QStringLiteral( "ERRORS" ), QObject::tr( "Error layer" ), QList() << static_cast( Qgis::ProcessingSourceType::VectorPoint ) + ) ); + addParameter( new QgsProcessingParameterField( + QStringLiteral( "UNIQUE_ID" ), QObject::tr( "Field of original feature unique identifier" ), + QStringLiteral( "id" ), QStringLiteral( "ERRORS" ) + ) ); + + // Outputs + addParameter( new QgsProcessingParameterFeatureSink( + QStringLiteral( "OUTPUT" ), QObject::tr( "Output layer" ), Qgis::ProcessingSourceType::VectorAnyGeometry + ) ); + addParameter( new QgsProcessingParameterFeatureSink( + QStringLiteral( "REPORT" ), QObject::tr( "Report layer" ), Qgis::ProcessingSourceType::VectorPoint + ) ); + + std::unique_ptr tolerance = std::make_unique( + QStringLiteral( "TOLERANCE" ), QObject::tr( "Tolerance" ), Qgis::ProcessingNumberParameterType::Integer, 8, false, 1, 13 + ); + tolerance->setFlags( tolerance->flags() | Qgis::ProcessingParameterFlag::Advanced ); + addParameter( tolerance.release() ); +} + +QVariantMap QgsFixGeometryMultipartAlgorithm::processAlgorithm( const QVariantMap ¶meters, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) +{ + const std::unique_ptr input( parameterAsSource( parameters, QStringLiteral( "INPUT" ), context ) ); + if ( !input ) + throw QgsProcessingException( invalidSourceError( parameters, QStringLiteral( "INPUT" ) ) ); + + const std::unique_ptr 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 ); + + // Verify that input fields exists + if ( errors->fields().indexFromName( featIdFieldName ) == -1 ) + throw QgsProcessingException( QObject::tr( "Field \"%1\" does not exist in the error layer." ).arg( featIdFieldName ) ); + 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 as in the error layer." ).arg( featIdFieldName ) ); + + QString dest_output; + const std::unique_ptr 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 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(); + QgsGeometryCheckContext checkContext = QgsGeometryCheckContext( mTolerance, input->sourceCrs(), project->transformContext(), project ); + QStringList messages; + + const QgsGeometryMultipartCheck check( &checkContext, QVariantMap() ); + + QgsVectorLayer *fixedLayer = input->materialize( QgsFeatureRequest() ); + QgsVectorDataProviderFeaturePool featurePool = QgsVectorDataProviderFeaturePool( fixedLayer ); + QMap featurePools; + featurePools.insert( fixedLayer->id(), &featurePool ); + + 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() ); + + 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 + { + QgsGeometryCheckError checkError = QgsGeometryCheckError( + &check, + QgsGeometryCheckerUtils::LayerFeature( &featurePool, inputFeature, &checkContext, false ), + errorFeature.geometry().asPoint(), + QgsVertexId() + ); + for ( QgsGeometryCheck::Changes changes : changesList ) + checkError.handleChanges( changes ); + + QgsGeometryCheck::Changes changes; + check.fixError( featurePools, &checkError, QgsGeometryMultipartCheck::ResolutionMethod::ConvertToSingle, QMap(), changes ); + changesList << changes; + QString resolutionMessage = checkError.resolutionMessage(); + if ( checkError.status() == QgsGeometryCheckError::StatusObsolete ) + resolutionMessage = QObject::tr( "Error is obsolete" ); + reportFeature.setAttributes( errorFeature.attributes() << 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; +} + +bool QgsFixGeometryMultipartAlgorithm::prepareAlgorithm( const QVariantMap ¶meters, QgsProcessingContext &context, QgsProcessingFeedback * ) +{ + mTolerance = parameterAsInt( parameters, QStringLiteral( "TOLERANCE" ), context ); + + return true; +} + +Qgis::ProcessingAlgorithmFlags QgsFixGeometryMultipartAlgorithm::flags() const +{ + return QgsProcessingAlgorithm::flags() | Qgis::ProcessingAlgorithmFlag::NoThreading; +} + +///@endcond diff --git a/src/analysis/processing/qgsalgorithmfixgeometrymultipart.h b/src/analysis/processing/qgsalgorithmfixgeometrymultipart.h new file mode 100644 index 00000000000..a0953170757 --- /dev/null +++ b/src/analysis/processing/qgsalgorithmfixgeometrymultipart.h @@ -0,0 +1,52 @@ +/*************************************************************************** + qgsalgorithmfixgeometrymultipart.h + --------------------- + begin : April 2025 + copyright : (C) 2025 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 QGSALGORITHMFIXGEOMETRYMULTIPART_H +#define QGSALGORITHMFIXGEOMETRYMULTIPART_H + +#define SIP_NO_FILE + +#include "qgis_sip.h" +#include "qgsprocessingalgorithm.h" + +///@cond PRIVATE + +class QgsFixGeometryMultipartAlgorithm : public QgsProcessingAlgorithm +{ + public: + QgsFixGeometryMultipartAlgorithm() = 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; + QgsFixGeometryMultipartAlgorithm *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 // QGSALGORITHMFIXGEOMETRYMULTIPART_H diff --git a/src/analysis/processing/qgsnativealgorithms.cpp b/src/analysis/processing/qgsnativealgorithms.cpp index c52b1948e90..304b4321c09 100644 --- a/src/analysis/processing/qgsnativealgorithms.cpp +++ b/src/analysis/processing/qgsnativealgorithms.cpp @@ -50,6 +50,7 @@ #include "qgsalgorithmfixgeometrydeletefeatures.h" #include "qgsalgorithmfixgeometrygap.h" #include "qgsalgorithmfixgeometryarea.h" +#include "qgsalgorithmfixgeometrymultipart.h" #include "qgsalgorithmfixgeometryoverlap.h" #include "qgsalgorithmfixgeometryhole.h" #include "qgsalgorithmfixgeometrymissingvertex.h" @@ -634,6 +635,7 @@ void QgsNativeAlgorithms::loadAlgorithms() addAlgorithm( new QgsFixGeometryOverlapAlgorithm() ); addAlgorithm( new QgsFixGeometryDeleteFeaturesAlgorithm() ); addAlgorithm( new QgsFixGeometryAngleAlgorithm() ); + addAlgorithm( new QgsFixGeometryMultipartAlgorithm() ); addAlgorithm( new QgsFixGeometrySelfIntersectionAlgorithm() ); addAlgorithm( new QgsFixGeometryGapAlgorithm() ); addAlgorithm( new QgsFixGeometryAreaAlgorithm() ); diff --git a/tests/src/analysis/testqgsprocessingfixgeometry.cpp b/tests/src/analysis/testqgsprocessingfixgeometry.cpp index 2d660dd7964..e701a87bc91 100644 --- a/tests/src/analysis/testqgsprocessingfixgeometry.cpp +++ b/tests/src/analysis/testqgsprocessingfixgeometry.cpp @@ -17,6 +17,7 @@ #include "qgsnativealgorithms.h" #include "qgsprocessingregistry.h" #include "qgstest.h" +#include "qgswkbtypes.h" #include "qgsvectorlayer.h" class TestQgsProcessingFixGeometry : public QgsTest @@ -36,6 +37,9 @@ class TestQgsProcessingFixGeometry : public QgsTest void fixAngleAlg_data(); void fixAngleAlg(); + void fixMultipartAlg_data(); + void fixMultipartAlg(); + void fixAreaAlg_data(); void fixAreaAlg(); @@ -187,6 +191,83 @@ void TestQgsProcessingFixGeometry::fixAngleAlg() } } +void TestQgsProcessingFixGeometry::fixMultipartAlg_data() +{ + const QDir testDataDir( QDir( TEST_DATA_DIR ).absoluteFilePath( "geometry_checker" ) ); + + QTest::addColumn( "sourceLayer" ); + QTest::addColumn( "errorsLayer" ); + QTest::addColumn( "reportList" ); + + QStringList linesReportList; + for ( int i = 0; i < 8; i++ ) + linesReportList << QStringLiteral( "Convert to single part feature" ); + QTest::newRow( "Lines" ) + << new QgsVectorLayer( testDataDir.absoluteFilePath( "line_layer.shp" ), QStringLiteral( "lines" ), QStringLiteral( "ogr" ) ) + << new QgsVectorLayer( mDataDir.absoluteFilePath( "strict_multipart.gpkg|layername=lines_to_fix" ), QStringLiteral( "lines fo fix" ), QStringLiteral( "ogr" ) ) + << linesReportList; + + QStringList polygonsReportList; + for ( int i = 0; i < 24; i++ ) + polygonsReportList << QStringLiteral( "Convert to single part feature" ); + QTest::newRow( "Polygons" ) + << new QgsVectorLayer( testDataDir.absoluteFilePath( "polygon_layer.shp" ), QStringLiteral( "polygon" ), QStringLiteral( "ogr" ) ) + << new QgsVectorLayer( mDataDir.absoluteFilePath( "strict_multipart.gpkg|layername=polygons_to_fix" ), QStringLiteral( "polygons fo fix" ), QStringLiteral( "ogr" ) ) + << polygonsReportList; +} + +void TestQgsProcessingFixGeometry::fixMultipartAlg() +{ + QFETCH( QgsVectorLayer *, sourceLayer ); + QFETCH( QgsVectorLayer *, errorsLayer ); + QFETCH( QStringList, reportList ); + + QVERIFY( sourceLayer->isValid() ); + QVERIFY( errorsLayer->isValid() ); + + const std::unique_ptr alg( + QgsApplication::processingRegistry()->createAlgorithmById( QStringLiteral( "native:fixgeometrymultipart" ) ) + ); + QVERIFY( alg != nullptr ); + + QVariantMap parameters; + parameters.insert( QStringLiteral( "INPUT" ), QVariant::fromValue( QgsProcessingFeatureSourceDefinition( sourceLayer->source() ) ) ); + parameters.insert( QStringLiteral( "ERRORS" ), QVariant::fromValue( QgsProcessingFeatureSourceDefinition( errorsLayer->source() ) ) ); + parameters.insert( QStringLiteral( "OUTPUT" ), QgsProcessing::TEMPORARY_OUTPUT ); + parameters.insert( QStringLiteral( "REPORT" ), QgsProcessing::TEMPORARY_OUTPUT ); + parameters.insert( QStringLiteral( "UNIQUE_ID" ), "id" ); + + bool ok = false; + QgsProcessingFeedback feedback; + auto context = std::make_unique(); + + QVariantMap results; + results = alg->run( parameters, *context, &feedback, &ok ); + QVERIFY( ok ); + + std::unique_ptr outputLayer( qobject_cast( context->getMapLayer( results.value( QStringLiteral( "OUTPUT" ) ).toString() ) ) ); + std::unique_ptr reportLayer( qobject_cast( context->getMapLayer( results.value( QStringLiteral( "REPORT" ) ).toString() ) ) ); + QVERIFY( reportLayer->isValid() ); + QVERIFY( outputLayer->isValid() ); + + QCOMPARE( outputLayer->featureCount(), sourceLayer->featureCount() ); + QCOMPARE( reportLayer->featureCount(), reportList.count() ); + int idx = 1; + for ( const QString &expectedReport : reportList ) + { + const QgsFeature reportFeature = reportLayer->getFeature( idx ); + QCOMPARE( reportFeature.attribute( "report" ), expectedReport ); + idx++; + } + + // Verification of multipart type + QSet singleTypeIds = reportLayer->uniqueValues( reportLayer->fields().indexFromName( QStringLiteral( "id" ) ) ); + QgsFeatureIterator it = outputLayer->getFeatures(); + QgsFeature feat; + while ( it.nextFeature( feat ) ) + QCOMPARE( QgsWkbTypes::isSingleType( feat.geometry().wkbType() ), singleTypeIds.contains( feat.attribute( QStringLiteral( "id" ) ) ) ); +} + void TestQgsProcessingFixGeometry::fixAreaAlg_data() { //create a line layer that will be used in tests diff --git a/tests/testdata/geometry_fix/strict_multipart.gpkg b/tests/testdata/geometry_fix/strict_multipart.gpkg new file mode 100644 index 00000000000..d84ad891b83 Binary files /dev/null and b/tests/testdata/geometry_fix/strict_multipart.gpkg differ