26using namespace Qt::StringLiterals;
30QString QgsExplodeHstoreAlgorithm::name()
const
32 return u
"explodehstorefield"_s;
35QString QgsExplodeHstoreAlgorithm::displayName()
const
37 return QObject::tr(
"Explode HStore Field" );
40QStringList QgsExplodeHstoreAlgorithm::tags()
const
42 return QObject::tr(
"field,explode,hstore,osm,openstreetmap" ).split(
',' );
45QString QgsExplodeHstoreAlgorithm::group()
const
47 return QObject::tr(
"Vector table" );
50QString QgsExplodeHstoreAlgorithm::groupId()
const
52 return u
"vectortable"_s;
55QString QgsExplodeHstoreAlgorithm::shortHelpString()
const
58 "This algorithm creates a copy of the input layer and adds a new field for every unique key in the HStore field.\n"
59 "The expected field list is an optional comma separated list. By default, all unique keys are added. If this list is specified, only these fields are added and the HStore field is updated."
63QString QgsExplodeHstoreAlgorithm::shortDescription()
const
65 return QObject::tr(
"Creates a copy of the input layer and adds a new field for every unique key in the HStore field." );
70 return new QgsExplodeHstoreAlgorithm();
73void QgsExplodeHstoreAlgorithm::initAlgorithm(
const QVariantMap & )
77 addParameter(
new QgsProcessingParameterString( u
"EXPECTED_FIELDS"_s, QObject::tr(
"Expected list of fields separated by a comma" ), QVariant(),
false,
true ) );
83 QGS_MARK_ALGORITHM_SOURCE
85 std::unique_ptr<QgsProcessingFeatureSource> source( parameterAsSource( parameters, u
"INPUT"_s, context ) );
88 int attrSourceCount = source->fields().count();
90 QString fieldName = parameterAsString( parameters, u
"FIELD"_s, context );
91 int fieldIndex = source->fields().lookupField( fieldName );
95 QStringList expectedFields;
96 QString fieldList = parameterAsString( parameters, u
"EXPECTED_FIELDS"_s, context );
97 if ( !fieldList.trimmed().isEmpty() )
99 expectedFields = fieldList.split(
',' );
102 QList<QString> fieldsToAdd;
103 QHash<QgsFeatureId, QVariantMap> hstoreFeatures;
104 QList<QgsFeature> features;
106 double step = source->featureCount() > 0 ? 50.0 / source->featureCount() : 1;
116 double progress = i * step;
117 if ( progress >= 50 )
123 for (
auto key = currentHStore.keyBegin(); key != currentHStore.keyEnd(); key++ )
125 if ( expectedFields.isEmpty() && !fieldsToAdd.contains( *key ) )
126 fieldsToAdd.insert( 0, *key );
128 hstoreFeatures.insert( feat.
id(), currentHStore );
129 features.append( feat );
132 if ( !expectedFields.isEmpty() )
134 fieldsToAdd = expectedFields;
138 for (
const QString &fieldName : fieldsToAdd )
140 hstoreFields.
append(
QgsField( fieldName, QMetaType::Type::QString ) );
146 std::unique_ptr<QgsFeatureSink> sink( parameterAsSink( parameters, u
"OUTPUT"_s, context, sinkId, outFields, source->wkbType(), source->sourceCrs() ) );
151 int attrCount = attrSourceCount + fieldsToAdd.count();
153 step = !features.empty() ? 50.0 / features.count() : 1;
155 for (
const QgsFeature &feat : std::as_const( features ) )
166 for (
int i = 0; i < fieldIndicesInput.count(); ++i )
167 outAttributes[i] = attrs[fieldIndicesInput[i]];
169 QVariantMap currentHStore = hstoreFeatures.take( feat.
id() );
172 for (
int i = 0; i < fieldsToAdd.count(); ++i )
174 current = fieldsToAdd.at( i );
175 if ( currentHStore.contains( current ) )
177 outAttributes[attrSourceCount + i] = currentHStore.take( current );
181 if ( !expectedFields.isEmpty() )
198 outputs.insert( u
"OUTPUT"_s, sinkId );
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.
@ 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.
void setGeometry(const QgsGeometry &geometry)
Set the feature's geometry.
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.
A geometry is the spatial representation of a feature.
static QString build(const QVariantMap &map)
Build a hstore-formatted string from a QVariantMap.
static QVariantMap parse(const QString &string)
Returns a QVariantMap object containing the key and values from a hstore-formatted string.
Abstract base class for processing algorithms.
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.
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 string parameter for processing algorithms.
static QList< int > fieldNamesToIndices(const QStringList &fieldNames, const QgsFields &fields)
Returns a list of field indices parsed from the given list of field names.
static QgsFields combineFields(const QgsFields &fieldsA, const QgsFields &fieldsB, const QString &fieldsBPrefix=QString())
Combines two field lists, avoiding duplicate field names (in a case-insensitive manner).