From c075f2a490374d7dbed4ef68da37d85f3517dabf Mon Sep 17 00:00:00 2001 From: Jacky Volpes Date: Fri, 23 Feb 2024 16:02:58 +0100 Subject: [PATCH] Processing: Add Check Geometry algorithm and test: Hole Porting hole check from geometry checker to processings --- src/analysis/CMakeLists.txt | 1 + .../qgsalgorithmcheckgeometryhole.cpp | 206 ++++++++++++++++++ .../qgsalgorithmcheckgeometryhole.h | 55 +++++ .../processing/qgsnativealgorithms.cpp | 2 + .../testqgsprocessingcheckgeometry.cpp | 30 +++ 5 files changed, 294 insertions(+) create mode 100644 src/analysis/processing/qgsalgorithmcheckgeometryhole.cpp create mode 100644 src/analysis/processing/qgsalgorithmcheckgeometryhole.h diff --git a/src/analysis/CMakeLists.txt b/src/analysis/CMakeLists.txt index 9000e699854..ea46877cbad 100644 --- a/src/analysis/CMakeLists.txt +++ b/src/analysis/CMakeLists.txt @@ -63,6 +63,7 @@ set(QGIS_ANALYSIS_SRCS processing/qgsalgorithmcentroid.cpp processing/qgsalgorithmcheckgeometryangle.cpp processing/qgsalgorithmfixgeometryangle.cpp + processing/qgsalgorithmcheckgeometryhole.cpp processing/qgsalgorithmcheckgeometryarea.cpp processing/qgsalgorithmfixgeometryarea.cpp processing/qgsalgorithmclip.cpp diff --git a/src/analysis/processing/qgsalgorithmcheckgeometryhole.cpp b/src/analysis/processing/qgsalgorithmcheckgeometryhole.cpp new file mode 100644 index 00000000000..42e6a414504 --- /dev/null +++ b/src/analysis/processing/qgsalgorithmcheckgeometryhole.cpp @@ -0,0 +1,206 @@ +/*************************************************************************** + qgsalgorithmcheckgeometryhole.cpp + --------------------- + begin : July 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 "qgsalgorithmcheckgeometryhole.h" +#include "qgsgeometrycheckcontext.h" +#include "qgsgeometrycheckerror.h" +#include "qgsgeometryholecheck.h" +#include "qgspoint.h" +#include "qgsvectorlayer.h" +#include "qgsvectordataproviderfeaturepool.h" + +///@cond PRIVATE + +auto QgsGeometryCheckHoleAlgorithm::name() const -> QString +{ + return QStringLiteral( "checkgeometryhole" ); +} + +auto QgsGeometryCheckHoleAlgorithm::displayName() const -> QString +{ + return QObject::tr( "Check geometry (Hole)" ); +} + +auto QgsGeometryCheckHoleAlgorithm::tags() const -> QStringList +{ + return QObject::tr( "check,geometry,hole" ).split( ',' ); +} + +auto QgsGeometryCheckHoleAlgorithm::group() const -> QString +{ + return QObject::tr( "Check geometry" ); +} + +auto QgsGeometryCheckHoleAlgorithm::groupId() const -> QString +{ + return QStringLiteral( "checkgeometry" ); +} + +auto QgsGeometryCheckHoleAlgorithm::shortHelpString() const -> QString +{ + return QObject::tr( "This algorithm check the holes of polygonal geometry." ); +} + +auto QgsGeometryCheckHoleAlgorithm::flags() const -> Qgis::ProcessingAlgorithmFlags +{ + return QgsProcessingAlgorithm::flags() | Qgis::ProcessingAlgorithmFlag::NoThreading; +} + +auto QgsGeometryCheckHoleAlgorithm::createInstance() const -> QgsGeometryCheckHoleAlgorithm * +{ + return new QgsGeometryCheckHoleAlgorithm(); +} + +void QgsGeometryCheckHoleAlgorithm::initAlgorithm( const QVariantMap &configuration ) +{ + Q_UNUSED( configuration ) + + // inputs + addParameter( + new QgsProcessingParameterFeatureSource( + QStringLiteral( "INPUT" ), QObject::tr( "Input layer" ), + QList() << static_cast( Qgis::ProcessingSourceType::VectorPolygon ) + ) + ); + addParameter( new QgsProcessingParameterField( QStringLiteral( "UNIQUE_ID" ), QObject::tr( "Unique feature identifier" ), QString(), QStringLiteral( "INPUT" ) ) ); + + // outputs + addParameter( new QgsProcessingParameterFeatureSink( QStringLiteral( "ERRORS" ), QObject::tr( "Errors layer" ), Qgis::ProcessingSourceType::VectorPoint ) ); + addParameter( new QgsProcessingParameterFeatureSink( QStringLiteral( "OUTPUT" ), QObject::tr( "Output layer" ), Qgis::ProcessingSourceType::VectorPolygon ) ); + + 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 QgsGeometryCheckHoleAlgorithm::prepareAlgorithm( const QVariantMap ¶meters, QgsProcessingContext &context, QgsProcessingFeedback * ) -> bool +{ + mTolerance = parameterAsInt( parameters, QStringLiteral( "TOLERANCE" ), context ); + + return true; +} + +auto QgsGeometryCheckHoleAlgorithm::outputFields() -> QgsFields +{ + QgsFields fields; + fields.append( QgsField( QStringLiteral( "gc_layerid" ), QMetaType::QString ) ); + fields.append( QgsField( QStringLiteral( "gc_layername" ), QMetaType::QString ) ); + fields.append( QgsField( QStringLiteral( "gc_partidx" ), QMetaType::Int ) ); + fields.append( QgsField( QStringLiteral( "gc_ringidx" ), QMetaType::Int ) ); + fields.append( QgsField( QStringLiteral( "gc_vertidx" ), QMetaType::Int ) ); + fields.append( QgsField( QStringLiteral( "gc_errorx" ), QMetaType::Double ) ); + fields.append( QgsField( QStringLiteral( "gc_errory" ), QMetaType::Double ) ); + fields.append( QgsField( QStringLiteral( "gc_error" ), QMetaType::QString ) ); + return fields; +} + + +auto QgsGeometryCheckHoleAlgorithm::processAlgorithm( const QVariantMap ¶meters, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) -> QVariantMap +{ + QString dest_output; + QString dest_errors; + QgsProcessingFeatureSource *input = parameterAsSource( parameters, QStringLiteral( "INPUT" ), context ); + + QString uniqueIdFieldName( parameterAsString( parameters, QStringLiteral( "UNIQUE_ID" ), context ) ); + int uniqueIdFieldIdx = input->fields().indexFromName( uniqueIdFieldName ); + if ( uniqueIdFieldIdx == -1 ) + throw QgsProcessingException( QObject::tr( "Missing field %1 in input layer" ).arg( uniqueIdFieldName ) ); + + const QgsField uniqueIdField = input->fields().at( uniqueIdFieldIdx ); + + QgsFields fields = outputFields(); + fields.append( uniqueIdField ); + + const std::unique_ptr< QgsFeatureSink > sink_output( parameterAsSink( parameters, QStringLiteral( "OUTPUT" ), context, dest_output, fields, input->wkbType(), input->sourceCrs() ) ); + if ( !sink_output ) + throw QgsProcessingException( invalidSinkError( parameters, QStringLiteral( "OUTPUT" ) ) ); + + const std::unique_ptr< QgsFeatureSink > sink_errors( parameterAsSink( parameters, QStringLiteral( "ERRORS" ), context, dest_errors, fields, Qgis::WkbType::Point, input->sourceCrs() ) ); + if ( !sink_errors ) + throw QgsProcessingException( invalidSinkError( parameters, QStringLiteral( "ERRORS" ) ) ); + + QgsProcessingMultiStepFeedback multiStepFeedback( 3, feedback ); + + QgsProject *project = QgsProject::instance(); + + const std::unique_ptr checkContext = std::make_unique( mTolerance, input->sourceCrs(), project->transformContext(), project ); + + // Test detection + QList checkErrors; + QStringList messages; + + const QgsGeometryHoleCheck check( checkContext.get(), QVariantMap() ); + + multiStepFeedback.setCurrentStep( 1 ); + feedback->setProgressText( QObject::tr( "Preparing features…" ) ); + QMap featurePools; + + QgsVectorLayer *inputLayer = input->materialize( QgsFeatureRequest() ); + featurePools.insert( inputLayer->id(), new QgsVectorDataProviderFeaturePool( inputLayer ) ); + + multiStepFeedback.setCurrentStep( 2 ); + feedback->setProgressText( QObject::tr( "Collecting errors…" ) ); + check.collectErrors( featurePools, checkErrors, messages, feedback ); + + multiStepFeedback.setCurrentStep( 3 ); + feedback->setProgressText( QObject::tr( "Exporting errors…" ) ); + const double step{checkErrors.size() > 0 ? 100.0 / checkErrors.size() : 1}; + long i = 0; + feedback->setProgress( 0.0 ); + + for ( QgsGeometryCheckError *error : checkErrors ) + { + + if ( feedback->isCanceled() ) + { + break; + } + QgsFeature f; + QgsAttributes attrs = f.attributes(); + + attrs << error->layerId() + << error->featureId() + << error->vidx().part + << error->vidx().ring + << error->vidx().vertex + << error->location().x() + << error->location().y() + << error->value().toString() + << inputLayer->getFeature( error->featureId() ).attribute( uniqueIdField.name() ); + f.setAttributes( attrs ); + + f.setGeometry( error->geometry() ); + if ( !sink_output->addFeature( f, QgsFeatureSink::FastInsert ) ) + throw QgsProcessingException( writeFeatureError( sink_output.get(), parameters, QStringLiteral( "OUTPUT" ) ) ); + + f.setGeometry( QgsGeometry::fromPoint( QgsPoint( error->location().x(), error->location().y() ) ) ); + if ( !sink_errors->addFeature( f, QgsFeatureSink::FastInsert ) ) + throw QgsProcessingException( writeFeatureError( sink_errors.get(), parameters, QStringLiteral( "ERRORS" ) ) ); + + i++; + feedback->setProgress( 100.0 * step * static_cast( i ) ); + } + + QVariantMap outputs; + outputs.insert( QStringLiteral( "OUTPUT" ), dest_output ); + outputs.insert( QStringLiteral( "ERRORS" ), dest_errors ); + + return outputs; +} + +///@endcond diff --git a/src/analysis/processing/qgsalgorithmcheckgeometryhole.h b/src/analysis/processing/qgsalgorithmcheckgeometryhole.h new file mode 100644 index 00000000000..eb2a6bafbfa --- /dev/null +++ b/src/analysis/processing/qgsalgorithmcheckgeometryhole.h @@ -0,0 +1,55 @@ +/*************************************************************************** + qgsalgorithmcheckgeometryhole.h + --------------------- + begin : July 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 QGSALGORITHMCHECKGEOMETRYHOLE_H +#define QGSALGORITHMCHECKGEOMETRYHOLE_H + +#define SIP_NO_FILE + +#include "qgis_sip.h" +#include "qgsprocessingalgorithm.h" + +///@cond PRIVATE + +class QgsGeometryCheckHoleAlgorithm : public QgsProcessingAlgorithm +{ + public: + + QgsGeometryCheckHoleAlgorithm() = 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; + QgsGeometryCheckHoleAlgorithm *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; + static QgsFields outputFields(); + + private: + int mTolerance{8}; +}; + +///@endcond PRIVATE + +#endif // QGSALGORITHMCHECKGEOMETRYHOLE_H diff --git a/src/analysis/processing/qgsnativealgorithms.cpp b/src/analysis/processing/qgsnativealgorithms.cpp index 184a771f078..e853f84ef11 100644 --- a/src/analysis/processing/qgsnativealgorithms.cpp +++ b/src/analysis/processing/qgsnativealgorithms.cpp @@ -46,6 +46,7 @@ #include "qgsalgorithmcheckgeometryangle.h" #include "qgsalgorithmcheckgeometryarea.h" #include "qgsalgorithmfixgeometryarea.h" +#include "qgsalgorithmcheckgeometryhole.h" #include "qgsalgorithmclip.h" #include "qgsalgorithmconcavehull.h" #include "qgsalgorithmconditionalbranch.h" @@ -329,6 +330,7 @@ void QgsNativeAlgorithms::loadAlgorithms() addAlgorithm( new QgsCentroidAlgorithm() ); addAlgorithm( new QgsGeometryCheckAngleAlgorithm() ); addAlgorithm( new QgsGeometryCheckAreaAlgorithm() ); + addAlgorithm( new QgsGeometryCheckHoleAlgorithm() ); addAlgorithm( new QgsClipAlgorithm() ); addAlgorithm( new QgsCollectAlgorithm() ); addAlgorithm( new QgsCombineStylesAlgorithm() ); diff --git a/tests/src/analysis/testqgsprocessingcheckgeometry.cpp b/tests/src/analysis/testqgsprocessingcheckgeometry.cpp index 52639e52ab4..c5d6a0b0cc4 100644 --- a/tests/src/analysis/testqgsprocessingcheckgeometry.cpp +++ b/tests/src/analysis/testqgsprocessingcheckgeometry.cpp @@ -37,6 +37,7 @@ class TestQgsProcessingCheckGeometry: public QgsTest void angleAlg(); void areaAlg(); + void holeAlg(); private: @@ -156,5 +157,34 @@ void TestQgsProcessingCheckGeometry::areaAlg() QCOMPARE( errorsLayer->featureCount(), 8 ); } +void TestQgsProcessingCheckGeometry::holeAlg() +{ + const std::unique_ptr< QgsProcessingAlgorithm > alg( + QgsApplication::processingRegistry()->createAlgorithmById( QStringLiteral( "native:checkgeometryhole" ) ) + ); + QVERIFY( alg != nullptr ); + + QVariantMap parameters; + parameters.insert( QStringLiteral( "INPUT" ), QVariant::fromValue( mPolygonLayer ) ); + parameters.insert( QStringLiteral( "UNIQUE_ID" ), "id" ); + parameters.insert( QStringLiteral( "OUTPUT" ), QgsProcessing::TEMPORARY_OUTPUT ); + parameters.insert( QStringLiteral( "ERRORS" ), QgsProcessing::TEMPORARY_OUTPUT ); + + bool ok = false; + QgsProcessingFeedback feedback; + const std::unique_ptr< QgsProcessingContext > context = std::make_unique< QgsProcessingContext >(); + + QVariantMap results; + results = alg->run( parameters, *context, &feedback, &ok ); + QVERIFY( ok ); + + std::unique_ptr outputLayer( qobject_cast< QgsVectorLayer * >( context->getMapLayer( results.value( QStringLiteral( "OUTPUT" ) ).toString() ) ) ); + std::unique_ptr errorsLayer( qobject_cast< QgsVectorLayer * >( context->getMapLayer( results.value( QStringLiteral( "ERRORS" ) ).toString() ) ) ); + QVERIFY( outputLayer->isValid() ); + QVERIFY( errorsLayer->isValid() ); + QCOMPARE( outputLayer->featureCount(), 1 ); + QCOMPARE( errorsLayer->featureCount(), 1 ); +} + QGSTEST_MAIN( TestQgsProcessingCheckGeometry ) #include "testqgsprocessingcheckgeometry.moc"