Skip to content

Commit

Permalink
[FEATURE][processing] New algorithm "Remove duplicates by attribute"
Browse files Browse the repository at this point in the history
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
nyalldawson committed Nov 1, 2018
1 parent 1151639 commit 4e9da6f
Show file tree
Hide file tree
Showing 4 changed files with 232 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/analysis/CMakeLists.txt
Expand Up @@ -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
Expand Down
171 changes: 171 additions & 0 deletions 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 &parameters, 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 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 &parameters,
QgsProcessingContext &context, QgsProcessingFeedback *feedback ) override;

};

///@endcond PRIVATE

#endif // QGSALGORITHMREMOVEDUPLICATESBYATTRIBUTE


2 changes: 2 additions & 0 deletions src/analysis/processing/qgsnativealgorithms.cpp
Expand Up @@ -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"
Expand Down Expand Up @@ -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() );
Expand Down

0 comments on commit 4e9da6f

Please sign in to comment.