Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[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.
- Loading branch information
1 parent
1151639
commit 4e9da6f
Showing
4 changed files
with
232 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
171 changes: 171 additions & 0 deletions
171
src/analysis/processing/qgsalgorithmremoveduplicatesbyattribute.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
|
||
|
58 changes: 58 additions & 0 deletions
58
src/analysis/processing/qgsalgorithmremoveduplicatesbyattribute.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters