QGIS API Documentation  3.2.0-Bonn (bc43194)
Classes | Static Public Member Functions | List of all members
QgsVectorLayerUtils Class Reference

Contains utility methods for working with QgsVectorLayers. More...

#include <qgsvectorlayerutils.h>

Classes

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

Static Public Member Functions

static QgsFeature createFeature (QgsVectorLayer *layer, const QgsGeometry &geometry=QgsGeometry(), const QgsAttributeMap &attributes=QgsAttributeMap(), QgsExpressionContext *context=nullptr)
 Creates a new feature ready for insertion into a layer. More...
 
static QVariant 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. More...
 
static QgsFeature duplicateFeature (QgsVectorLayer *layer, const QgsFeature &feature, QgsProject *project, int depth, QgsDuplicateFeatureContext &duplicateFeatureContext)
 Duplicates a feature and it's children (one level deep). More...
 
static QList< double > 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. More...
 
static QList< QVariant > 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. More...
 
static QgsFeatureIterator getValuesIterator (const QgsVectorLayer *layer, const QString &fieldOrExpression, bool &ok, bool selectedOnly)
 Create a feature iterator for a specified field name or expression. More...
 
static bool 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. More...
 
static bool valueExists (const QgsVectorLayer *layer, int fieldIndex, const QVariant &value, const QgsFeatureIds &ignoreIds=QgsFeatureIds())
 Returns true if the specified value already exists within a field. More...
 

Detailed Description

Contains utility methods for working with QgsVectorLayers.

Since
QGIS 3.0

Definition at line 31 of file qgsvectorlayerutils.h.

Member Function Documentation

◆ createFeature()

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

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.

Definition at line 349 of file qgsvectorlayerutils.cpp.

◆ createUniqueValue()

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

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()

Definition at line 177 of file qgsvectorlayerutils.cpp.

◆ duplicateFeature()

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

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)

Since
QGIS 3.0

Definition at line 443 of file qgsvectorlayerutils.cpp.

◆ getDoubleValues()

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

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

Null values or invalid expression results are skipped.

Parameters
layervector layer to retrieve values from
fieldOrExpressionfield name or an expression string evaluating to a double value
okwill be set to false if field or expression is invalid, otherwise true
selectedOnlyset to true to get values from selected features only
nullCountoptional pointer to integer to store number of null values encountered in
feedbackoptional feedback object to allow cancelation
Returns
list of fetched values
See also
getValues
Since
QGIS 3.0

Definition at line 109 of file qgsvectorlayerutils.cpp.

◆ getValues()

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

Fetches all values from a specified field name or expression.

Parameters
layervector layer to retrieve values from
fieldOrExpressionfield name or an expression string
okwill be set to false if field or expression is invalid, otherwise true
selectedOnlyset to true to get values from selected features only
feedbackoptional feedback object to allow cancelation
Returns
list of fetched values
See also
getDoubleValues
Since
QGIS 3.0

Definition at line 69 of file qgsvectorlayerutils.cpp.

◆ getValuesIterator()

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

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

Parameters
layervector layer to retrieve values from
fieldOrExpressionfield name or an expression string
okwill be set to false if field or expression is invalid, otherwise true
selectedOnlyset to true to get values from selected features only
Returns
feature iterator
Since
QGIS 3.0

Definition at line 27 of file qgsvectorlayerutils.cpp.

◆ validateAttribute()

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

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.

Definition at line 257 of file qgsvectorlayerutils.cpp.

◆ valueExists()

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

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
createUniqueValue()

Definition at line 140 of file qgsvectorlayerutils.cpp.


The documentation for this class was generated from the following files: