QgsVectorLayerUtils class

Contains utility methods for working with QgsVectorLayers.

Public types

class QgsDuplicateFeatureContext
Contains mainly the QMap with QgsVectorLayer and QgsFeatureIds do list all the duplicated features.

Public static functions

static auto createFeature(const QgsVectorLayer* layer, const QgsGeometry& geometry = QgsGeometry(), const QgsAttributeMap& attributes = QgsAttributeMap(), QgsExpressionContext* context = nullptr) -> QgsFeature
Creates a new feature ready for insertion into a layer.
static auto createUniqueValue(const QgsVectorLayer* layer, int fieldIndex, const QVariant& seed = QVariant()) -> QVariant
Returns a new attribute value for the specified field index which is guaranteed to be unique.
static auto duplicateFeature(QgsVectorLayer* layer, const QgsFeature& feature, QgsProject* project, int depth, QgsDuplicateFeatureContext& duplicateFeatureContext) -> QgsFeature
Duplicates a feature and it's children (one level deep).
static auto getDoubleValues(const QgsVectorLayer* layer, const QString& fieldOrExpression, bool& ok, bool selectedOnly = false, int* nullCount = nullptr, QgsFeedback* feedback = nullptr) -> QList<double>
Fetches all double values from a specified field name or expression.
static auto getFeatureSource(QPointer<QgsVectorLayer> layer, QgsFeedback* feedback = nullptr) -> std::unique_ptr<QgsVectorLayerFeatureSource>
Gets the feature source from a QgsVectorLayer pointer.
static auto getValues(const QgsVectorLayer* layer, const QString& fieldOrExpression, bool& ok, bool selectedOnly = false, QgsFeedback* feedback = nullptr) -> QList<QVariant>
Fetches all values from a specified field name or expression.
static auto getValuesIterator(const QgsVectorLayer* layer, const QString& fieldOrExpression, bool& ok, bool selectedOnly) -> QgsFeatureIterator
Create a feature iterator for a specified field name or expression.
static auto makeFeatureCompatible(const QgsFeature& feature, const QgsVectorLayer* layer) -> QgsFeatureList
Converts input feature to be compatible with the given layer.
static auto makeFeaturesCompatible(const QgsFeatureList& features, const QgsVectorLayer* layer) -> QgsFeatureList
Converts input features to be compatible with the given layer.
static void matchAttributesToFields(QgsFeature& feature, const QgsFields& fields)
Matches the attributes in feature to the specified fields.
static auto validateAttribute(const QgsVectorLayer* layer, const QgsFeature& feature, int attributeIndex, QStringList& errors, QgsFieldConstraints::ConstraintStrength strength = QgsFieldConstraints::ConstraintStrengthNotSet, QgsFieldConstraints::ConstraintOrigin origin = QgsFieldConstraints::ConstraintOriginNotSet) -> bool
Tests an attribute value to check whether it passes all constraints which are present on the corresponding field.
static auto valueExists(const QgsVectorLayer* layer, int fieldIndex, const QVariant& value, const QgsFeatureIds& ignoreIds = QgsFeatureIds()) -> bool
Returns true if the specified value already exists within a field.

Function documentation

static QgsFeature QgsVectorLayerUtils::createFeature(const QgsVectorLayer* layer, const QgsGeometry& geometry = QgsGeometry(), const QgsAttributeMap& attributes = QgsAttributeMap(), QgsExpressionContext* context = nullptr)

Creates a new feature ready for insertion into a layer.

Default values and constraints (e.g., unique constraints) will automatically be handled. An optional attribute map can be passed for the new feature to copy as many attribute values as possible from the map, assuming that they respect the layer's constraints. Note that the created feature is not automatically inserted into the layer.

static QVariant QgsVectorLayerUtils::createUniqueValue(const QgsVectorLayer* layer, int fieldIndex, const QVariant& seed = QVariant())

Returns a new attribute value for the specified field index which is guaranteed to be unique.

The optional seed value can be used as a basis for generated values.

static QgsFeature QgsVectorLayerUtils::duplicateFeature(QgsVectorLayer* layer, const QgsFeature& feature, QgsProject* project, int depth, QgsDuplicateFeatureContext& duplicateFeatureContext)

Duplicates a feature and it's children (one level deep).

It calls CreateFeature, so default values and constraints (e.g., unique constraints) will automatically be handled. The duplicated feature will be automatically inserted into the layer. depth the higher this number the deeper the level - With depth > 0 the children of the feature are not duplicated duplicateFeatureContext stores all the layers and the featureids of the duplicated features (incl. children)

static QList<double> QgsVectorLayerUtils::getDoubleValues(const QgsVectorLayer* layer, const QString& fieldOrExpression, bool& ok, bool selectedOnly = false, int* nullCount = nullptr, QgsFeedback* feedback = nullptr)

Fetches all double values from a specified field name or expression.

