Class: QgsVectorLayerUtils¶
-
class
qgis.core.
QgsVectorLayerUtils
¶ Bases:
sip.wrapper
Contains utility methods for working with QgsVectorLayers.
Methods
Creates a new feature ready for insertion into a layer.
Creates a set of new features ready for insertion into a layer.
Returns a new attribute value for the specified field index which is guaranteed to be unique.
Returns a new attribute value for the specified field index which is guaranteed to be unique within regard to
existingValues
.Duplicates a feature and it’s children (one level deep).
- rtype
bool
Fetches all double values from a specified field name or expression.
- rtype
str
Fetches all values from a specified field name or expression.
Create a feature iterator for a specified field name or expression.
- rtype
Tuple[bool, QgsVectorLayerUtils.QgsDuplicateFeatureContext]
Converts input
feature
to be compatible with the givenlayer
.Converts input
features
to be compatible with the givenlayer
.Matches the attributes in
feature
to the specifiedfields
.Tests an attribute value to check whether it passes all constraints which are present on the corresponding field.
Returns
True
if the specified value already exists within a field.-
class
QgsDuplicateFeatureContext
¶ Bases:
sip.wrapper
Constructor for QgsDuplicateFeatureContext
QgsVectorLayerUtils.QgsDuplicateFeatureContext(QgsVectorLayerUtils.:py:class:.QgsDuplicateFeatureContext)
Contains mainly the QMap with
QgsVectorLayer
andQgsFeatureIds
do list all the duplicated featuresNew in version 3.0.
-
duplicatedFeatures
(self, layer: QgsVectorLayer) → object¶ Returns the duplicated features in the given layer
New in version 3.0.
- Parameters
layer (QgsVectorLayer) –
- Return type
object
-
layers
(self) → List[QgsVectorLayer]¶ Returns all the layers on which features have been duplicated
New in version 3.0.
- Return type
List[QgsVectorLayer]
-
-
class
QgsFeatureData
(geometry: QgsGeometry = QgsGeometry(), attributes: Dict[int, Any] = {})¶ Bases:
sip.wrapper
Constructs a new
QgsFeatureData
with givengeometry
andattributes
QgsVectorLayerUtils.QgsFeatureData(QgsVectorLayerUtils.:py:class:.QgsFeatureData)
Encapsulate geometry and attributes for new features, to be passed to createFeatures
See also
createFeatures()
New in version 3.6.
- Parameters
geometry (
QgsGeometry
= QgsGeometry()) –attributes (Dict[int) –
-
attributes
(self) → Dict[int, Any]¶ Returns attributes
- Return type
Dict[int, Any]
-
geometry
(self) → QgsGeometry¶ Returns geometry
- Return type
-
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.
See also
- Parameters
layer (QgsVectorLayer) –
geometry (
QgsGeometry
= QgsGeometry()) –attributes (Dict[int) –
context (
QgsExpressionContext
= None) –
- Return type
-
createFeatures
(layer: QgsVectorLayer, featuresData: Iterable[QgsVectorLayerUtils.QgsFeatureData], context: QgsExpressionContext = None) → List[QgsFeature]¶ Creates a set of new features ready for insertion into a layer. Default values and constraints (e.g., unique constraints) will automatically be handled. Note that the created features are not automatically inserted into the layer.
See also
New in version 3.6.
- Parameters
layer (QgsVectorLayer) –
featuresData (Iterable[QgsVectorLayerUtils.QgsFeatureData]) –
context (
QgsExpressionContext
= None) –
- Return type
List[QgsFeature]
-
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
- Parameters
layer (QgsVectorLayer) –
fieldIndex (int) –
seed (Any = None) –
- Return type
Any
-
createUniqueValueFromCache
(layer: QgsVectorLayer, fieldIndex: int, existingValues: Iterable[Any], seed: Any = None) → Any¶ Returns a new attribute value for the specified field index which is guaranteed to be unique within regard to
existingValues
. The optional seed value can be used as a basis for generated values.New in version 3.6.
- Parameters
layer (QgsVectorLayer) –
fieldIndex (int) –
existingValues (Iterable[Any]) –
seed (Any = None) –
- Return type
Any
-
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 duplicatedduplicateFeatureContext
stores all the layers and the featureids of the duplicated features (incl. children)New in version 3.0.
- Parameters
layer (QgsVectorLayer) –
feature (QgsFeature) –
project (QgsProject) –
depth (int) –
- Return type
Tuple[
QgsFeature
, QgsVectorLayerUtils.QgsDuplicateFeatureContext]
-
fieldIsEditable
(layer: QgsVectorLayer, fieldIndex: int, feature: QgsFeature) → bool¶ - Return type
bool
- Returns
true if the:param feature: field at index:param fieldIndex: from:param layer: is editable, false if the field is readonly
New in version 3.10.
- Parameters
layer (QgsVectorLayer) –
fieldIndex (int) –
feature (QgsFeature) –
-
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 (QgsVectorLayer) – vector layer to retrieve values from
fieldOrExpression (str) – field name or an expression string evaluating to a double value
ok – will be set to
False
if field or expression is invalid, otherwiseTrue
selectedOnly (bool = False) – set to
True
to get values from selected features onlynullCount – optional pointer to integer to store number of null values encountered in
feedback (
QgsFeedback
= None) – optional feedback object to allow cancellation
- Return type
Tuple[List[float], bool, int]
- Returns
list of fetched values
See also
New in version 3.0.
-
getFeatureDisplayString
(layer: QgsVectorLayer, feature: QgsFeature) → str¶ - Return type
str
- Returns
a descriptive string for a
feature
, suitable for displaying to the user. The definition is taken from thedisplayExpression
property oflayer
.
New in version 3.12.
- Parameters
layer (QgsVectorLayer) –
feature (QgsFeature) –
-
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 (QgsVectorLayer) – vector layer to retrieve values from
fieldOrExpression (str) – field name or an expression string
ok – will be set to
False
if field or expression is invalid, otherwiseTrue
selectedOnly (bool = False) – set to
True
to get values from selected features onlyfeedback (
QgsFeedback
= None) – optional feedback object to allow cancellation
- Return type
Tuple[List[Any], bool]
- Returns
list of fetched values
See also
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 (QgsVectorLayer) – vector layer to retrieve values from
fieldOrExpression (str) – field name or an expression string
ok – will be set to
False
if field or expression is invalid, otherwiseTrue
selectedOnly (bool) – set to
True
to get values from selected features only
- Return type
Tuple[
QgsFeatureIterator
, bool]- Returns
feature iterator
New in version 3.0.
-
impactsCascadeFeatures
(layer: QgsVectorLayer, fids: object, project: QgsProject) → Tuple[bool, QgsVectorLayerUtils.QgsDuplicateFeatureContext]¶ - Return type
Tuple[bool, QgsVectorLayerUtils.QgsDuplicateFeatureContext]
- Returns
True
if at least one feature of thefids
onlayer
is connected as parent in at least one composition relation of theproject
or contains joins, where cascade delete is set. Details about cascading effects will be written tocontext
.
New in version 3.14.
- Parameters
layer (QgsVectorLayer) –
fids (object) –
project (QgsProject) –
-
makeFeatureCompatible
(feature: QgsFeature, layer: QgsVectorLayer) → List[QgsFeature]¶ Converts input
feature
to be compatible with the givenlayer
.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
New in version 3.4.
- Parameters
feature (QgsFeature) –
layer (QgsVectorLayer) –
- Return type
List[QgsFeature]
-
makeFeaturesCompatible
(features: Iterable[QgsFeature], layer: QgsVectorLayer) → List[QgsFeature]¶ Converts input
features
to be compatible with the givenlayer
.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
New in version 3.4.
- Parameters
features (Iterable[QgsFeature]) –
layer (QgsVectorLayer) –
- Return type
List[QgsFeature]
-
matchAttributesToFields
(feature: QgsFeature, fields: QgsFields)¶ Matches the attributes in
feature
to the specifiedfields
.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 byfields
.The exact behavior depends on whether or not
feature
has a valid fields container set (seeQgsFeature.fields()
). If a fields container is set, then the names of the feature’s fields are matched tofields
. In this case attributes fromfeature
will be rearranged or dropped in order to match the field names fromfields
.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 infields
(or if less attributes are present infeature
than infields
, the feature’s attributes are padded with NULL values to match the required length). Finally, the feature’s fields are set tofields
.New in version 3.4.
- Parameters
feature (QgsFeature) –
fields (QgsFields) –
-
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.- Parameters
layer (QgsVectorLayer) –
feature (QgsFeature) –
attributeIndex (int) –
strength (QgsFieldConstraints.ConstraintStrength = QgsFieldConstraints.ConstraintStrengthNotSet) –
origin (QgsFieldConstraints.ConstraintOrigin = QgsFieldConstraints.ConstraintOriginNotSet) –
- Return type
Tuple[bool, List[str]]
-
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.See also
- Parameters
layer (QgsVectorLayer) –
fieldIndex (int) –
value (Any) –
ignoreIds (object = QgsFeatureIds()) –
- Return type
bool