22using namespace Qt::StringLiterals;
26QString QgsAddUniqueValueIndexAlgorithm::name()
const
28 return u
"adduniquevalueindexfield"_s;
31QString QgsAddUniqueValueIndexAlgorithm::displayName()
const
33 return QObject::tr(
"Add unique value index field" );
36QStringList QgsAddUniqueValueIndexAlgorithm::tags()
const
38 return QObject::tr(
"categorize,categories,category,reclassify,classes,create" ).split(
',' );
41QString QgsAddUniqueValueIndexAlgorithm::group()
const
43 return QObject::tr(
"Vector table" );
46QString QgsAddUniqueValueIndexAlgorithm::groupId()
const
48 return u
"vectortable"_s;
51void QgsAddUniqueValueIndexAlgorithm::initAlgorithm(
const QVariantMap & )
58 classedOutput->setCreateByDefault(
true );
59 addParameter( classedOutput.release() );
61 auto summaryOutput = std::make_unique<QgsProcessingParameterFeatureSink>( u
"SUMMARY_OUTPUT"_s, QObject::tr(
"Class summary" ),
Qgis::ProcessingSourceType::Vector, QVariant(),
true );
62 summaryOutput->setCreateByDefault(
false );
63 addParameter( summaryOutput.release() );
66QString QgsAddUniqueValueIndexAlgorithm::shortHelpString()
const
68 return QObject::tr(
"This algorithm takes a vector layer and an attribute and adds a new numeric field. Values in this field correspond to values in the specified attribute, so features with the same "
69 "value for the attribute will have the same value in the new numeric field. This creates a numeric equivalent of the specified attribute, which defines the same classes.\n\n"
70 "The new attribute is not added to the input layer but a new layer is generated instead.\n\n"
71 "Optionally, a separate table can be output which contains a summary of the class field values mapped to the new unique numeric value." );
74QString QgsAddUniqueValueIndexAlgorithm::shortDescription()
const
76 return QObject::tr(
"Adds a numeric field and assigns the same index to features of the same attribute value." );
79QgsAddUniqueValueIndexAlgorithm *QgsAddUniqueValueIndexAlgorithm::createInstance()
const
81 return new QgsAddUniqueValueIndexAlgorithm();
86 std::unique_ptr<QgsProcessingFeatureSource> source( parameterAsSource( parameters, u
"INPUT"_s, context ) );
90 const QString newFieldName = parameterAsString( parameters, u
"FIELD_NAME"_s, context );
95 throw QgsProcessingException( QObject::tr(
"A field with the same name (%1) already exists" ).arg( newFieldName ) );
102 std::unique_ptr<QgsFeatureSink> sink( parameterAsSink( parameters, u
"OUTPUT"_s, context, dest, fields, source->wkbType(), source->sourceCrs() ) );
104 const QString sourceFieldName = parameterAsString( parameters, u
"FIELD"_s, context );
105 const int fieldIndex = source->fields().lookupField( sourceFieldName );
106 if ( fieldIndex < 0 )
111 summaryFields.
append( newField );
112 summaryFields.
append( source->fields().at( fieldIndex ) );
113 std::unique_ptr<QgsFeatureSink> summarySink( parameterAsSink( parameters, u
"SUMMARY_OUTPUT"_s, context, summaryDest, summaryFields,
Qgis::WkbType::NoGeometry ) );
115 QHash<QVariant, int> classes;
119 const long count = source->featureCount();
120 const double step = count > 0 ? 100.0 / count : 1;
131 const QVariant clazz = attributes.at( fieldIndex );
133 int thisValue = classes.value( clazz, -1 );
134 if ( thisValue == -1 )
136 thisValue = classes.count();
137 classes.insert( clazz, thisValue );
142 attributes.append( thisValue );
155 QMap<int, QVariant> sorted;
156 for (
auto classIt = classes.constBegin(); classIt != classes.constEnd(); ++classIt )
158 sorted.insert( classIt.value(), classIt.key() );
161 for (
auto sortedIt = sorted.constBegin(); sortedIt != sorted.constEnd(); ++sortedIt )
174 results.insert( u
"OUTPUT"_s, dest );
178 summarySink->finalize();
179 results.insert( u
"SUMMARY_OUTPUT"_s, summaryDest );
@ 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...
void setAttributes(const QgsAttributes &attrs)
Sets the feature's attributes.
bool isCanceled() const
Tells whether the operation has been canceled already.
void setProgress(double progress)
Sets the current progress for the feedback object.
Encapsulate a field in an attribute table or data source.
Container of fields for a vector layer.
bool append(const QgsField &field, Qgis::FieldOrigin origin=Qgis::FieldOrigin::Provider, int originIndex=-1)
Appends a field.
Q_INVOKABLE int lookupField(const QString &fieldName) const
Looks up field's index from the field name.
Contains information about the context in which a processing algorithm is executed.
Custom exception class for processing related exceptions.
Base class for providing feedback from a processing algorithm.
An input feature source (such as vector layers) parameter for processing algorithms.
A vector layer or feature source field parameter for processing algorithms.
A string parameter for processing algorithms.