22QString QgsRemoveDuplicatesByAttributeAlgorithm::name()
const
24 return QStringLiteral(
"removeduplicatesbyattribute" );
27QString QgsRemoveDuplicatesByAttributeAlgorithm::displayName()
const
29 return QObject::tr(
"Delete duplicates by attribute" );
32QStringList QgsRemoveDuplicatesByAttributeAlgorithm::tags()
const
34 return QObject::tr(
"drop,remove,field,value,same,filter" ).split(
',' );
37QString QgsRemoveDuplicatesByAttributeAlgorithm::group()
const
39 return QObject::tr(
"Vector general" );
42QString QgsRemoveDuplicatesByAttributeAlgorithm::groupId()
const
44 return QStringLiteral(
"vectorgeneral" );
47void QgsRemoveDuplicatesByAttributeAlgorithm::initAlgorithm(
const QVariantMap & )
55 addParameter( failOutput );
57 addOutput(
new QgsProcessingOutputNumber( QStringLiteral(
"RETAINED_COUNT" ), QObject::tr(
"Count of retained records" ) ) );
58 addOutput(
new QgsProcessingOutputNumber( QStringLiteral(
"DUPLICATE_COUNT" ), QObject::tr(
"Count of discarded duplicate records" ) ) );
61QString QgsRemoveDuplicatesByAttributeAlgorithm::shortHelpString()
const
63 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"
64 "Optionally, these duplicate records can be saved to a separate output for analysis." );
67QString QgsRemoveDuplicatesByAttributeAlgorithm::shortDescription()
const
69 return QObject::tr(
"Removes duplicate rows by a field value (or multiple field values)." );
72QgsRemoveDuplicatesByAttributeAlgorithm *QgsRemoveDuplicatesByAttributeAlgorithm::createInstance()
const
74 return new QgsRemoveDuplicatesByAttributeAlgorithm();
79 std::unique_ptr<QgsProcessingFeatureSource> source( parameterAsSource( parameters, QStringLiteral(
"INPUT" ), context ) );
83 const QStringList fieldNames = parameterAsStrings( parameters, QStringLiteral(
"FIELDS" ), context );
86 for (
const QString &field : fieldNames )
88 const int index = source->fields().lookupField( field );
90 feedback->
reportError( QObject::tr(
"Field %1 not found in INPUT layer, skipping" ).arg( field ) );
92 attributes.append( index );
94 if ( attributes.isEmpty() )
99 std::unique_ptr<QgsFeatureSink> noDupeSink( parameterAsSink( parameters, QStringLiteral(
"OUTPUT" ), context, noDupeSinkId, source->fields(), source->wkbType(), source->sourceCrs() ) );
104 std::unique_ptr<QgsFeatureSink> dupesSink( parameterAsSink( parameters, QStringLiteral(
"DUPLICATES" ), context, dupeSinkId, source->fields(), source->wkbType(), source->sourceCrs() ) );
106 const long count = source->featureCount();
107 const double step = count > 0 ? 100.0 / count : 1;
110 long long keptCount = 0;
111 long long discardedCount = 0;
113 QSet<QVariantList> matched;
118 QVariantList dupeKey;
119 dupeKey.reserve( attributes.size() );
120 for (
const int i : attributes )
123 dupeKey.append( QVariant() );
134 for (
const int attr : attributes )
135 dupeKey[i++] = f.attribute( attr );
137 if ( matched.contains( dupeKey ) )
144 throw QgsProcessingException( writeFeatureError( dupesSink.get(), parameters, QStringLiteral(
"DUPLICATES" ) ) );
151 matched.insert( dupeKey );
153 throw QgsProcessingException( writeFeatureError( noDupeSink.get(), parameters, QStringLiteral(
"OUTPUT" ) ) );
161 noDupeSink->finalize();
164 outputs.insert( QStringLiteral(
"RETAINED_COUNT" ), keptCount );
165 outputs.insert( QStringLiteral(
"DUPLICATE_COUNT" ), discardedCount );
166 outputs.insert( QStringLiteral(
"OUTPUT" ), noDupeSinkId );
169 dupesSink->finalize();
170 outputs.insert( QStringLiteral(
"DUPLICATES" ), 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.
This class 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...
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