Subgroup: Vector

Class: QgsVectorLayerUtils

class qgis.core.QgsVectorLayerUtils

Bases: sip.wrapper

Contains utility methods for working with QgsVectorLayers.

New in version 3.0: Methods

createFeature Creates a new feature ready for insertion into a layer.
createUniqueValue Returns a new attribute value for the specified field index which is guaranteed to be unique.
duplicateFeature Duplicates a feature and it’s children (one level deep).
getDoubleValues Fetches all double values from a specified field name or expression.
getValues Fetches all values from a specified field name or expression.
getValuesIterator Create a feature iterator for a specified field name or expression.
validateAttribute Tests an attribute value to check whether it passes all constraints which are present on the corresponding field.
valueExists Returns true if the specified value already exists within a field.

Signals

Attributes

class QgsDuplicateFeatureContext

Bases: sip.wrapper

Constructor for QgsDuplicateFeatureContext

QgsVectorLayerUtils.QgsDuplicateFeatureContext(QgsVectorLayerUtils.QgsDuplicateFeatureContext)

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

New in version 3.0.

duplicatedFeatures(self, layer: QgsVectorLayer) → object

Returns the duplicated features in the given layer

New in version 3.0.

layers(self) → List[QgsVectorLayer]

Returns all the layers on which features have been duplicated

New in version 3.0.

createFeature(layer: QgsVectorLayer, geometry: QgsGeometry = QgsGeometry(), attributes: Dict[int, Any] = {}, context: QgsExpressionContext = None) → QgsFeature

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.

createUniqueValue(layer: QgsVectorLayer, fieldIndex: int, seed: Any = None) → Any

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.

See also

valueExists()

duplicateFeature(layer: QgsVectorLayer, feature: QgsFeature, project: QgsProject, depth: int) → Tuple[QgsFeature, QgsVectorLayerUtils.QgsDuplicateFeatureContext]

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)

New in version 3.0.

getDoubleValues(layer: QgsVectorLayer, fieldOrExpression: str, selectedOnly: bool = False, feedback: QgsFeedback = None) → Tuple[List[float], bool, int]

Fetches all double values from a specified field name or expression. Null values or invalid expression results are skipped.

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

See also

getValues()

New in version 3.0.

getValues(layer: QgsVectorLayer, fieldOrExpression: str, selectedOnly: bool = False, feedback: QgsFeedback = None) → Tuple[List[Any], bool]

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

New in version 3.0.

getValuesIterator(layer: QgsVectorLayer, fieldOrExpression: str, selectedOnly: bool) → Tuple[QgsFeatureIterator, bool]

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

New in version 3.0.

validateAttribute(layer: QgsVectorLayer, feature: QgsFeature, attributeIndex: int, strength: QgsFieldConstraints.ConstraintStrength = QgsFieldConstraints.ConstraintStrengthNotSet, origin: QgsFieldConstraints.ConstraintOrigin = QgsFieldConstraints.ConstraintOriginNotSet) → Tuple[bool, List[str]]

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.

valueExists(layer: QgsVectorLayer, fieldIndex: int, value: Any, ignoreIds: object = QgsFeatureIds()) → bool

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.