From 52cbd4b7f4727e625b34aef4d2d590158b345b40 Mon Sep 17 00:00:00 2001 From: Jacky Volpes Date: Fri, 12 Jan 2024 12:08:38 +0100 Subject: [PATCH] Processing: Add Check Geometry algorithm and test: Line layer intersection --- src/analysis/CMakeLists.txt | 1 + ...ithmcheckgeometrylinelayerintersection.cpp | 215 ++++++++++++++++++ ...orithmcheckgeometrylinelayerintersection.h | 60 +++++ .../processing/qgsnativealgorithms.cpp | 2 + .../testqgsprocessingcheckgeometry.cpp | 31 +++ 5 files changed, 309 insertions(+) create mode 100644 src/analysis/processing/qgsalgorithmcheckgeometrylinelayerintersection.cpp create mode 100644 src/analysis/processing/qgsalgorithmcheckgeometrylinelayerintersection.h diff --git a/src/analysis/CMakeLists.txt b/src/analysis/CMakeLists.txt index 754f1d9d6cdf..ef969b6c931b 100644 --- a/src/analysis/CMakeLists.txt +++ b/src/analysis/CMakeLists.txt @@ -66,6 +66,7 @@ set(QGIS_ANALYSIS_SRCS processing/qgsalgorithmcheckgeometrymultipart.cpp processing/qgsalgorithmcheckgeometryangle.cpp processing/qgsalgorithmcheckgeometryselfintersection.cpp + processing/qgsalgorithmcheckgeometrylinelayerintersection.cpp processing/qgsalgorithmclip.cpp processing/qgsalgorithmconcavehull.cpp processing/qgsalgorithmconditionalbranch.cpp diff --git a/src/analysis/processing/qgsalgorithmcheckgeometrylinelayerintersection.cpp b/src/analysis/processing/qgsalgorithmcheckgeometrylinelayerintersection.cpp new file mode 100644 index 000000000000..f6eb14aed789 --- /dev/null +++ b/src/analysis/processing/qgsalgorithmcheckgeometrylinelayerintersection.cpp @@ -0,0 +1,215 @@ +/*************************************************************************** + qgsalgorithmcheckgeometrylinelayerintersection.cpp + --------------------- + begin : January 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 "qgsalgorithmcheckgeometrylinelayerintersection.h" +#include "qgsgeometrycheckcontext.h" +#include "qgsgeometrycheckerror.h" +#include "qgsgeometrylinelayerintersectioncheck.h" +#include "qgspoint.h" +#include "qgsvectorlayer.h" +#include "qgsvectordataproviderfeaturepool.h" + +///@cond PRIVATE + +auto QgsGeometryCheckLineLayerIntersectionAlgorithm::name() const -> QString +{ + return QStringLiteral( "checkgeometrylinelayerintersection" ); +} + +auto QgsGeometryCheckLineLayerIntersectionAlgorithm::displayName() const -> QString +{ + return QObject::tr( "Check Geometry (Line layer intersection)" ); +} + +auto QgsGeometryCheckLineLayerIntersectionAlgorithm::tags() const -> QStringList +{ + return QObject::tr( "check,geometry,line,layer,intersection" ).split( ',' ); +} + +auto QgsGeometryCheckLineLayerIntersectionAlgorithm::group() const -> QString +{ + return QObject::tr( "Check geometry" ); +} + +auto QgsGeometryCheckLineLayerIntersectionAlgorithm::groupId() const -> QString +{ + return QStringLiteral( "checkgeometry" ); +} + +auto QgsGeometryCheckLineLayerIntersectionAlgorithm::shortHelpString() const -> QString +{ + return QObject::tr( "This algorithm check the line intersection of geometry (linestring) with another layer." ); +} + +auto QgsGeometryCheckLineLayerIntersectionAlgorithm::flags() const -> QgsProcessingAlgorithm::Flags +{ + return QgsProcessingAlgorithm::flags() | QgsProcessingAlgorithm::FlagNoThreading; +} + +auto QgsGeometryCheckLineLayerIntersectionAlgorithm::createInstance() const -> QgsGeometryCheckLineLayerIntersectionAlgorithm * +{ + return new QgsGeometryCheckLineLayerIntersectionAlgorithm(); +} + +void QgsGeometryCheckLineLayerIntersectionAlgorithm::initAlgorithm( const QVariantMap &configuration ) +{ + Q_UNUSED( configuration ) + + addParameter( new QgsProcessingParameterFeatureSource( QStringLiteral( "INPUT" ), QObject::tr( "Input layer" ), QList< int >() << QgsProcessing::TypeVectorLine ) ); + addParameter( new QgsProcessingParameterFeatureSource( QStringLiteral( "INTERSECTION_LAYER" ), QObject::tr( "Intersection layer" ), QList< int >() << QgsProcessing::TypeVectorLine << QgsProcessing:: TypeVectorPolygon ) ); + addParameter( new QgsProcessingParameterFeatureSink( QStringLiteral( "ERRORS" ), QObject::tr( "Errors layer" ), QgsProcessing::TypeVectorPoint ) ); + addParameter( new QgsProcessingParameterFeatureSink( QStringLiteral( "OUTPUT" ), QObject::tr( "Output layer" ), QgsProcessing::TypeVectorLine ) ); + + std::unique_ptr< QgsProcessingParameterNumber > tolerance = std::make_unique< QgsProcessingParameterNumber >( QStringLiteral( "TOLERANCE" ), + QObject::tr( "Tolerance" ), QgsProcessingParameterNumber::Integer, 8, false, 1, 13 ); + tolerance->setFlags( tolerance->flags() | QgsProcessingParameterDefinition::FlagAdvanced ); + addParameter( tolerance.release() ); + +} + +auto QgsGeometryCheckLineLayerIntersectionAlgorithm::prepareAlgorithm( const QVariantMap ¶meters, QgsProcessingContext &context, QgsProcessingFeedback * ) -> bool +{ + mTolerance = parameterAsInt( parameters, QStringLiteral( "TOLERANCE" ), context ); + + return true; +} + +auto QgsGeometryCheckLineLayerIntersectionAlgorithm::createFeaturePool( QgsVectorLayer *layer, bool selectedOnly ) const -> QgsFeaturePool * +{ + + return new QgsVectorDataProviderFeaturePool( layer, selectedOnly ); +} + +static auto outputFields( ) -> QgsFields +{ + QgsFields fields; + fields.append( QgsField( QStringLiteral( "gc_layerid" ), QVariant::String ) ); + fields.append( QgsField( QStringLiteral( "gc_layername" ), QVariant::String ) ); + fields.append( QgsField( QStringLiteral( "gc_featid" ), QVariant::Int ) ); + fields.append( QgsField( QStringLiteral( "gc_partidx" ), QVariant::Int ) ); + fields.append( QgsField( QStringLiteral( "gc_ringidx" ), QVariant::Int ) ); + fields.append( QgsField( QStringLiteral( "gc_vertidx" ), QVariant::Int ) ); + fields.append( QgsField( QStringLiteral( "gc_errorx" ), QVariant::Double ) ); + fields.append( QgsField( QStringLiteral( "gc_errory" ), QVariant::Double ) ); + fields.append( QgsField( QStringLiteral( "gc_error" ), QVariant::String ) ); + return fields; +} + + +auto QgsGeometryCheckLineLayerIntersectionAlgorithm::processAlgorithm( const QVariantMap ¶meters, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) -> QVariantMap +{ + QString dest_output; + QString dest_errors; + + std::unique_ptr< QgsProcessingFeatureSource > source( parameterAsSource( parameters, QStringLiteral( "INPUT" ), context ) ); + if ( !source ) + throw QgsProcessingException( invalidSourceError( parameters, QStringLiteral( "INPUT" ) ) ); + + std::unique_ptr< QgsProcessingFeatureSource > intersection_layer( parameterAsSource( parameters, QStringLiteral( "INTERSECTION_LAYER" ), context ) ); + if ( !intersection_layer ) + throw QgsProcessingException( invalidSourceError( parameters, QStringLiteral( "INTERSECTION_LAYER" ) ) ); + + mInputLayer.reset( source->materialize( QgsFeatureRequest() ) ); + mIntersectionLayer.reset( intersection_layer->materialize( QgsFeatureRequest() ) ); + + if ( !mInputLayer.get() ) + throw QgsProcessingException( QObject::tr( "Could not load source layer for INPUT" ) ); + + if ( !mIntersectionLayer.get() ) + throw QgsProcessingException( QObject::tr( "Could not load source layer for INTERSECTION_LAYER" ) ); + + QgsFields fields = outputFields(); + + std::unique_ptr< QgsFeatureSink > sink_output( parameterAsSink( parameters, QStringLiteral( "OUTPUT" ), context, dest_output, fields, mInputLayer->wkbType(), mInputLayer->sourceCrs() ) ); + if ( !sink_output ) + throw QgsProcessingException( invalidSinkError( parameters, QStringLiteral( "OUTPUT" ) ) ); + + std::unique_ptr< QgsFeatureSink > sink_errors( parameterAsSink( parameters, QStringLiteral( "ERRORS" ), context, dest_errors, fields, Qgis::WkbType::Point, mInputLayer->sourceCrs() ) ); + if ( !sink_errors ) + throw QgsProcessingException( invalidSinkError( parameters, QStringLiteral( "ERRORS" ) ) ); + + QgsProcessingMultiStepFeedback multiStepFeedback( 3, feedback ); + + QgsProject *project = mInputLayer->project() ? mInputLayer->project() : QgsProject::instance(); + std::unique_ptr checkContext = std::make_unique( mTolerance, mInputLayer->sourceCrs(), project->transformContext(), project ); + + // Test detection + QList checkErrors; + QStringList messages; + + QVariantMap configurationCheck; + configurationCheck.insert( "checkLayer", mIntersectionLayer->id() ); + const QgsGeometryLineLayerIntersectionCheck check( checkContext.get(), configurationCheck ); + + multiStepFeedback.setCurrentStep( 1 ); + feedback->setProgressText( QObject::tr( "Preparing features…" ) ); + QMap featurePools; + featurePools.insert( mInputLayer->id(), createFeaturePool( mInputLayer.get() ) ); + featurePools.insert( mIntersectionLayer->id(), createFeaturePool( mIntersectionLayer.get() ) ); + + multiStepFeedback.setCurrentStep( 2 ); + feedback->setProgressText( QObject::tr( "Collecting errors…" ) ); + check.collectErrors( featurePools, checkErrors, messages, feedback ); + + multiStepFeedback.setCurrentStep( 3 ); + feedback->setProgressText( QObject::tr( "Exporting errors…" ) ); + 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() + << mInputLayer->name() + << error->featureId() + << error->vidx().part + << error->vidx().ring + << error->vidx().vertex + << error->location().x() + << error->location().y() + << error->value().toString(); + 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/qgsalgorithmcheckgeometrylinelayerintersection.h b/src/analysis/processing/qgsalgorithmcheckgeometrylinelayerintersection.h new file mode 100644 index 000000000000..2b24967aa805 --- /dev/null +++ b/src/analysis/processing/qgsalgorithmcheckgeometrylinelayerintersection.h @@ -0,0 +1,60 @@ +/*************************************************************************** + qgsalgorithmcheckgeometrylinelayerintersection.h + --------------------- + begin : January 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 QGSALGORITHMCHECKGEOMETRYLINELAYERINTERSECTION_H +#define QGSALGORITHMCHECKGEOMETRYLINELAYERINTERSECTION_H + +#define SIP_NO_FILE + +#include "qgis_sip.h" +#include "qgsprocessingalgorithm.h" +#include "qgsfeaturepool.h" + +///@cond PRIVATE + +class QgsGeometryCheckLineLayerIntersectionAlgorithm : public QgsProcessingAlgorithm +{ + public: + + QgsGeometryCheckLineLayerIntersectionAlgorithm() = 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; + Flags flags() const override; + QgsGeometryCheckLineLayerIntersectionAlgorithm *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: + QgsFeaturePool *createFeaturePool( QgsVectorLayer *layer, bool selectedOnly = false ) const; + + std::unique_ptr< QgsVectorLayer > mInputLayer; + std::unique_ptr< QgsVectorLayer > mIntersectionLayer; + int mTolerance{8}; +}; + +///@endcond PRIVATE + +#endif // QGSALGORITHMCHECKGEOMETRYLINELAYERINTERSECTION_H diff --git a/src/analysis/processing/qgsnativealgorithms.cpp b/src/analysis/processing/qgsnativealgorithms.cpp index f2bc4dd931e6..7a3383e1b782 100644 --- a/src/analysis/processing/qgsnativealgorithms.cpp +++ b/src/analysis/processing/qgsnativealgorithms.cpp @@ -47,6 +47,7 @@ #include "qgsalgorithmcheckgeometrymultipart.h" #include "qgsalgorithmcheckgeometryangle.h" #include "qgsalgorithmcheckgeometryselfintersection.h" +#include "qgsalgorithmcheckgeometrylinelayerintersection.h" #include "qgsalgorithmclip.h" #include "qgsalgorithmconcavehull.h" #include "qgsalgorithmconditionalbranch.h" @@ -320,6 +321,7 @@ void QgsNativeAlgorithms::loadAlgorithms() addAlgorithm( new QgsGeometryCheckMultipartAlgorithm() ); addAlgorithm( new QgsGeometryCheckAngleAlgorithm() ); addAlgorithm( new QgsGeometryCheckSelfIntersectionAlgorithm() ); + addAlgorithm( new QgsGeometryCheckLineLayerIntersectionAlgorithm() ); addAlgorithm( new QgsClipAlgorithm() ); addAlgorithm( new QgsCollectAlgorithm() ); addAlgorithm( new QgsCombineStylesAlgorithm() ); diff --git a/tests/src/analysis/testqgsprocessingcheckgeometry.cpp b/tests/src/analysis/testqgsprocessingcheckgeometry.cpp index f590f3efcca7..5bd84432af5a 100644 --- a/tests/src/analysis/testqgsprocessingcheckgeometry.cpp +++ b/tests/src/analysis/testqgsprocessingcheckgeometry.cpp @@ -47,6 +47,8 @@ class TestQgsProcessingCheckGeometry: public QgsTest void selfIntersectionAlg_data(); void selfIntersectionAlg(); + void lineLayerIntersectionAlg(); + private: QgsVectorLayer *mLineLayer = nullptr; @@ -300,5 +302,34 @@ void TestQgsProcessingCheckGeometry::selfIntersectionAlg() QCOMPARE( errorsLayer->featureCount(), expectedErrorCount ); } +void TestQgsProcessingCheckGeometry::lineLayerIntersectionAlg() +{ + std::unique_ptr< QgsProcessingAlgorithm > alg( + QgsApplication::processingRegistry()->createAlgorithmById( QStringLiteral( "native:checkgeometrylinelayerintersection" ) ) + ); + QVERIFY( alg != nullptr ); + + QVariantMap parameters; + parameters.insert( QStringLiteral( "INPUT" ), QVariant::fromValue( mLineLayer ) ); + parameters.insert( QStringLiteral( "INTERSECTION_LAYER" ), QVariant::fromValue( mPolygonLayer ) ); + parameters.insert( QStringLiteral( "OUTPUT" ), QgsProcessing::TEMPORARY_OUTPUT ); + parameters.insert( QStringLiteral( "ERRORS" ), 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 ); + + 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(), 5 ); + QCOMPARE( errorsLayer->featureCount(), 5 ); +} + QGSTEST_MAIN( TestQgsProcessingCheckGeometry ) #include "testqgsprocessingcheckgeometry.moc"