From e2ce3826621326d152a4a293bfcbdbe4c4bbb08b Mon Sep 17 00:00:00 2001 From: Jacky Volpes Date: Fri, 23 Feb 2024 17:34:43 +0100 Subject: [PATCH] Processing: Add Check Geometry algorithm and test: Missing vertex Porting missing vertex check from geometry checker to processings --- src/analysis/CMakeLists.txt | 1 + ...qgsalgorithmcheckgeometrymissingvertex.cpp | 205 ++++++++++++++++++ .../qgsalgorithmcheckgeometrymissingvertex.h | 55 +++++ .../processing/qgsnativealgorithms.cpp | 2 + .../testqgsprocessingcheckgeometry.cpp | 33 +++ 5 files changed, 296 insertions(+) create mode 100644 src/analysis/processing/qgsalgorithmcheckgeometrymissingvertex.cpp create mode 100644 src/analysis/processing/qgsalgorithmcheckgeometrymissingvertex.h diff --git a/src/analysis/CMakeLists.txt b/src/analysis/CMakeLists.txt index 1b96e68884b..f9ae6c41e84 100644 --- a/src/analysis/CMakeLists.txt +++ b/src/analysis/CMakeLists.txt @@ -65,6 +65,7 @@ set(QGIS_ANALYSIS_SRCS processing/qgsalgorithmfixgeometryangle.cpp processing/qgsalgorithmcheckgeometryhole.cpp processing/qgsalgorithmfixgeometryhole.cpp + processing/qgsalgorithmcheckgeometrymissingvertex.cpp processing/qgsalgorithmcheckgeometryarea.cpp processing/qgsalgorithmfixgeometryarea.cpp processing/qgsalgorithmclip.cpp diff --git a/src/analysis/processing/qgsalgorithmcheckgeometrymissingvertex.cpp b/src/analysis/processing/qgsalgorithmcheckgeometrymissingvertex.cpp new file mode 100644 index 00000000000..fa9a2843e49 --- /dev/null +++ b/src/analysis/processing/qgsalgorithmcheckgeometrymissingvertex.cpp @@ -0,0 +1,205 @@ +/*************************************************************************** + qgsalgorithmcheckgeometrymissingvertex.cpp + --------------------- + begin : February 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 "qgsalgorithmcheckgeometrymissingvertex.h" +#include "qgsgeometrycheckcontext.h" +#include "qgsgeometrycheckerror.h" +#include "qgsgeometrymissingvertexcheck.h" +#include "qgspoint.h" +#include "qgsvectorlayer.h" +#include "qgsvectordataproviderfeaturepool.h" + +///@cond PRIVATE + +auto QgsGeometryCheckMissingVertexAlgorithm::name() const -> QString +{ + return QStringLiteral( "checkgeometrymissingvertex" ); +} + +auto QgsGeometryCheckMissingVertexAlgorithm::displayName() const -> QString +{ + return QObject::tr( "Check geometry (Missing Vertex)" ); +} + +auto QgsGeometryCheckMissingVertexAlgorithm::tags() const -> QStringList +{ + return QObject::tr( "check,geometry,missing,vertex" ).split( ',' ); +} + +auto QgsGeometryCheckMissingVertexAlgorithm::group() const -> QString +{ + return QObject::tr( "Check geometry" ); +} + +auto QgsGeometryCheckMissingVertexAlgorithm::groupId() const -> QString +{ + return QStringLiteral( "checkgeometry" ); +} + +auto QgsGeometryCheckMissingVertexAlgorithm::shortHelpString() const -> QString +{ + return QObject::tr( "This algorithm checks the missing vertices along polygons junctions." ); +} + +auto QgsGeometryCheckMissingVertexAlgorithm::flags() const -> Qgis::ProcessingAlgorithmFlags +{ + return QgsProcessingAlgorithm::flags() | Qgis::ProcessingAlgorithmFlag::NoThreading; +} + +auto QgsGeometryCheckMissingVertexAlgorithm::createInstance() const -> QgsGeometryCheckMissingVertexAlgorithm * +{ + return new QgsGeometryCheckMissingVertexAlgorithm(); +} + +void QgsGeometryCheckMissingVertexAlgorithm::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 QgsGeometryCheckMissingVertexAlgorithm::prepareAlgorithm( const QVariantMap ¶meters, QgsProcessingContext &context, QgsProcessingFeedback * ) -> bool +{ + mTolerance = parameterAsInt( parameters, QStringLiteral( "TOLERANCE" ), context ); + + return true; +} + +auto QgsGeometryCheckMissingVertexAlgorithm::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 QgsGeometryCheckMissingVertexAlgorithm::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 QgsGeometryMissingVertexCheck 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() + << inputLayer->name() + << 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/qgsalgorithmcheckgeometrymissingvertex.h b/src/analysis/processing/qgsalgorithmcheckgeometrymissingvertex.h new file mode 100644 index 00000000000..7139e09dd11 --- /dev/null +++ b/src/analysis/processing/qgsalgorithmcheckgeometrymissingvertex.h @@ -0,0 +1,55 @@ +/*************************************************************************** + qgsalgorithmcheckgeometrymissingvertex.h + --------------------- + begin : February 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 QGSALGORITHMCHECKGEOMETRYMISSINGVERTEX_H +#define QGSALGORITHMCHECKGEOMETRYMISSINGVERTEX_H + +#define SIP_NO_FILE + +#include "qgis_sip.h" +#include "qgsprocessingalgorithm.h" + +///@cond PRIVATE + +class QgsGeometryCheckMissingVertexAlgorithm : public QgsProcessingAlgorithm +{ + public: + + QgsGeometryCheckMissingVertexAlgorithm() = 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; + QgsGeometryCheckMissingVertexAlgorithm *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 // QGSALGORITHMCHECKGEOMETRYMISSINGVERTEX_H diff --git a/src/analysis/processing/qgsnativealgorithms.cpp b/src/analysis/processing/qgsnativealgorithms.cpp index e80e1a555d9..0239c9394d7 100644 --- a/src/analysis/processing/qgsnativealgorithms.cpp +++ b/src/analysis/processing/qgsnativealgorithms.cpp @@ -48,6 +48,7 @@ #include "qgsalgorithmfixgeometryarea.h" #include "qgsalgorithmfixgeometryhole.h" #include "qgsalgorithmcheckgeometryhole.h" +#include "qgsalgorithmcheckgeometrymissingvertex.h" #include "qgsalgorithmclip.h" #include "qgsalgorithmconcavehull.h" #include "qgsalgorithmconditionalbranch.h" @@ -332,6 +333,7 @@ void QgsNativeAlgorithms::loadAlgorithms() addAlgorithm( new QgsGeometryCheckAngleAlgorithm() ); addAlgorithm( new QgsGeometryCheckAreaAlgorithm() ); addAlgorithm( new QgsGeometryCheckHoleAlgorithm() ); + addAlgorithm( new QgsGeometryCheckMissingVertexAlgorithm() ); addAlgorithm( new QgsClipAlgorithm() ); addAlgorithm( new QgsCollectAlgorithm() ); addAlgorithm( new QgsCombineStylesAlgorithm() ); diff --git a/tests/src/analysis/testqgsprocessingcheckgeometry.cpp b/tests/src/analysis/testqgsprocessingcheckgeometry.cpp index c5d6a0b0cc4..8882aab20c2 100644 --- a/tests/src/analysis/testqgsprocessingcheckgeometry.cpp +++ b/tests/src/analysis/testqgsprocessingcheckgeometry.cpp @@ -38,6 +38,7 @@ class TestQgsProcessingCheckGeometry: public QgsTest void areaAlg(); void holeAlg(); + void missingVertexAlg(); private: @@ -186,5 +187,37 @@ void TestQgsProcessingCheckGeometry::holeAlg() QCOMPARE( errorsLayer->featureCount(), 1 ); } +void TestQgsProcessingCheckGeometry::missingVertexAlg() +{ + const std::unique_ptr< QgsProcessingAlgorithm > alg( + QgsApplication::processingRegistry()->createAlgorithmById( QStringLiteral( "native:checkgeometrymissingvertex" ) ) + ); + QVERIFY( alg != nullptr ); + + const QDir testDataDir( QDir( TEST_DATA_DIR ).absoluteFilePath( "geometry_checker" ) ); + QgsVectorLayer *missingVertexLayer = new QgsVectorLayer( testDataDir.absoluteFilePath( "missing_vertex.gpkg" ), QStringLiteral( "polygons" ), QStringLiteral( "ogr" ) ); + + QVariantMap parameters; + parameters.insert( QStringLiteral( "INPUT" ), QVariant::fromValue( missingVertexLayer ) ); + 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 ); + + const std::unique_ptr outputLayer( qobject_cast< QgsVectorLayer * >( context->getMapLayer( results.value( QStringLiteral( "OUTPUT" ) ).toString() ) ) ); + const std::unique_ptr errorsLayer( qobject_cast< QgsVectorLayer * >( context->getMapLayer( results.value( QStringLiteral( "ERRORS" ) ).toString() ) ) ); + QVERIFY( outputLayer->isValid() ); + QVERIFY( errorsLayer->isValid() ); + QCOMPARE( outputLayer->featureCount(), 5 ); + QCOMPARE( errorsLayer->featureCount(), 5 ); +} + QGSTEST_MAIN( TestQgsProcessingCheckGeometry ) #include "testqgsprocessingcheckgeometry.moc"