22using namespace Qt::StringLiterals;
26QString QgsRemoveDuplicatesByAttributeAlgorithm::name()
const
28 return u
"removeduplicatesbyattribute"_s;
31QString QgsRemoveDuplicatesByAttributeAlgorithm::displayName()
const
33 return QObject::tr(
"Delete duplicates by attribute" );
36QStringList QgsRemoveDuplicatesByAttributeAlgorithm::tags()
const
38 return QObject::tr(
"drop,remove,field,value,same,filter" ).split(
',' );
41QString QgsRemoveDuplicatesByAttributeAlgorithm::group()
const
43 return QObject::tr(
"Vector general" );
46QString QgsRemoveDuplicatesByAttributeAlgorithm::groupId()
const
48 return u
"vectorgeneral"_s;
51void QgsRemoveDuplicatesByAttributeAlgorithm::initAlgorithm(
const QVariantMap & )
59 addParameter( failOutput );
62 addOutput(
new QgsProcessingOutputNumber( u
"DUPLICATE_COUNT"_s, QObject::tr(
"Count of discarded duplicate records" ) ) );
65QString QgsRemoveDuplicatesByAttributeAlgorithm::shortHelpString()
const
67 return QObject::tr(
"This algorithm 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"
68 "Optionally, these duplicate records can be saved to a separate output for analysis." );
71QString QgsRemoveDuplicatesByAttributeAlgorithm::shortDescription()
const
73 return QObject::tr(
"Removes duplicate rows by a field value (or multiple field values)." );
76QgsRemoveDuplicatesByAttributeAlgorithm *QgsRemoveDuplicatesByAttributeAlgorithm::createInstance()
const
78 return new QgsRemoveDuplicatesByAttributeAlgorithm();
83 std::unique_ptr<QgsProcessingFeatureSource> source( parameterAsSource( parameters, u
"INPUT"_s, context ) );
87 const QStringList fieldNames = parameterAsStrings( parameters, u
"FIELDS"_s, context );
90 for (
const QString &field : fieldNames )
92 const int index = source->fields().lookupField( field );
94 feedback->
reportError( QObject::tr(
"Field %1 not found in INPUT layer, skipping" ).arg( field ) );
96 attributes.append( index );
98 if ( attributes.isEmpty() )
102 QString noDupeSinkId;
103 std::unique_ptr<QgsFeatureSink> noDupeSink( parameterAsSink( parameters, u
"OUTPUT"_s, context, noDupeSinkId, source->fields(), source->wkbType(), source->sourceCrs() ) );
108 std::unique_ptr<QgsFeatureSink> dupesSink( parameterAsSink( parameters, u
"DUPLICATES"_s, context, dupeSinkId, source->fields(), source->wkbType(), source->sourceCrs() ) );
110 const long count = source->featureCount();
111 const double step = count > 0 ? 100.0 / count : 1;
114 long long keptCount = 0;
115 long long discardedCount = 0;
117 QSet<QVariantList> matched;
122 QVariantList dupeKey;
123 dupeKey.reserve( attributes.size() );
124 for (
const int i : attributes )
127 dupeKey.append( QVariant() );
138 for (
const int attr : attributes )
141 if ( matched.contains( dupeKey ) )
155 matched.insert( dupeKey );
165 noDupeSink->finalize();
168 outputs.insert( u
"RETAINED_COUNT"_s, keptCount );
169 outputs.insert( u
"DUPLICATE_COUNT"_s, discardedCount );
170 outputs.insert( u
"OUTPUT"_s, noDupeSinkId );
173 dupesSink->finalize();
174 outputs.insert( u
"DUPLICATES"_s, dupeSinkId );
@ Vector
Tables (i.e. vector layers with or without geometry). When used for a sink this indicates the sink ha...
@ VectorAnyGeometry
Any vector layer with geometry.
@ SkipGeometryValidityChecks
Invalid geometry checks should always be skipped. This flag can be useful for algorithms which always...
Wrapper for iterator of features from vector data provider or vector layer.
bool nextFeature(QgsFeature &f)
Fetch next feature and stores in f, returns true on success.
Wraps a request for features to a vector layer (or directly its vector data provider).
@ FastInsert
Use faster inserts, at the cost of updating the passed features to reflect changes made at the provid...
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Q_INVOKABLE QVariant attribute(const QString &name) const
Lookup attribute value by attribute name.
bool isCanceled() const
Tells whether the operation has been canceled already.
void setProgress(double progress)
Sets the current progress for the feedback object.
Contains information about the context in which a processing algorithm is executed.
void setCreateByDefault(bool createByDefault)
Sets whether the destination should be created by default.
Custom exception class for processing related exceptions.
Base class for providing feedback from a processing algorithm.
virtual void reportError(const QString &error, bool fatalError=false)
Reports that the algorithm encountered an error while executing.
A numeric output for processing algorithms.
A feature sink output for processing algorithms.
An input feature source (such as vector layers) parameter for processing algorithms.
A vector layer or feature source field parameter for processing algorithms.
QList< int > QgsAttributeList