From 4e9da6f1906c6ba6076742e9aa3d49bebd1965b6 Mon Sep 17 00:00:00 2001 From: Nyall Dawson Date: Mon, 29 Oct 2018 12:10:59 +1000 Subject: [PATCH] [FEATURE][processing] New algorithm "Remove duplicates by attribute" Allows for removal of duplicate features, identified using the values in one (or more) field values from the input features. Optionally any discarded (duplicate) features can be saved to a separate sink. --- src/analysis/CMakeLists.txt | 1 + ...gsalgorithmremoveduplicatesbyattribute.cpp | 171 ++++++++++++++++++ .../qgsalgorithmremoveduplicatesbyattribute.h | 58 ++++++ .../processing/qgsnativealgorithms.cpp | 2 + 4 files changed, 232 insertions(+) create mode 100644 src/analysis/processing/qgsalgorithmremoveduplicatesbyattribute.cpp create mode 100644 src/analysis/processing/qgsalgorithmremoveduplicatesbyattribute.h diff --git a/src/analysis/CMakeLists.txt b/src/analysis/CMakeLists.txt index b2d57f2b59b..d3d385f6862 100644 --- a/src/analysis/CMakeLists.txt +++ b/src/analysis/CMakeLists.txt @@ -74,6 +74,7 @@ SET(QGIS_ANALYSIS_SRCS processing/qgsalgorithmpromotetomultipart.cpp processing/qgsalgorithmrasterlayeruniquevalues.cpp processing/qgsalgorithmreclassifybylayer.cpp + processing/qgsalgorithmremoveduplicatesbyattribute.cpp processing/qgsalgorithmremoveduplicatevertices.cpp processing/qgsalgorithmremoveholes.cpp processing/qgsalgorithmremovenullgeometry.cpp diff --git a/src/analysis/processing/qgsalgorithmremoveduplicatesbyattribute.cpp b/src/analysis/processing/qgsalgorithmremoveduplicatesbyattribute.cpp new file mode 100644 index 00000000000..ccde99eec8c --- /dev/null +++ b/src/analysis/processing/qgsalgorithmremoveduplicatesbyattribute.cpp @@ -0,0 +1,171 @@ +/*************************************************************************** + qgsalgorithmremoveduplicatesbyattribute.cpp + ---------------------------------- + begin : October 2018 + copyright : (C) 2018 by Nyall Dawson + email : nyall dot dawson at gmail 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 "qgsalgorithmremoveduplicatesbyattribute.h" + +///@cond PRIVATE + +QString QgsRemoveDuplicatesByAttributeAlgorithm::name() const +{ + return QStringLiteral( "removeduplicatesbyattribute" ); +} + +QString QgsRemoveDuplicatesByAttributeAlgorithm::displayName() const +{ + return QObject::tr( "Remove duplicates by attribute" ); +} + +QStringList QgsRemoveDuplicatesByAttributeAlgorithm::tags() const +{ + return QObject::tr( "field,value,same,filter" ).split( ',' ); +} + +QString QgsRemoveDuplicatesByAttributeAlgorithm::group() const +{ + return QObject::tr( "Vector selection" ); +} + +QString QgsRemoveDuplicatesByAttributeAlgorithm::groupId() const +{ + return QStringLiteral( "vectorselection" ); +} + +void QgsRemoveDuplicatesByAttributeAlgorithm::initAlgorithm( const QVariantMap & ) +{ + addParameter( new QgsProcessingParameterFeatureSource( QStringLiteral( "INPUT" ), QObject::tr( "Input layer" ), + QList< int >() << QgsProcessing::TypeVector ) ); + addParameter( new QgsProcessingParameterField( QStringLiteral( "FIELDS" ), QObject::tr( "Field to match duplicates by" ), QVariant(), QStringLiteral( "INPUT" ), QgsProcessingParameterField::Any, true ) ); + + addParameter( new QgsProcessingParameterFeatureSink( QStringLiteral( "OUTPUT" ), QObject::tr( "Filtered (no duplicates)" ) ) ); + QgsProcessingParameterFeatureSink *failOutput = new QgsProcessingParameterFeatureSink( QStringLiteral( "DUPLICATES" ), QObject::tr( "Filtered (duplicates)" ), + QgsProcessing::TypeVectorAnyGeometry, QVariant(), true ); + failOutput->setCreateByDefault( false ); + addParameter( failOutput ); + + addOutput( new QgsProcessingOutputNumber( QStringLiteral( "RETAINED_COUNT" ), QObject::tr( "Count of retained records" ) ) ); + addOutput( new QgsProcessingOutputNumber( QStringLiteral( "DUPLICATE_COUNT" ), QObject::tr( "Count of discarded duplicate records" ) ) ); +} + +QString QgsRemoveDuplicatesByAttributeAlgorithm::shortHelpString() const +{ + return QObject::tr( "Removes duplicate rows by a field value (or multiple field values). The first matching row will be retained, and duplicates will be discarded.\n\n" + "Optionally, these duplicate records can be saved to a separate output for analysis." ); +} + +QString QgsRemoveDuplicatesByAttributeAlgorithm::shortDescription() const +{ + return QObject::tr( "Removes duplicate rows by a field value (or multiple field values)." ); +} + +QgsRemoveDuplicatesByAttributeAlgorithm *QgsRemoveDuplicatesByAttributeAlgorithm::createInstance() const +{ + return new QgsRemoveDuplicatesByAttributeAlgorithm(); +} + +QVariantMap QgsRemoveDuplicatesByAttributeAlgorithm::processAlgorithm( const QVariantMap ¶meters, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) +{ + std::unique_ptr< QgsProcessingFeatureSource > source( parameterAsSource( parameters, QStringLiteral( "INPUT" ), context ) ); + if ( !source ) + throw QgsProcessingException( invalidSourceError( parameters, QStringLiteral( "INPUT" ) ) ); + + const QStringList fieldNames = parameterAsFields( parameters, QStringLiteral( "FIELDS" ), context ); + + QgsAttributeList attributes; + for ( const QString &field : fieldNames ) + { + const int index = source->fields().lookupField( field ); + if ( index < 0 ) + feedback->reportError( QObject::tr( "Field %1 not found in INPUT layer, skipping" ).arg( field ) ); + else + attributes.append( index ); + } + if ( attributes.isEmpty() ) + throw QgsProcessingException( QObject::tr( "No input fields found" ) ); + + + QString noDupeSinkId; + std::unique_ptr< QgsFeatureSink > noDupeSink( parameterAsSink( parameters, QStringLiteral( "OUTPUT" ), context, noDupeSinkId, source->fields(), + source->wkbType(), source->sourceCrs() ) ); + if ( !noDupeSink ) + throw QgsProcessingException( invalidSinkError( parameters, QStringLiteral( "OUTPUT" ) ) ); + + QString dupeSinkId; + std::unique_ptr< QgsFeatureSink > dupesSink( parameterAsSink( parameters, QStringLiteral( "DUPLICATES" ), context, dupeSinkId, source->fields(), + source->wkbType(), source->sourceCrs() ) ); + + const long count = source->featureCount(); + double step = count > 0 ? 100.0 / count : 1; + int current = 0; + + long long keptCount = 0; + long long discardedCount = 0; + + QSet< QVariantList > matched; + + QgsFeatureIterator it = source->getFeatures( QgsFeatureRequest(), QgsProcessingFeatureSource::FlagSkipGeometryValidityChecks ); + QgsFeature f; + + QVariantList dupeKey; + dupeKey.reserve( attributes.size() ); + for ( int i : attributes ) + { + ( void )i; + dupeKey.append( QVariant() ); + } + + while ( it.nextFeature( f ) ) + { + if ( feedback->isCanceled() ) + { + break; + } + + int i = 0; + for ( int attr : attributes ) + dupeKey[i++] = f.attribute( attr ); + + if ( matched.contains( dupeKey ) ) + { + // duplicate + discardedCount++; + if ( dupesSink ) + dupesSink->addFeature( f, QgsFeatureSink::FastInsert ); + } + else + { + // not duplicate + keptCount++; + matched.insert( dupeKey ); + noDupeSink->addFeature( f, QgsFeatureSink::FastInsert ); + } + + feedback->setProgress( current * step ); + current++; + } + + QVariantMap outputs; + outputs.insert( QStringLiteral( "RETAINED_COUNT" ), keptCount ); + outputs.insert( QStringLiteral( "DUPLICATE_COUNT" ), discardedCount ); + outputs.insert( QStringLiteral( "OUTPUT" ), noDupeSinkId ); + if ( dupesSink ) + outputs.insert( QStringLiteral( "DUPLICATES" ), dupeSinkId ); + return outputs; +} + +///@endcond + + diff --git a/src/analysis/processing/qgsalgorithmremoveduplicatesbyattribute.h b/src/analysis/processing/qgsalgorithmremoveduplicatesbyattribute.h new file mode 100644 index 00000000000..a91dd394137 --- /dev/null +++ b/src/analysis/processing/qgsalgorithmremoveduplicatesbyattribute.h @@ -0,0 +1,58 @@ +/*************************************************************************** + qgsalgorithmremoveduplicatesbyattribute.h + --------------------- + begin : October 2018 + copyright : (C) 2018 by Nyall Dawson + email : nyall dot dawson at gmail 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 QGSALGORITHMREMOVEDUPLICATESBYATTRIBUTE_H +#define QGSALGORITHMREMOVEDUPLICATESBYATTRIBUTE_H + +#define SIP_NO_FILE + +#include "qgis.h" +#include "qgsprocessingalgorithm.h" + +///@cond PRIVATE + +/** + * Native remove duplicates by attribute values algorithm. + */ +class QgsRemoveDuplicatesByAttributeAlgorithm : public QgsProcessingAlgorithm +{ + + public: + + QgsRemoveDuplicatesByAttributeAlgorithm() = 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; + QString shortDescription() const override; + QgsRemoveDuplicatesByAttributeAlgorithm *createInstance() const override SIP_FACTORY; + + protected: + + QVariantMap processAlgorithm( const QVariantMap ¶meters, + QgsProcessingContext &context, QgsProcessingFeedback *feedback ) override; + +}; + +///@endcond PRIVATE + +#endif // QGSALGORITHMREMOVEDUPLICATESBYATTRIBUTE + + diff --git a/src/analysis/processing/qgsnativealgorithms.cpp b/src/analysis/processing/qgsnativealgorithms.cpp index 48bf5216680..51cb3ec9333 100644 --- a/src/analysis/processing/qgsnativealgorithms.cpp +++ b/src/analysis/processing/qgsnativealgorithms.cpp @@ -69,6 +69,7 @@ #include "qgsalgorithmpromotetomultipart.h" #include "qgsalgorithmrasterlayeruniquevalues.h" #include "qgsalgorithmreclassifybylayer.h" +#include "qgsalgorithmremoveduplicatesbyattribute.h" #include "qgsalgorithmremoveduplicatevertices.h" #include "qgsalgorithmremoveholes.h" #include "qgsalgorithmremovenullgeometry.h" @@ -198,6 +199,7 @@ void QgsNativeAlgorithms::loadAlgorithms() addAlgorithm( new QgsAlgorithmRemoveDuplicateVertices() ); addAlgorithm( new QgsReclassifyByLayerAlgorithm() ); addAlgorithm( new QgsReclassifyByTableAlgorithm() ); + addAlgorithm( new QgsRemoveDuplicatesByAttributeAlgorithm() ); addAlgorithm( new QgsRemoveHolesAlgorithm() ); addAlgorithm( new QgsRemoveNullGeometryAlgorithm() ); addAlgorithm( new QgsRenameLayerAlgorithm() );