25 QString QgsExplodeHstoreAlgorithm::name()
const 27 return QStringLiteral(
"explodehstorefield" );
30 QString QgsExplodeHstoreAlgorithm::displayName()
const 32 return QObject::tr(
"Explode HStore Field" );
35 QStringList QgsExplodeHstoreAlgorithm::tags()
const 37 return QObject::tr(
"field,explode,hstore,osm,openstreetmap" ).split(
',' );
40 QString QgsExplodeHstoreAlgorithm::group()
const 42 return QObject::tr(
"Vector table" );
45 QString QgsExplodeHstoreAlgorithm::groupId()
const 47 return QStringLiteral(
"vectortable" );
50 QString QgsExplodeHstoreAlgorithm::shortHelpString()
const 52 return QObject::tr(
"This algorithm creates a copy of the input layer and adds a new field for every unique key in the HStore field.\n" 53 "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." );
58 return new QgsExplodeHstoreAlgorithm();
61 void QgsExplodeHstoreAlgorithm::initAlgorithm(
const QVariantMap & )
64 QObject::tr(
"Input layer" ) ) );
66 QObject::tr(
"HStore field" ), QVariant(), QStringLiteral(
"INPUT" ) ) );
67 addParameter(
new QgsProcessingParameterString( QStringLiteral(
"EXPECTED_FIELDS" ), QObject::tr(
"Expected list of fields separated by a comma" ), QVariant(),
false,
true ) );
73 std::unique_ptr< QgsProcessingFeatureSource > source( parameterAsSource( parameters, QStringLiteral(
"INPUT" ), context ) );
76 int attrSourceCount = source->fields().count();
78 QString fieldName = parameterAsString( parameters, QStringLiteral(
"FIELD" ), context );
79 int fieldIndex = source->fields().lookupField( fieldName );
83 QStringList expectedFields;
84 QString fieldList = parameterAsString( parameters, QStringLiteral(
"EXPECTED_FIELDS" ), context );
85 if ( ! fieldList.trimmed().isEmpty() )
87 expectedFields = fieldList.split(
',' );
90 QList<QString> fieldsToAdd;
91 QHash<QgsFeatureId, QVariantMap> hstoreFeatures;
92 QList<QgsFeature> features;
94 double step = source->featureCount() > 0 ? 50.0 / source->featureCount() : 1;
104 double progress = i * step;
105 if ( progress >= 50 )
111 for (
const QString &key : currentHStore.keys() )
113 if ( expectedFields.isEmpty() && ! fieldsToAdd.contains( key ) )
114 fieldsToAdd.insert( 0, key );
116 hstoreFeatures.insert( feat.
id(), currentHStore );
117 features.append( feat );
120 if ( ! expectedFields.isEmpty() )
122 fieldsToAdd = expectedFields;
126 for (
const QString &fieldName : fieldsToAdd )
134 std::unique_ptr< QgsFeatureSink > sink( parameterAsSink( parameters, QStringLiteral(
"OUTPUT" ), context, sinkId, outFields, source->wkbType(), source->sourceCrs() ) );
139 int attrCount = attrSourceCount + fieldsToAdd.count();
141 step = !features.empty() ? 50.0 / features.count() : 1;
143 for (
const QgsFeature &feat : qgis::as_const( features ) )
154 for (
int i = 0; i < fieldIndicesInput.count(); ++i )
155 outAttributes[i] = attrs[fieldIndicesInput[i]];
157 QVariantMap currentHStore = hstoreFeatures.take( feat.
id() );
160 for (
int i = 0; i < fieldsToAdd.count(); ++i )
162 current = fieldsToAdd.at( i );
163 if ( currentHStore.contains( current ) )
165 outAttributes[attrSourceCount + i] = currentHStore.take( current );
169 if ( ! expectedFields.isEmpty() )
180 outputs.insert( QStringLiteral(
"OUTPUT" ), sinkId );
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.
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.
A geometry is the spatial representation of a feature.
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...
Abstract base class for processing algorithms.
A feature sink output for processing algorithms.
CORE_EXPORT QString build(const QVariantMap &map)
Build a hstore-formatted string from a QVariantMap.
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)
Combines two field lists, avoiding duplicate field names (in a case-insensitive manner).
Custom exception class for processing related exceptions.
bool append(const QgsField &field, FieldOrigin origin=OriginProvider, int originIndex=-1)
Appends 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.
void setGeometry(const QgsGeometry &geometry)
Set the feature's geometry.
bool nextFeature(QgsFeature &f)
CORE_EXPORT QVariantMap parse(const QString &string)
Returns a QVariantMap object containing the key and values from a hstore-formatted string...
QVariant attribute(const QString &name) const
Lookup attribute value from attribute name.
Contains information about the context in which a processing algorithm is executed.
A string parameter for processing algorithms.