22using namespace Qt::StringLiterals;
26QString QgsUniqueValuesAlgorithm::name()
const
28 return u
"listuniquevalues"_s;
31QString QgsUniqueValuesAlgorithm::displayName()
const
33 return QObject::tr(
"List unique values" );
36QStringList QgsUniqueValuesAlgorithm::tags()
const
38 return QObject::tr(
"count,unique,values" ).split(
',' );
41QString QgsUniqueValuesAlgorithm::group()
const
43 return QObject::tr(
"Vector analysis" );
46QString QgsUniqueValuesAlgorithm::groupId()
const
48 return u
"vectoranalysis"_s;
51QString QgsUniqueValuesAlgorithm::shortHelpString()
const
53 return QObject::tr(
"This algorithm generates a report with information about the unique values found in a given attribute (or attributes) of a vector layer." );
56QString QgsUniqueValuesAlgorithm::shortDescription()
const
58 return QObject::tr(
"Returns list of unique values in given field(s) of a vector layer." );
61QgsUniqueValuesAlgorithm *QgsUniqueValuesAlgorithm::createInstance()
const
63 return new QgsUniqueValuesAlgorithm();
66void QgsUniqueValuesAlgorithm::initAlgorithm(
const QVariantMap & )
78 std::unique_ptr<QgsProcessingFeatureSource> source( parameterAsSource( parameters, u
"INPUT"_s, context ) );
82 const QStringList fieldNames = parameterAsStrings( parameters, u
"FIELDS"_s, context );
83 const QString outputHtml = parameterAsFileOutput( parameters, u
"OUTPUT_HTML_FILE"_s, context );
86 QList<int> fieldIndices;
88 for (
const QString &fieldName : fieldNames )
90 int fieldIndex = source->fields().lookupField( fieldName );
93 feedback->
reportError( QObject::tr(
"Invalid field name %1" ).arg( fieldName ) );
96 fields.
append( source->fields().at( fieldIndex ) );
97 fieldIndices << fieldIndex;
107 QSet<QgsAttributes> values;
108 if ( fieldIndices.size() == 1 )
110 const QSet<QVariant> unique = source->uniqueValues( fieldIndices.at( 0 ) );
111 for (
const QVariant &v : unique )
124 const double step = source->featureCount() > 0 ? 100.0 / source->featureCount() : 0;
134 for (
auto &i : std::as_const( fieldIndices ) )
138 values.insert( attrs );
146 outputs.insert( u
"TOTAL_VALUES"_s, values.size() );
148 QStringList valueList;
149 for (
auto it = values.constBegin(); it != values.constEnd(); ++it )
152 for (
const QVariant &v : std::as_const( *it ) )
154 s.append( v.toString() );
156 valueList.append( s.join(
',' ) );
158 outputs.insert( u
"UNIQUE_VALUES"_s, valueList.join(
';' ) );
162 for (
auto it = values.constBegin(); it != values.constEnd(); ++it )
173 outputs.insert( u
"OUTPUT"_s, dest );
176 if ( !outputHtml.isEmpty() )
178 QFile file( outputHtml );
179 if ( file.open( QIODevice::WriteOnly | QIODevice::Truncate ) )
181 QTextStream out( &file );
182 out << u
"<html><head><meta http-equiv=\"Content-Type\" content=\"text/html;charset=utf-8\"/></head><body>\n"_s;
183 out << QObject::tr(
"<p>Total unique values: %1</p>" ).arg( values.size() );
184 out << QObject::tr(
"<p>Unique values:</p>" );
186 for (
auto &v : std::as_const( valueList ) )
188 out << u
"<li>%1</li>"_s.arg( v );
190 out << u
"</ul></body></html>"_s;
192 outputs.insert( u
"OUTPUT_HTML_FILE"_s, outputHtml );
@ Vector
Tables (i.e. vector layers with or without geometry). When used for a sink this indicates the sink ha...
@ NoGeometry
Geometry is not required. It may still be returned if e.g. required for a filter condition.
Represents a coordinate reference system (CRS).
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).
QgsFeatureRequest & setFlags(Qgis::FeatureRequestFlags flags)
Sets flags that affect how features will be fetched.
QgsFeatureRequest & setSubsetOfAttributes(const QgsAttributeList &attrs)
Set a subset of attributes that will be fetched.
@ 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.
Q_INVOKABLE QVariant attribute(const QString &name) const
Lookup attribute value by attribute name.
bool isCanceled() const
Tells whether the operation has been canceled already.
void setProgress(double progress)
Sets the current progress for the feedback object.
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.
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 string 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.
A generic file based destination parameter, for specifying the destination path for a file (non-map l...