22 QString QgsAddUniqueValueIndexAlgorithm::name()
const 24 return QStringLiteral(
"adduniquevalueindexfield" );
27 QString QgsAddUniqueValueIndexAlgorithm::displayName()
const 29 return QObject::tr(
"Add unique value index field" );
32 QStringList QgsAddUniqueValueIndexAlgorithm::tags()
const 34 return QObject::tr(
"categorize,categories,category,reclassify,classes,create" ).split(
',' );
37 QString QgsAddUniqueValueIndexAlgorithm::group()
const 39 return QObject::tr(
"Vector table" );
42 QString QgsAddUniqueValueIndexAlgorithm::groupId()
const 44 return QStringLiteral(
"vectortable" );
47 void QgsAddUniqueValueIndexAlgorithm::initAlgorithm(
const QVariantMap & )
53 QObject::tr(
"Output field name" ), QStringLiteral(
"NUM_FIELD" ) ) );
55 std::unique_ptr< QgsProcessingParameterFeatureSink > classedOutput = qgis::make_unique< QgsProcessingParameterFeatureSink >( QStringLiteral(
"OUTPUT" ), QObject::tr(
"Layer with index field" ),
QgsProcessing::TypeVectorAnyGeometry, QVariant(), true );
56 classedOutput->setCreateByDefault(
true );
57 addParameter( classedOutput.release() );
59 std::unique_ptr< QgsProcessingParameterFeatureSink > summaryOutput = qgis::make_unique< QgsProcessingParameterFeatureSink >( QStringLiteral(
"SUMMARY_OUTPUT" ), QObject::tr(
"Class summary" ),
61 summaryOutput->setCreateByDefault(
false );
62 addParameter( summaryOutput.release() );
65 QString QgsAddUniqueValueIndexAlgorithm::shortHelpString()
const 67 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 " 68 "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" 69 "The new attribute is not added to the input layer but a new layer is generated instead.\n\n" 70 "Optionally, a separate table can be output which contains a summary of the class field values mapped to the new unique numeric value." );
73 QgsAddUniqueValueIndexAlgorithm *QgsAddUniqueValueIndexAlgorithm::createInstance()
const 75 return new QgsAddUniqueValueIndexAlgorithm();
80 std::unique_ptr< QgsProcessingFeatureSource > source( parameterAsSource( parameters, QStringLiteral(
"INPUT" ), context ) );
84 QString newFieldName = parameterAsString( parameters, QStringLiteral(
"FIELD_NAME" ), context );
90 std::unique_ptr< QgsFeatureSink > sink( parameterAsSink( parameters, QStringLiteral(
"OUTPUT" ), context, dest, fields, source->wkbType(), source->sourceCrs() ) );
92 QString sourceFieldName = parameterAsString( parameters, QStringLiteral(
"FIELD" ), context );
93 int fieldIndex = source->fields().lookupField( sourceFieldName );
99 summaryFields.
append( newField );
100 summaryFields.
append( source->fields().at( fieldIndex ) );
101 std::unique_ptr< QgsFeatureSink > summarySink( parameterAsSink( parameters, QStringLiteral(
"SUMMARY_OUTPUT" ), context, summaryDest, summaryFields,
QgsWkbTypes::NoGeometry ) );
103 QHash< QVariant, int > classes;
107 long count = source->featureCount();
108 double step = count > 0 ? 100.0 / count : 1;
119 QVariant clazz = attributes.at( fieldIndex );
121 int thisValue = classes.value( clazz, -1 );
122 if ( thisValue == -1 )
124 thisValue = classes.count();
125 classes.insert( clazz, thisValue );
130 attributes.append( thisValue );
142 QMap< int, QVariant > sorted;
143 for (
auto classIt = classes.constBegin(); classIt != classes.constEnd(); ++classIt )
145 sorted.insert( classIt.value(), classIt.key() );
148 for (
auto sortedIt = sorted.constBegin(); sortedIt != sorted.constEnd(); ++sortedIt )
158 results.insert( QStringLiteral(
"OUTPUT" ), dest );
160 results.insert( QStringLiteral(
"SUMMARY_OUTPUT" ), summaryDest );
Wrapper for iterator of features from vector data provider or vector layer.
Use faster inserts, at the cost of updating the passed features to reflect changes made at the provid...
Base class for providing feedback from a processing algorithm.
Invalid geometry checks should always be skipped. This flag can be useful for algorithms which always...
A vector layer or feature source field parameter for processing algorithms.
void setProgress(double progress)
Sets the current progress for the feedback object.
Container of fields for a vector layer.
void setAttributes(const QgsAttributes &attrs)
Sets the feature's attributes.
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
This class wraps a request for features to a vector layer (or directly its vector data provider)...
Custom exception class for processing related exceptions.
bool append(const QgsField &field, FieldOrigin origin=OriginProvider, int originIndex=-1)
Append a field. The field must have unique name, otherwise it is rejected (returns false) ...
Encapsulate a field in an attribute table or data source.
bool isCanceled() const
Tells whether the operation has been canceled already.
An input feature source (such as vector layers) parameter for processing algorithms.
Tables (i.e. vector layers with or without geometry). When used for a sink this indicates the sink ha...
bool nextFeature(QgsFeature &f)
Contains information about the context in which a processing algorithm is executed.
A string parameter for processing algorithms.
Any vector layer with geometry.