Processing: Add Fix Geometry algorithm and test: Hole

Porting hole fix from geometry checker to processings
This commit is contained in:
Jacky Volpes 2024-07-25 16:53:04 +02:00 committed by Loïc Bartoletti
parent c075f2a490
commit eb7482b815
6 changed files with 361 additions and 0 deletions

View File

@ -64,6 +64,7 @@ set(QGIS_ANALYSIS_SRCS
processing/qgsalgorithmcheckgeometryangle.cpp
processing/qgsalgorithmfixgeometryangle.cpp
processing/qgsalgorithmcheckgeometryhole.cpp
processing/qgsalgorithmfixgeometryhole.cpp
processing/qgsalgorithmcheckgeometryarea.cpp
processing/qgsalgorithmfixgeometryarea.cpp
processing/qgsalgorithmclip.cpp

View File

@ -0,0 +1,256 @@
/***************************************************************************
qgsalgorithmfixgeometryhole.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 "qgsalgorithmfixgeometryhole.h"
#include "qgsgeometrycheckerror.h"
#include "qgsgeometrycheckerutils.h"
#include "qgsgeometryholecheck.h"
#include "qgsvectordataproviderfeaturepool.h"
#include "qgsvectorlayer.h"
#include "qgsvectorfilewriter.h"
///@cond PRIVATE
auto QgsFixGeometryHoleAlgorithm::name() const -> QString
{
return QStringLiteral( "fixgeometryhole" );
}
auto QgsFixGeometryHoleAlgorithm::displayName() const -> QString
{
return QObject::tr( "Fix geometry (Hole)" );
}
auto QgsFixGeometryHoleAlgorithm::tags() const -> QStringList
{
return QObject::tr( "delete,fix,hole" ).split( ',' );
}
auto QgsFixGeometryHoleAlgorithm::group() const -> QString
{
return QObject::tr( "Fix geometry" );
}
auto QgsFixGeometryHoleAlgorithm::groupId() const -> QString
{
return QStringLiteral( "fixgeometry" );
}
auto QgsFixGeometryHoleAlgorithm::shortHelpString() const -> QString
{
return QObject::tr( "This algorithm deletes holes based on an error layer from the "
"check holes algorithm.\n" );
}
auto QgsFixGeometryHoleAlgorithm::createInstance() const -> QgsFixGeometryHoleAlgorithm *
{
return new QgsFixGeometryHoleAlgorithm();
}
void QgsFixGeometryHoleAlgorithm::initAlgorithm( const QVariantMap &configuration )
{
Q_UNUSED( configuration )
// Inputs
addParameter( new QgsProcessingParameterFeatureSource( QStringLiteral( "INPUT" ), QObject::tr( "Input layer" ),
QList< int >() << static_cast<int>( Qgis::ProcessingSourceType::VectorPolygon ) << static_cast<int>( Qgis::ProcessingSourceType::VectorLine ) )
);
addParameter( new QgsProcessingParameterFeatureSource( QStringLiteral( "ERRORS" ), QObject::tr( "Errors layer" ),
QList< int >() << static_cast<int>( Qgis::ProcessingSourceType::VectorPoint ) )
);
addParameter( new QgsProcessingParameterField(
QStringLiteral( "UNIQUE_ID" ), QObject::tr( "Field of original feature unique identifier" ),
QStringLiteral( "id" ), QStringLiteral( "ERRORS" ) )
);
addParameter( new QgsProcessingParameterField(
QStringLiteral( "PART_IDX" ), QObject::tr( "Field of part index" ),
QStringLiteral( "gc_partidx" ), QStringLiteral( "ERRORS" ),
Qgis::ProcessingFieldParameterDataType::Numeric )
);
addParameter( new QgsProcessingParameterField(
QStringLiteral( "RING_IDX" ), QObject::tr( "Field of ring index" ),
QStringLiteral( "gc_ringidx" ), QStringLiteral( "ERRORS" ),
Qgis::ProcessingFieldParameterDataType::Numeric )
);
addParameter( new QgsProcessingParameterField(
QStringLiteral( "VERTEX_IDX" ), QObject::tr( "Field of vertex index" ),
QStringLiteral( "gc_vertidx" ), QStringLiteral( "ERRORS" ),
Qgis::ProcessingFieldParameterDataType::Numeric )
);
// 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< 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 QgsFixGeometryHoleAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) -> QVariantMap
{
const std::unique_ptr< QgsProcessingFeatureSource > input( parameterAsSource( parameters, QStringLiteral( "INPUT" ), context ) );
if ( !input )
throw QgsProcessingException( invalidSourceError( parameters, QStringLiteral( "INPUT" ) ) );
const std::unique_ptr< QgsProcessingFeatureSource > 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 );
const QString partIdxFieldName = parameterAsString( parameters, QStringLiteral( "PART_IDX" ), context );
const QString ringIdxFieldName = parameterAsString( parameters, QStringLiteral( "RING_IDX" ), context );
const QString vertexIdxFieldName = parameterAsString( parameters, QStringLiteral( "VERTEX_IDX" ), context );
// Verify that input fields exists
if ( errors->fields().indexFromName( featIdFieldName ) == -1 )
throw QgsProcessingException( QObject::tr( "Field %1 does not exist in errors layer." ).arg( featIdFieldName ) );
if ( errors->fields().indexFromName( partIdxFieldName ) == -1 )
throw QgsProcessingException( QObject::tr( "Field %1 does not exist in errors layer." ).arg( partIdxFieldName ) );
if ( errors->fields().indexFromName( ringIdxFieldName ) == -1 )
throw QgsProcessingException( QObject::tr( "Field %1 does not exist in errors layer." ).arg( ringIdxFieldName ) );
if ( errors->fields().indexFromName( vertexIdxFieldName ) == -1 )
throw QgsProcessingException( QObject::tr( "Field %1 does not exist in errors layer." ).arg( vertexIdxFieldName ) );
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 than in errors layer." ).arg( featIdFieldName ) );
QString dest_output;
const std::unique_ptr< QgsFeatureSink > 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< QgsFeatureSink > 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();
std::unique_ptr<QgsGeometryCheckContext> checkContext = std::make_unique<QgsGeometryCheckContext>( mTolerance, input->sourceCrs(), project->transformContext(), project );
QStringList messages;
const QgsGeometryHoleCheck check( checkContext.get(), QVariantMap() );
QgsVectorLayer *fixedLayer = input->materialize( QgsFeatureRequest() );
std::unique_ptr<QgsFeaturePool> featurePool = std::make_unique<QgsVectorDataProviderFeaturePool>( fixedLayer );
QMap<QString, QgsFeaturePool *> featurePools;
featurePools.insert( fixedLayer->id(), featurePool.get() );
QgsFeature errorFeature, inputFeature, testDuplicateIdFeature;
QgsFeatureIterator errorFeaturesIt = errors->getFeatures();
QList<QgsGeometryCheck::Changes> 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<double>( static_cast<long double>( 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 if ( QgsGeometryCheckerUtils::getGeomPart( inputFeature.geometry().constGet(), errorFeature.attribute( partIdxFieldName ).toInt() ) == nullptr )
reportFeature.setAttributes( errorFeature.attributes() << QObject::tr( "Feature geometry part is null" ) << false );
else
{
QgsGeometryCheckError checkError = QgsGeometryCheckError(
&check,
QgsGeometryCheckerUtils::LayerFeature( featurePool.get(), inputFeature, checkContext.get(), false ),
errorFeature.geometry().asPoint(),
QgsVertexId(
errorFeature.attribute( partIdxFieldName ).toInt(),
errorFeature.attribute( ringIdxFieldName ).toInt(),
errorFeature.attribute( vertexIdxFieldName ).toInt()
)
);
for ( auto changes : changesList )
checkError.handleChanges( changes );
QgsGeometryCheck::Changes changes;
check.fixError( featurePools, &checkError, QgsGeometryHoleCheck::ResolutionMethod::RemoveHoles, QMap<QString, int>(), changes );
changesList << changes;
reportFeature.setAttributes( errorFeature.attributes() << checkError.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<double>( static_cast<long double>( 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;
}
auto QgsFixGeometryHoleAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * ) -> bool
{
mTolerance = parameterAsInt( parameters, QStringLiteral( "TOLERANCE" ), context );
return true;
}
auto QgsFixGeometryHoleAlgorithm::flags() const -> Qgis::ProcessingAlgorithmFlags
{
return QgsProcessingAlgorithm::flags() | Qgis::ProcessingAlgorithmFlag::NoThreading;
}
///@endcond

View File

@ -0,0 +1,55 @@
/***************************************************************************
qgsalgorithmfixgeometryhole.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 QGSALGORITHMFIXGEOMETRYHOLE_H
#define QGSALGORITHMFIXGEOMETRYHOLE_H
#define SIP_NO_FILE
#include "qgis_sip.h"
#include "qgsprocessingalgorithm.h"
///@cond PRIVATE
class QgsFixGeometryHoleAlgorithm : public QgsProcessingAlgorithm
{
public:
QgsFixGeometryHoleAlgorithm() = 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;
QgsFixGeometryHoleAlgorithm *createInstance() const override SIP_FACTORY;
protected:
bool prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) override;
QVariantMap processAlgorithm( const QVariantMap &parameters,
QgsProcessingContext &context, QgsProcessingFeedback *feedback ) override;
private:
int mTolerance{8};
};
///@endcond PRIVATE
#endif // QGSALGORITHMFIXGEOMETRYHOLE_H

View File

@ -46,6 +46,7 @@
#include "qgsalgorithmcheckgeometryangle.h"
#include "qgsalgorithmcheckgeometryarea.h"
#include "qgsalgorithmfixgeometryarea.h"
#include "qgsalgorithmfixgeometryhole.h"
#include "qgsalgorithmcheckgeometryhole.h"
#include "qgsalgorithmclip.h"
#include "qgsalgorithmconcavehull.h"
@ -585,6 +586,7 @@ void QgsNativeAlgorithms::loadAlgorithms()
addAlgorithm( new QgsDensifyGeometriesByCountAlgorithm() );
addAlgorithm( new QgsFixGeometryAngleAlgorithm() );
addAlgorithm( new QgsFixGeometryAreaAlgorithm() );
addAlgorithm( new QgsFixGeometryHoleAlgorithm() );
}
///@endcond

View File

@ -38,6 +38,8 @@ class TestQgsProcessingFixGeometry: public QgsTest
void fixAreaAlg_data();
void fixAreaAlg();
void fixHoleAlg();
private:
const QDir mDataDir{ QDir( TEST_DATA_DIR ).absoluteFilePath( QStringLiteral( "geometry_fix" ) ) };
@ -272,5 +274,50 @@ void TestQgsProcessingFixGeometry::fixAreaAlg()
}
}
void TestQgsProcessingFixGeometry::fixHoleAlg()
{
const QDir testDataDir( QDir( TEST_DATA_DIR ).absoluteFilePath( "geometry_checker" ) );
QgsVectorLayer sourceLayer = QgsVectorLayer( testDataDir.absoluteFilePath( "polygon_layer.shp" ), QStringLiteral( "polygons" ), QStringLiteral( "ogr" ) );
QgsVectorLayer errorsLayer = QgsVectorLayer( mDataDir.absoluteFilePath( "remove_hole.gpkg|layername=errors_layer" ), QStringLiteral( "" ), QStringLiteral( "ogr" ) );
QVERIFY( sourceLayer.isValid() );
QVERIFY( errorsLayer.isValid() );
const auto reportList = QStringList() << QStringLiteral( "Remove hole" );
const std::unique_ptr< QgsProcessingAlgorithm > alg(
QgsApplication::processingRegistry()->createAlgorithmById( QStringLiteral( "native:fixgeometryhole" ) )
);
QVERIFY( alg != nullptr );
QVariantMap parameters;
parameters.insert( QStringLiteral( "INPUT" ), QVariant::fromValue( &sourceLayer ) );
parameters.insert( QStringLiteral( "UNIQUE_ID" ), "id" );
parameters.insert( QStringLiteral( "ERRORS" ), QVariant::fromValue( &errorsLayer ) );
parameters.insert( QStringLiteral( "OUTPUT" ), QgsProcessing::TEMPORARY_OUTPUT );
parameters.insert( QStringLiteral( "REPORT" ), 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 );
const std::unique_ptr<QgsVectorLayer> outputLayer( qobject_cast< QgsVectorLayer * >( context->getMapLayer( results.value( QStringLiteral( "OUTPUT" ) ).toString() ) ) );
const std::unique_ptr<QgsVectorLayer> reportLayer( qobject_cast< QgsVectorLayer * >( context->getMapLayer( results.value( QStringLiteral( "REPORT" ) ).toString() ) ) );
QVERIFY( reportLayer->isValid() );
QVERIFY( outputLayer->isValid() );
QCOMPARE( outputLayer->featureCount(), 25 );
QCOMPARE( reportLayer->featureCount(), reportList.count() );
int idx = 1;
for ( QString expectedReport : reportList )
{
const QgsFeature reportFeature = reportLayer->getFeature( idx );
QCOMPARE( reportFeature.attribute( "report" ), expectedReport );
idx++;
}
}
QGSTEST_MAIN( TestQgsProcessingFixGeometry )
#include "testqgsprocessingfixgeometry.moc"

Binary file not shown.