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 QGS_MARK_ALGORITHM_SOURCE
80 std::unique_ptr<QgsProcessingFeatureSource> source( parameterAsSource( parameters, u
"INPUT"_s, context ) );
84 const QStringList fieldNames = parameterAsStrings( parameters, u
"FIELDS"_s, context );
85 const QString outputHtml = parameterAsFileOutput( parameters, u
"OUTPUT_HTML_FILE"_s, context );
88 QList<int> fieldIndices;
90 for (
const QString &fieldName : fieldNames )
92 int fieldIndex = source->fields().lookupField( fieldName );
95 feedback->
reportError( QObject::tr(
"Invalid field name %1" ).arg( fieldName ) );
98 fields.
append( source->fields().at( fieldIndex ) );
99 fieldIndices << fieldIndex;
109 QSet<QgsAttributes> values;
110 if ( fieldIndices.size() == 1 )
112 const QSet<QVariant> unique = source->uniqueValues( fieldIndices.at( 0 ) );
113 for (
const QVariant &v : unique )
126 const double step = source->featureCount() > 0 ? 100.0 / source->featureCount() : 0;
136 for (
auto &i : std::as_const( fieldIndices ) )
140 values.insert( attrs );
148 outputs.insert( u
"TOTAL_VALUES"_s, values.size() );
150 QStringList valueList;
151 for (
auto it = values.constBegin(); it != values.constEnd(); ++it )
154 for (
const QVariant &v : std::as_const( *it ) )
156 s.append( v.toString() );
158 valueList.append( s.join(
',' ) );
160 outputs.insert( u
"UNIQUE_VALUES"_s, valueList.join(
';' ) );
164 for (
auto it = values.constBegin(); it != values.constEnd(); ++it )
178 outputs.insert( u
"OUTPUT"_s, dest );
181 if ( !outputHtml.isEmpty() )
183 QFile file( outputHtml );
184 if ( file.open( QIODevice::WriteOnly | QIODevice::Truncate ) )
186 QTextStream out( &file );
187 out << u
"<html><head><meta http-equiv=\"Content-Type\" content=\"text/html;charset=utf-8\"/></head><body>\n"_s;
188 out << QObject::tr(
"<p>Total unique values: %1</p>" ).arg( values.size() );
189 out << QObject::tr(
"<p>Unique values:</p>" );
191 for (
auto &v : std::as_const( valueList ) )
193 out << u
"<li>%1</li>"_s.arg( v );
195 out << u
"</ul></body></html>"_s;
197 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.
void featureAddedToSink(const QString &output)
Reports that a feature was added to the the sink associated with the specified algorithm output.
void featureSinkFinalized(const QString &output)
Reports that a feature sink has been finalized.
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...