mirror of
https://github.com/qgis/QGIS.git
synced 2025-11-22 00:14:55 -05:00
Merge pull request #61511 from Djedouas/checker_fix_multipart
Processing: Add Fix Geometry algorithm and test: Multipart
This commit is contained in:
commit
622359a928
@ -63,6 +63,7 @@ set(QGIS_ANALYSIS_SRCS
|
|||||||
processing/qgsalgorithmcentroid.cpp
|
processing/qgsalgorithmcentroid.cpp
|
||||||
processing/qgsalgorithmcheckgeometrysegmentlength.cpp
|
processing/qgsalgorithmcheckgeometrysegmentlength.cpp
|
||||||
processing/qgsalgorithmcheckgeometryangle.cpp
|
processing/qgsalgorithmcheckgeometryangle.cpp
|
||||||
|
processing/qgsalgorithmfixgeometrymultipart.cpp
|
||||||
processing/qgsalgorithmfixgeometryoverlap.cpp
|
processing/qgsalgorithmfixgeometryoverlap.cpp
|
||||||
processing/qgsalgorithmfixgeometrydeletefeatures.cpp
|
processing/qgsalgorithmfixgeometrydeletefeatures.cpp
|
||||||
processing/qgsalgorithmfixgeometryangle.cpp
|
processing/qgsalgorithmfixgeometryangle.cpp
|
||||||
|
|||||||
236
src/analysis/processing/qgsalgorithmfixgeometrymultipart.cpp
Normal file
236
src/analysis/processing/qgsalgorithmfixgeometrymultipart.cpp
Normal file
@ -0,0 +1,236 @@
|
|||||||
|
/***************************************************************************
|
||||||
|
qgsalgorithmfixgeometrymultipart.cpp
|
||||||
|
---------------------
|
||||||
|
begin : April 2025
|
||||||
|
copyright : (C) 2025 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 "qgsalgorithmfixgeometrymultipart.h"
|
||||||
|
#include "qgsgeometrycheckerror.h"
|
||||||
|
#include "qgsgeometrycheckerutils.h"
|
||||||
|
#include "qgsgeometrymultipartcheck.h"
|
||||||
|
#include "qgsvectordataproviderfeaturepool.h"
|
||||||
|
#include "qgsvectorlayer.h"
|
||||||
|
#include "qgsvectorfilewriter.h"
|
||||||
|
|
||||||
|
///@cond PRIVATE
|
||||||
|
|
||||||
|
QString QgsFixGeometryMultipartAlgorithm::name() const
|
||||||
|
{
|
||||||
|
return QStringLiteral( "fixgeometrymultipart" );
|
||||||
|
}
|
||||||
|
|
||||||
|
QString QgsFixGeometryMultipartAlgorithm::displayName() const
|
||||||
|
{
|
||||||
|
return QObject::tr( "Fix geometry (strict multipart)" );
|
||||||
|
}
|
||||||
|
|
||||||
|
QStringList QgsFixGeometryMultipartAlgorithm::tags() const
|
||||||
|
{
|
||||||
|
return QObject::tr( "fix,multipart,singlepart" ).split( ',' );
|
||||||
|
}
|
||||||
|
|
||||||
|
QString QgsFixGeometryMultipartAlgorithm::group() const
|
||||||
|
{
|
||||||
|
return QObject::tr( "Fix geometry" );
|
||||||
|
}
|
||||||
|
|
||||||
|
QString QgsFixGeometryMultipartAlgorithm::groupId() const
|
||||||
|
{
|
||||||
|
return QStringLiteral( "fixgeometry" );
|
||||||
|
}
|
||||||
|
|
||||||
|
QString QgsFixGeometryMultipartAlgorithm::shortHelpString() const
|
||||||
|
{
|
||||||
|
return QObject::tr( "This algorithm converts multipart geometries that consists of only one geometry "
|
||||||
|
"into singlepart geometries, based on an error layer from the check strict multipart algorithm." );
|
||||||
|
}
|
||||||
|
|
||||||
|
QgsFixGeometryMultipartAlgorithm *QgsFixGeometryMultipartAlgorithm::createInstance() const
|
||||||
|
{
|
||||||
|
return new QgsFixGeometryMultipartAlgorithm();
|
||||||
|
}
|
||||||
|
|
||||||
|
void QgsFixGeometryMultipartAlgorithm::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( "Error 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" )
|
||||||
|
) );
|
||||||
|
|
||||||
|
// 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() );
|
||||||
|
}
|
||||||
|
|
||||||
|
QVariantMap QgsFixGeometryMultipartAlgorithm::processAlgorithm( const QVariantMap ¶meters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
|
||||||
|
{
|
||||||
|
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 );
|
||||||
|
|
||||||
|
// Verify that input fields exists
|
||||||
|
if ( errors->fields().indexFromName( featIdFieldName ) == -1 )
|
||||||
|
throw QgsProcessingException( QObject::tr( "Field \"%1\" does not exist in the error layer." ).arg( featIdFieldName ) );
|
||||||
|
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 as in the error 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();
|
||||||
|
QgsGeometryCheckContext checkContext = QgsGeometryCheckContext( mTolerance, input->sourceCrs(), project->transformContext(), project );
|
||||||
|
QStringList messages;
|
||||||
|
|
||||||
|
const QgsGeometryMultipartCheck check( &checkContext, QVariantMap() );
|
||||||
|
|
||||||
|
QgsVectorLayer *fixedLayer = input->materialize( QgsFeatureRequest() );
|
||||||
|
QgsVectorDataProviderFeaturePool featurePool = QgsVectorDataProviderFeaturePool( fixedLayer );
|
||||||
|
QMap<QString, QgsFeaturePool *> featurePools;
|
||||||
|
featurePools.insert( fixedLayer->id(), &featurePool );
|
||||||
|
|
||||||
|
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
|
||||||
|
{
|
||||||
|
QgsGeometryCheckError checkError = QgsGeometryCheckError(
|
||||||
|
&check,
|
||||||
|
QgsGeometryCheckerUtils::LayerFeature( &featurePool, inputFeature, &checkContext, false ),
|
||||||
|
errorFeature.geometry().asPoint(),
|
||||||
|
QgsVertexId()
|
||||||
|
);
|
||||||
|
for ( QgsGeometryCheck::Changes changes : changesList )
|
||||||
|
checkError.handleChanges( changes );
|
||||||
|
|
||||||
|
QgsGeometryCheck::Changes changes;
|
||||||
|
check.fixError( featurePools, &checkError, QgsGeometryMultipartCheck::ResolutionMethod::ConvertToSingle, QMap<QString, int>(), changes );
|
||||||
|
changesList << changes;
|
||||||
|
QString resolutionMessage = checkError.resolutionMessage();
|
||||||
|
if ( checkError.status() == QgsGeometryCheckError::StatusObsolete )
|
||||||
|
resolutionMessage = QObject::tr( "Error is obsolete" );
|
||||||
|
reportFeature.setAttributes( errorFeature.attributes() << 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool QgsFixGeometryMultipartAlgorithm::prepareAlgorithm( const QVariantMap ¶meters, QgsProcessingContext &context, QgsProcessingFeedback * )
|
||||||
|
{
|
||||||
|
mTolerance = parameterAsInt( parameters, QStringLiteral( "TOLERANCE" ), context );
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
Qgis::ProcessingAlgorithmFlags QgsFixGeometryMultipartAlgorithm::flags() const
|
||||||
|
{
|
||||||
|
return QgsProcessingAlgorithm::flags() | Qgis::ProcessingAlgorithmFlag::NoThreading;
|
||||||
|
}
|
||||||
|
|
||||||
|
///@endcond
|
||||||
52
src/analysis/processing/qgsalgorithmfixgeometrymultipart.h
Normal file
52
src/analysis/processing/qgsalgorithmfixgeometrymultipart.h
Normal file
@ -0,0 +1,52 @@
|
|||||||
|
/***************************************************************************
|
||||||
|
qgsalgorithmfixgeometrymultipart.h
|
||||||
|
---------------------
|
||||||
|
begin : April 2025
|
||||||
|
copyright : (C) 2025 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 QGSALGORITHMFIXGEOMETRYMULTIPART_H
|
||||||
|
#define QGSALGORITHMFIXGEOMETRYMULTIPART_H
|
||||||
|
|
||||||
|
#define SIP_NO_FILE
|
||||||
|
|
||||||
|
#include "qgis_sip.h"
|
||||||
|
#include "qgsprocessingalgorithm.h"
|
||||||
|
|
||||||
|
///@cond PRIVATE
|
||||||
|
|
||||||
|
class QgsFixGeometryMultipartAlgorithm : public QgsProcessingAlgorithm
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
QgsFixGeometryMultipartAlgorithm() = 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;
|
||||||
|
QgsFixGeometryMultipartAlgorithm *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 };
|
||||||
|
};
|
||||||
|
|
||||||
|
///@endcond PRIVATE
|
||||||
|
|
||||||
|
#endif // QGSALGORITHMFIXGEOMETRYMULTIPART_H
|
||||||
@ -50,6 +50,7 @@
|
|||||||
#include "qgsalgorithmfixgeometrydeletefeatures.h"
|
#include "qgsalgorithmfixgeometrydeletefeatures.h"
|
||||||
#include "qgsalgorithmfixgeometrygap.h"
|
#include "qgsalgorithmfixgeometrygap.h"
|
||||||
#include "qgsalgorithmfixgeometryarea.h"
|
#include "qgsalgorithmfixgeometryarea.h"
|
||||||
|
#include "qgsalgorithmfixgeometrymultipart.h"
|
||||||
#include "qgsalgorithmfixgeometryoverlap.h"
|
#include "qgsalgorithmfixgeometryoverlap.h"
|
||||||
#include "qgsalgorithmfixgeometryhole.h"
|
#include "qgsalgorithmfixgeometryhole.h"
|
||||||
#include "qgsalgorithmfixgeometrymissingvertex.h"
|
#include "qgsalgorithmfixgeometrymissingvertex.h"
|
||||||
@ -634,6 +635,7 @@ void QgsNativeAlgorithms::loadAlgorithms()
|
|||||||
addAlgorithm( new QgsFixGeometryOverlapAlgorithm() );
|
addAlgorithm( new QgsFixGeometryOverlapAlgorithm() );
|
||||||
addAlgorithm( new QgsFixGeometryDeleteFeaturesAlgorithm() );
|
addAlgorithm( new QgsFixGeometryDeleteFeaturesAlgorithm() );
|
||||||
addAlgorithm( new QgsFixGeometryAngleAlgorithm() );
|
addAlgorithm( new QgsFixGeometryAngleAlgorithm() );
|
||||||
|
addAlgorithm( new QgsFixGeometryMultipartAlgorithm() );
|
||||||
addAlgorithm( new QgsFixGeometrySelfIntersectionAlgorithm() );
|
addAlgorithm( new QgsFixGeometrySelfIntersectionAlgorithm() );
|
||||||
addAlgorithm( new QgsFixGeometryGapAlgorithm() );
|
addAlgorithm( new QgsFixGeometryGapAlgorithm() );
|
||||||
addAlgorithm( new QgsFixGeometryAreaAlgorithm() );
|
addAlgorithm( new QgsFixGeometryAreaAlgorithm() );
|
||||||
|
|||||||
@ -17,6 +17,7 @@
|
|||||||
#include "qgsnativealgorithms.h"
|
#include "qgsnativealgorithms.h"
|
||||||
#include "qgsprocessingregistry.h"
|
#include "qgsprocessingregistry.h"
|
||||||
#include "qgstest.h"
|
#include "qgstest.h"
|
||||||
|
#include "qgswkbtypes.h"
|
||||||
#include "qgsvectorlayer.h"
|
#include "qgsvectorlayer.h"
|
||||||
|
|
||||||
class TestQgsProcessingFixGeometry : public QgsTest
|
class TestQgsProcessingFixGeometry : public QgsTest
|
||||||
@ -36,6 +37,9 @@ class TestQgsProcessingFixGeometry : public QgsTest
|
|||||||
void fixAngleAlg_data();
|
void fixAngleAlg_data();
|
||||||
void fixAngleAlg();
|
void fixAngleAlg();
|
||||||
|
|
||||||
|
void fixMultipartAlg_data();
|
||||||
|
void fixMultipartAlg();
|
||||||
|
|
||||||
void fixAreaAlg_data();
|
void fixAreaAlg_data();
|
||||||
void fixAreaAlg();
|
void fixAreaAlg();
|
||||||
|
|
||||||
@ -187,6 +191,83 @@ void TestQgsProcessingFixGeometry::fixAngleAlg()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void TestQgsProcessingFixGeometry::fixMultipartAlg_data()
|
||||||
|
{
|
||||||
|
const QDir testDataDir( QDir( TEST_DATA_DIR ).absoluteFilePath( "geometry_checker" ) );
|
||||||
|
|
||||||
|
QTest::addColumn<QgsVectorLayer *>( "sourceLayer" );
|
||||||
|
QTest::addColumn<QgsVectorLayer *>( "errorsLayer" );
|
||||||
|
QTest::addColumn<QStringList>( "reportList" );
|
||||||
|
|
||||||
|
QStringList linesReportList;
|
||||||
|
for ( int i = 0; i < 8; i++ )
|
||||||
|
linesReportList << QStringLiteral( "Convert to single part feature" );
|
||||||
|
QTest::newRow( "Lines" )
|
||||||
|
<< new QgsVectorLayer( testDataDir.absoluteFilePath( "line_layer.shp" ), QStringLiteral( "lines" ), QStringLiteral( "ogr" ) )
|
||||||
|
<< new QgsVectorLayer( mDataDir.absoluteFilePath( "strict_multipart.gpkg|layername=lines_to_fix" ), QStringLiteral( "lines fo fix" ), QStringLiteral( "ogr" ) )
|
||||||
|
<< linesReportList;
|
||||||
|
|
||||||
|
QStringList polygonsReportList;
|
||||||
|
for ( int i = 0; i < 24; i++ )
|
||||||
|
polygonsReportList << QStringLiteral( "Convert to single part feature" );
|
||||||
|
QTest::newRow( "Polygons" )
|
||||||
|
<< new QgsVectorLayer( testDataDir.absoluteFilePath( "polygon_layer.shp" ), QStringLiteral( "polygon" ), QStringLiteral( "ogr" ) )
|
||||||
|
<< new QgsVectorLayer( mDataDir.absoluteFilePath( "strict_multipart.gpkg|layername=polygons_to_fix" ), QStringLiteral( "polygons fo fix" ), QStringLiteral( "ogr" ) )
|
||||||
|
<< polygonsReportList;
|
||||||
|
}
|
||||||
|
|
||||||
|
void TestQgsProcessingFixGeometry::fixMultipartAlg()
|
||||||
|
{
|
||||||
|
QFETCH( QgsVectorLayer *, sourceLayer );
|
||||||
|
QFETCH( QgsVectorLayer *, errorsLayer );
|
||||||
|
QFETCH( QStringList, reportList );
|
||||||
|
|
||||||
|
QVERIFY( sourceLayer->isValid() );
|
||||||
|
QVERIFY( errorsLayer->isValid() );
|
||||||
|
|
||||||
|
const std::unique_ptr<QgsProcessingAlgorithm> alg(
|
||||||
|
QgsApplication::processingRegistry()->createAlgorithmById( QStringLiteral( "native:fixgeometrymultipart" ) )
|
||||||
|
);
|
||||||
|
QVERIFY( alg != nullptr );
|
||||||
|
|
||||||
|
QVariantMap parameters;
|
||||||
|
parameters.insert( QStringLiteral( "INPUT" ), QVariant::fromValue( QgsProcessingFeatureSourceDefinition( sourceLayer->source() ) ) );
|
||||||
|
parameters.insert( QStringLiteral( "ERRORS" ), QVariant::fromValue( QgsProcessingFeatureSourceDefinition( errorsLayer->source() ) ) );
|
||||||
|
parameters.insert( QStringLiteral( "OUTPUT" ), QgsProcessing::TEMPORARY_OUTPUT );
|
||||||
|
parameters.insert( QStringLiteral( "REPORT" ), QgsProcessing::TEMPORARY_OUTPUT );
|
||||||
|
parameters.insert( QStringLiteral( "UNIQUE_ID" ), "id" );
|
||||||
|
|
||||||
|
bool ok = false;
|
||||||
|
QgsProcessingFeedback feedback;
|
||||||
|
auto context = std::make_unique<QgsProcessingContext>();
|
||||||
|
|
||||||
|
QVariantMap results;
|
||||||
|
results = alg->run( parameters, *context, &feedback, &ok );
|
||||||
|
QVERIFY( ok );
|
||||||
|
|
||||||
|
std::unique_ptr<QgsVectorLayer> outputLayer( qobject_cast<QgsVectorLayer *>( context->getMapLayer( results.value( QStringLiteral( "OUTPUT" ) ).toString() ) ) );
|
||||||
|
std::unique_ptr<QgsVectorLayer> reportLayer( qobject_cast<QgsVectorLayer *>( context->getMapLayer( results.value( QStringLiteral( "REPORT" ) ).toString() ) ) );
|
||||||
|
QVERIFY( reportLayer->isValid() );
|
||||||
|
QVERIFY( outputLayer->isValid() );
|
||||||
|
|
||||||
|
QCOMPARE( outputLayer->featureCount(), sourceLayer->featureCount() );
|
||||||
|
QCOMPARE( reportLayer->featureCount(), reportList.count() );
|
||||||
|
int idx = 1;
|
||||||
|
for ( const QString &expectedReport : reportList )
|
||||||
|
{
|
||||||
|
const QgsFeature reportFeature = reportLayer->getFeature( idx );
|
||||||
|
QCOMPARE( reportFeature.attribute( "report" ), expectedReport );
|
||||||
|
idx++;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Verification of multipart type
|
||||||
|
QSet<QVariant> singleTypeIds = reportLayer->uniqueValues( reportLayer->fields().indexFromName( QStringLiteral( "id" ) ) );
|
||||||
|
QgsFeatureIterator it = outputLayer->getFeatures();
|
||||||
|
QgsFeature feat;
|
||||||
|
while ( it.nextFeature( feat ) )
|
||||||
|
QCOMPARE( QgsWkbTypes::isSingleType( feat.geometry().wkbType() ), singleTypeIds.contains( feat.attribute( QStringLiteral( "id" ) ) ) );
|
||||||
|
}
|
||||||
|
|
||||||
void TestQgsProcessingFixGeometry::fixAreaAlg_data()
|
void TestQgsProcessingFixGeometry::fixAreaAlg_data()
|
||||||
{
|
{
|
||||||
//create a line layer that will be used in tests
|
//create a line layer that will be used in tests
|
||||||
|
|||||||
BIN
tests/testdata/geometry_fix/strict_multipart.gpkg
vendored
Normal file
BIN
tests/testdata/geometry_fix/strict_multipart.gpkg
vendored
Normal file
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user