22QString QgsAddUniqueValueIndexAlgorithm::name()
const
24 return QStringLiteral(
"adduniquevalueindexfield" );
27QString QgsAddUniqueValueIndexAlgorithm::displayName()
const
29 return QObject::tr(
"Add unique value index field" );
32QStringList QgsAddUniqueValueIndexAlgorithm::tags()
const
34 return QObject::tr(
"categorize,categories,category,reclassify,classes,create" ).split(
',' );
37QString QgsAddUniqueValueIndexAlgorithm::group()
const
39 return QObject::tr(
"Vector table" );
42QString QgsAddUniqueValueIndexAlgorithm::groupId()
const
44 return QStringLiteral(
"vectortable" );
47void QgsAddUniqueValueIndexAlgorithm::initAlgorithm(
const QVariantMap & )
51 addParameter(
new QgsProcessingParameterString( QStringLiteral(
"FIELD_NAME" ), QObject::tr(
"Output field name" ), QStringLiteral(
"NUM_FIELD" ) ) );
53 std::unique_ptr<QgsProcessingParameterFeatureSink> classedOutput = std::make_unique<QgsProcessingParameterFeatureSink>( QStringLiteral(
"OUTPUT" ), QObject::tr(
"Layer with index field" ),
Qgis::ProcessingSourceType::VectorAnyGeometry, QVariant(),
true );
54 classedOutput->setCreateByDefault(
true );
55 addParameter( classedOutput.release() );
57 std::unique_ptr<QgsProcessingParameterFeatureSink> summaryOutput = std::make_unique<QgsProcessingParameterFeatureSink>( QStringLiteral(
"SUMMARY_OUTPUT" ), QObject::tr(
"Class summary" ),
Qgis::ProcessingSourceType::Vector, QVariant(),
true );
58 summaryOutput->setCreateByDefault(
false );
59 addParameter( summaryOutput.release() );
62QString QgsAddUniqueValueIndexAlgorithm::shortHelpString()
const
64 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 "
65 "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"
66 "The new attribute is not added to the input layer but a new layer is generated instead.\n\n"
67 "Optionally, a separate table can be output which contains a summary of the class field values mapped to the new unique numeric value." );
70QgsAddUniqueValueIndexAlgorithm *QgsAddUniqueValueIndexAlgorithm::createInstance()
const
72 return new QgsAddUniqueValueIndexAlgorithm();
77 std::unique_ptr<QgsProcessingFeatureSource> source( parameterAsSource( parameters, QStringLiteral(
"INPUT" ), context ) );
81 const QString newFieldName = parameterAsString( parameters, QStringLiteral(
"FIELD_NAME" ), context );
87 std::unique_ptr<QgsFeatureSink> sink( parameterAsSink( parameters, QStringLiteral(
"OUTPUT" ), context, dest, fields, source->wkbType(), source->sourceCrs() ) );
89 const QString sourceFieldName = parameterAsString( parameters, QStringLiteral(
"FIELD" ), context );
90 const int fieldIndex = source->fields().lookupField( sourceFieldName );
96 summaryFields.
append( newField );
97 summaryFields.
append( source->fields().at( fieldIndex ) );
98 std::unique_ptr<QgsFeatureSink> summarySink( parameterAsSink( parameters, QStringLiteral(
"SUMMARY_OUTPUT" ), context, summaryDest, summaryFields,
Qgis::WkbType::NoGeometry ) );
100 QHash<QVariant, int> classes;
104 const long count = source->featureCount();
105 const double step = count > 0 ? 100.0 / count : 1;
116 const QVariant clazz = attributes.at( fieldIndex );
118 int thisValue = classes.value( clazz, -1 );
119 if ( thisValue == -1 )
121 thisValue = classes.count();
122 classes.insert( clazz, thisValue );
127 attributes.append( thisValue );
140 QMap<int, QVariant> sorted;
141 for (
auto classIt = classes.constBegin(); classIt != classes.constEnd(); ++classIt )
143 sorted.insert( classIt.value(), classIt.key() );
146 for (
auto sortedIt = sorted.constBegin(); sortedIt != sorted.constEnd(); ++sortedIt )
151 throw QgsProcessingException( writeFeatureError( summarySink.get(), parameters, QStringLiteral(
"SUMMARY_OUTPUT" ) ) );
159 results.insert( QStringLiteral(
"OUTPUT" ), dest );
163 summarySink->finalize();
164 results.insert( QStringLiteral(
"SUMMARY_OUTPUT" ), 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.
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...
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.
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.