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 & )
54 QObject::tr(
"Output field name" ), QStringLiteral(
"NUM_FIELD" ) ) );
56 std::unique_ptr< QgsProcessingParameterFeatureSink > classedOutput = qgis::make_unique< QgsProcessingParameterFeatureSink >( QStringLiteral(
"OUTPUT" ), QObject::tr(
"Layer with index field" ),
QgsProcessing::TypeVectorAnyGeometry, QVariant(),
true );
57 classedOutput->setCreateByDefault(
true );
58 addParameter( classedOutput.release() );
60 std::unique_ptr< QgsProcessingParameterFeatureSink > summaryOutput = qgis::make_unique< QgsProcessingParameterFeatureSink >( QStringLiteral(
"SUMMARY_OUTPUT" ), QObject::tr(
"Class summary" ),
62 summaryOutput->setCreateByDefault(
false );
63 addParameter( summaryOutput.release() );
66 QString 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." );
74 QgsAddUniqueValueIndexAlgorithm *QgsAddUniqueValueIndexAlgorithm::createInstance()
const
76 return new QgsAddUniqueValueIndexAlgorithm();
81 std::unique_ptr< QgsProcessingFeatureSource > source( parameterAsSource( parameters, QStringLiteral(
"INPUT" ), context ) );
85 QString newFieldName = parameterAsString( parameters, QStringLiteral(
"FIELD_NAME" ), context );
91 std::unique_ptr< QgsFeatureSink > sink( parameterAsSink( parameters, QStringLiteral(
"OUTPUT" ), context, dest, fields, source->wkbType(), source->sourceCrs() ) );
93 QString sourceFieldName = parameterAsString( parameters, QStringLiteral(
"FIELD" ), context );
94 int fieldIndex = source->fields().lookupField( sourceFieldName );
100 summaryFields.
append( newField );
101 summaryFields.
append( source->fields().at( fieldIndex ) );
102 std::unique_ptr< QgsFeatureSink > summarySink( parameterAsSink( parameters, QStringLiteral(
"SUMMARY_OUTPUT" ), context, summaryDest, summaryFields,
QgsWkbTypes::NoGeometry ) );
104 QHash< QVariant, int > classes;
108 long count = source->featureCount();
109 double step = count > 0 ? 100.0 / count : 1;
120 QVariant clazz = attributes.at( fieldIndex );
122 int thisValue = classes.value( clazz, -1 );
123 if ( thisValue == -1 )
125 thisValue = classes.count();
126 classes.insert( clazz, thisValue );
131 attributes.append( thisValue );
143 QMap< int, QVariant > sorted;
144 for (
auto classIt = classes.constBegin(); classIt != classes.constEnd(); ++classIt )
146 sorted.insert( classIt.value(), classIt.key() );
149 for (
auto sortedIt = sorted.constBegin(); sortedIt != sorted.constEnd(); ++sortedIt )
159 results.insert( QStringLiteral(
"OUTPUT" ), dest );
161 results.insert( QStringLiteral(
"SUMMARY_OUTPUT" ), summaryDest );