diff --git a/src/analysis/CMakeLists.txt b/src/analysis/CMakeLists.txt index 4e6acd42166..a1609045a46 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/qgsalgorithmcheckgeometryarea.cpp processing/qgsalgorithmclip.cpp processing/qgsalgorithmconcavehull.cpp processing/qgsalgorithmconditionalbranch.cpp diff --git a/src/analysis/processing/qgsalgorithmcheckgeometryarea.cpp b/src/analysis/processing/qgsalgorithmcheckgeometryarea.cpp new file mode 100644 index 00000000000..2ea83118ace --- /dev/null +++ b/src/analysis/processing/qgsalgorithmcheckgeometryarea.cpp @@ -0,0 +1,210 @@ +/*************************************************************************** + qgsalgorithmcheckgeometryarea.cpp + --------------------- + begin : November 2023 + copyright : (C) 2023 by Loïc Bartoletti + email : loic dot bartoletti 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 "qgsalgorithmcheckgeometryarea.h" +#include "qgsgeometrycheckcontext.h" +#include "qgsgeometrycheckerror.h" +#include "qgsgeometryareacheck.h" +#include "qgspoint.h" +#include "qgsvectorlayer.h" +#include "qgsvectordataproviderfeaturepool.h" + +///@cond PRIVATE + +auto QgsGeometryCheckAreaAlgorithm::name() const -> QString +{ + return QStringLiteral( "checkgeometryarea" ); +} + +auto QgsGeometryCheckAreaAlgorithm::displayName() const -> QString +{ + return QObject::tr( "Check geometry (Area)" ); +} + +auto QgsGeometryCheckAreaAlgorithm::tags() const -> QStringList +{ + return QObject::tr( "check,geometry,area" ).split( ',' ); +} + +auto QgsGeometryCheckAreaAlgorithm::group() const -> QString +{ + return QObject::tr( "Check geometry" ); +} + +auto QgsGeometryCheckAreaAlgorithm::groupId() const -> QString +{ + return QStringLiteral( "checkgeometry" ); +} + +auto QgsGeometryCheckAreaAlgorithm::shortHelpString() const -> QString +{ + return QObject::tr( "This algorithm check the area of geometry (polygon)." ); +} + +auto QgsGeometryCheckAreaAlgorithm::flags() const -> Qgis::ProcessingAlgorithmFlags +{ + return QgsProcessingAlgorithm::flags() | Qgis::ProcessingAlgorithmFlag::NoThreading; +} + +auto QgsGeometryCheckAreaAlgorithm::createInstance() const -> QgsGeometryCheckAreaAlgorithm * +{ + return new QgsGeometryCheckAreaAlgorithm(); +} + +void QgsGeometryCheckAreaAlgorithm::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" ) ) ); + addParameter( new QgsProcessingParameterNumber( QStringLiteral( "AREATHRESHOLD" ), QObject::tr( "area threshold" ), Qgis::ProcessingNumberParameterType::Double, 0, false, 0.0 ) ); + + // 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 QgsGeometryCheckAreaAlgorithm::prepareAlgorithm( const QVariantMap ¶meters, QgsProcessingContext &context, QgsProcessingFeedback * ) -> bool +{ + mTolerance = parameterAsInt( parameters, QStringLiteral( "TOLERANCE" ), context ); + return true; +} + +auto QgsGeometryCheckAreaAlgorithm::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 QgsGeometryCheckAreaAlgorithm::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 double areaThreshold = parameterAsDouble( parameters, QStringLiteral( "AREATHRESHOLD" ), context ); + + QVariantMap configurationCheck; + configurationCheck.insert( "areaThreshold", areaThreshold ); + const QgsGeometryAreaCheck check( checkContext.get(), configurationCheck ); + + 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/qgsalgorithmcheckgeometryarea.h b/src/analysis/processing/qgsalgorithmcheckgeometryarea.h new file mode 100644 index 00000000000..bb35a8d0059 --- /dev/null +++ b/src/analysis/processing/qgsalgorithmcheckgeometryarea.h @@ -0,0 +1,56 @@ +/*************************************************************************** + qgsalgorithmcheckgeometryarea.h + --------------------- + begin : November 2023 + copyright : (C) 2023 by Loïc Bartoletti + email : loic dot bartoletti 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 QGSALGORITHMCHECKGEOMETRYAREA_H +#define QGSALGORITHMCHECKGEOMETRYAREA_H + +#define SIP_NO_FILE + +#include "qgis_sip.h" +#include "qgsprocessingalgorithm.h" + +///@cond PRIVATE + +class QgsGeometryCheckAreaAlgorithm : public QgsProcessingAlgorithm +{ + public: + + QgsGeometryCheckAreaAlgorithm() = 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; + QgsGeometryCheckAreaAlgorithm *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 }; + static QgsFields outputFields(); +}; + +///@endcond PRIVATE + +#endif // QGSALGORITHMCHECKGEOMETRYAREA_H diff --git a/src/analysis/processing/qgsnativealgorithms.cpp b/src/analysis/processing/qgsnativealgorithms.cpp index 4b3f4d3f7d3..8bc8bfd5291 100644 --- a/src/analysis/processing/qgsnativealgorithms.cpp +++ b/src/analysis/processing/qgsnativealgorithms.cpp @@ -44,6 +44,7 @@ #include "qgsalgorithmcellstatistics.h" #include "qgsalgorithmcentroid.h" #include "qgsalgorithmcheckgeometryangle.h" +#include "qgsalgorithmcheckgeometryarea.h" #include "qgsalgorithmclip.h" #include "qgsalgorithmconcavehull.h" #include "qgsalgorithmconditionalbranch.h" @@ -326,6 +327,7 @@ void QgsNativeAlgorithms::loadAlgorithms() addAlgorithm( new QgsCellStatisticsPercentRankFromValueAlgorithm() ); addAlgorithm( new QgsCentroidAlgorithm() ); addAlgorithm( new QgsGeometryCheckAngleAlgorithm() ); + addAlgorithm( new QgsGeometryCheckAreaAlgorithm() ); addAlgorithm( new QgsClipAlgorithm() ); addAlgorithm( new QgsCollectAlgorithm() ); addAlgorithm( new QgsCombineStylesAlgorithm() ); diff --git a/tests/src/analysis/testqgsprocessingcheckgeometry.cpp b/tests/src/analysis/testqgsprocessingcheckgeometry.cpp index 321b4ff25f3..52639e52ab4 100644 --- a/tests/src/analysis/testqgsprocessingcheckgeometry.cpp +++ b/tests/src/analysis/testqgsprocessingcheckgeometry.cpp @@ -36,6 +36,8 @@ class TestQgsProcessingCheckGeometry: public QgsTest void angleAlg_data(); void angleAlg(); + void areaAlg(); + private: QgsVectorLayer *mLineLayer = nullptr; @@ -124,5 +126,35 @@ void TestQgsProcessingCheckGeometry::angleAlg() QCOMPARE( errorsLayer->featureCount(), expectedErrorCount ); } +void TestQgsProcessingCheckGeometry::areaAlg() +{ + std::unique_ptr< QgsProcessingAlgorithm > alg( + QgsApplication::processingRegistry()->createAlgorithmById( QStringLiteral( "native:checkgeometryarea" ) ) + ); + QVERIFY( alg != nullptr ); + + QVariantMap parameters; + parameters.insert( QStringLiteral( "INPUT" ), QVariant::fromValue( mPolygonLayer ) ); + parameters.insert( QStringLiteral( "UNIQUE_ID" ), "id" ); + parameters.insert( QStringLiteral( "AREATHRESHOLD" ), 0.04 ); + 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(), 8 ); + QCOMPARE( errorsLayer->featureCount(), 8 ); +} + QGSTEST_MAIN( TestQgsProcessingCheckGeometry ) #include "testqgsprocessingcheckgeometry.moc"