Parameters
layer vector layer to retrieve values from
fieldOrExpression field name or an expression string evaluating to a double value
ok will be set to false if field or expression is invalid, otherwise true
selectedOnly set to true to get values from selected features only
nullCount optional pointer to integer to store number of null values encountered in
feedback optional feedback object to allow cancelation
Returns list of fetched values

Null values or invalid expression results are skipped.

static std::unique_ptr<QgsVectorLayerFeatureSource> QgsVectorLayerUtils::getFeatureSource(QPointer<QgsVectorLayer> layer, QgsFeedback* feedback = nullptr)

Gets the feature source from a QgsVectorLayer pointer.

This method is thread-safe but will block the main thread for execution. Executing it from the main thread is safe too. This should be used in scenarios, where a QWeakPointer<QgsVectorLayer> is kept in a thread and features should be fetched from this layer. Using the layer directly is not safe to do. The result will be nullptr if the layer has been deleted. If feedback is specified, the call will return if the feedback is canceled. Returns a new feature source for the layer. The source may be a nullptr if the layer no longer exists or if the feedback is canceled.

static QList<QVariant> QgsVectorLayerUtils::getValues(const QgsVectorLayer* layer, const QString& fieldOrExpression, bool& ok, bool selectedOnly = false, QgsFeedback* feedback = nullptr)

Fetches all values from a specified field name or expression.

Parameters
layer vector layer to retrieve values from
fieldOrExpression field name or an expression string
ok will be set to false if field or expression is invalid, otherwise true
selectedOnly set to true to get values from selected features only
feedback optional feedback object to allow cancelation
Returns list of fetched values

static QgsFeatureIterator QgsVectorLayerUtils::getValuesIterator(const QgsVectorLayer* layer, const QString& fieldOrExpression, bool& ok, bool selectedOnly)

Create a feature iterator for a specified field name or expression.

Parameters
layer vector layer to retrieve values from
fieldOrExpression field name or an expression string
ok will be set to false if field or expression is invalid, otherwise true
selectedOnly set to true to get values from selected features only
Returns feature iterator

static QgsFeatureList QgsVectorLayerUtils::makeFeatureCompatible(const QgsFeature& feature, const QgsVectorLayer* layer)

Converts input feature to be compatible with the given layer.

This function returns a new list of transformed features compatible with the input layer, note that the number of features returned might be greater than one when converting a multi part geometry to single part

The following operations will be performed to convert the input features:

  • convert single geometries to multi part
  • drop additional attributes
  • drop geometry if layer is geometry-less
  • add missing attribute fields
  • add back M/Z values (initialized to 0)
  • drop Z/M
  • convert multi part geometries to single part

static QgsFeatureList QgsVectorLayerUtils::makeFeaturesCompatible(const QgsFeatureList& features, const QgsVectorLayer* layer)

Converts input features to be compatible with the given layer.

This function returns a new list of transformed features compatible with the input layer, note that the number of features returned might be greater than the number of input features.

The following operations will be performed to convert the input features:

  • convert single geometries to multi part
  • drop additional attributes
  • drop geometry if layer is geometry-less
  • add missing attribute fields
  • add back M/Z values (initialized to 0)
  • drop Z/M
  • convert multi part geometries to single part

static void QgsVectorLayerUtils::matchAttributesToFields(QgsFeature& feature, const QgsFields& fields)

Matches the attributes in feature to the specified fields.

This causes the attributes contained within the given feature to be rearranged (or in some cases dropped) in order to match the fields and order indicated by fields.

The exact behavior depends on whether or not feature has a valid fields container set (see QgsFeature::fields()). If a fields container is set, then the names of the feature's fields are matched to fields. In this case attributes from feature will be rearranged or dropped in order to match the field names from fields.

If the feature does not have a valid fields container set, then the feature's attributes are simply truncated to match the number of fields present in fields (or if less attributes are present in feature than in fields, the feature's attributes are padded with NULL values to match the required length).

static bool QgsVectorLayerUtils::validateAttribute(const QgsVectorLayer* layer, const QgsFeature& feature, int attributeIndex, QStringList& errors, QgsFieldConstraints::ConstraintStrength strength = QgsFieldConstraints::ConstraintStrengthNotSet, QgsFieldConstraints::ConstraintOrigin origin = QgsFieldConstraints::ConstraintOriginNotSet)

Tests an attribute value to check whether it passes all constraints which are present on the corresponding field.

Returns true if the attribute value is valid for the field. Any constraint failures will be reported in the errors argument. If the strength or origin parameter is set then only constraints with a matching strength/origin will be checked.

static bool QgsVectorLayerUtils::valueExists(const QgsVectorLayer* layer, int fieldIndex, const QVariant& value, const QgsFeatureIds& ignoreIds = QgsFeatureIds())

Returns true if the specified value already exists within a field.

This method can be used to test for uniqueness of values inside a layer's attributes. An optional list of ignored feature IDs can be provided, if so, any features with IDs within this list are ignored when testing for existence of the value.