QGIS API Documentation 3.38.0-Grenoble (exported)
Loading...
Searching...
No Matches
Classes | Public Types | 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...
 
class  QgsFeatureData
 Encapsulate geometry and attributes for new features, to be passed to createFeatures. More...
 

Public Types

enum  CascadedFeatureFlag { IgnoreAuxiliaryLayers = 1 << 1 }
 Flags that can be used when determining cascaded features. More...
 
typedef QFlags< CascadedFeatureFlagCascadedFeatureFlags
 
typedef QList< QgsVectorLayerUtils::QgsFeatureDataQgsFeaturesDataList
 Alias for list of QgsFeatureData.
 

Static Public Member Functions

static bool attributeHasConstraints (const QgsVectorLayer *layer, int attributeIndex)
 Returns true if a feature attribute has active constraints.
 
static QgsFeature 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.
 
static QgsFeatureList createFeatures (const QgsVectorLayer *layer, const QgsFeaturesDataList &featuresData, QgsExpressionContext *context=nullptr)
 Creates a set of new features ready for insertion into a layer.
 
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.
 
static QVariant createUniqueValueFromCache (const QgsVectorLayer *layer, int fieldIndex, const QSet< QVariant > &existingValues, const QVariant &seed=QVariant())
 Returns a new attribute value for the specified field index which is guaranteed to be unique within regard to existingValues.
 
static QgsFeature duplicateFeature (QgsVectorLayer *layer, const QgsFeature &feature, QgsProject *project, QgsDuplicateFeatureContext &duplicateFeatureContext, const int maxDepth=0, int depth=0, QList< QgsVectorLayer * > referencedLayersBranch=QList< QgsVectorLayer * >())
 Duplicates a feature and it's children (one level deep).
 
static bool fieldEditabilityDependsOnFeature (const QgsVectorLayer *layer, int fieldIndex)
 Returns true if the editability of the field at index fieldIndex from layer may vary feature by feature.
 
static bool fieldIsEditable (const QgsVectorLayer *layer, int fieldIndex, const QgsFeature &feature)
 Tests whether a field is editable for a particular feature.
 
static bool fieldIsReadOnly (const QgsVectorLayer *layer, int fieldIndex)
 
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.
 
static QString getFeatureDisplayString (const QgsVectorLayer *layer, const QgsFeature &feature)
 
static std::unique_ptr< QgsVectorLayerFeatureSourcegetFeatureSource (QPointer< QgsVectorLayer > layer, QgsFeedback *feedback=nullptr)
 Gets the feature source from a QgsVectorLayer pointer.
 
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.
 
static QgsFeatureIterator getValuesIterator (const QgsVectorLayer *layer, const QString &fieldOrExpression, bool &ok, bool selectedOnly)
 Create a feature iterator for a specified field name or expression.
 
static QString guessFriendlyIdentifierField (const QgsFields &fields, bool *foundFriendly=nullptr)
 Given a set of fields, attempts to pick the "most useful" field for user-friendly identification of features.
 
static bool impactsCascadeFeatures (const QgsVectorLayer *layer, const QgsFeatureIds &fids, const QgsProject *project, QgsDuplicateFeatureContext &context, QgsVectorLayerUtils::CascadedFeatureFlags flags=QgsVectorLayerUtils::CascadedFeatureFlags())
 
static QHash< QString, QgsMaskedLayerslabelMasks (const QgsVectorLayer *)
 Returns masks defined in labeling options of a layer.
 
static QgsFeatureList makeFeatureCompatible (const QgsFeature &feature, const QgsVectorLayer *layer, QgsFeatureSink::SinkFlags sinkFlags=QgsFeatureSink::SinkFlags())
 Converts input feature to be compatible with the given layer.
 
static QgsFeatureList makeFeaturesCompatible (const QgsFeatureList &features, const QgsVectorLayer *layer, QgsFeatureSink::SinkFlags sinkFlags=QgsFeatureSink::SinkFlags())
 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 QgsMaskedLayers symbolLayerMasks (const QgsVectorLayer *)
 Returns all masks that may be defined on symbol layers for a given vector layer.
 
static bool validateAttribute (const QgsVectorLayer *layer, const QgsFeature &feature, int attributeIndex, QStringList &errors, QgsFieldConstraints::ConstraintStrength strength=QgsFieldConstraints::ConstraintStrengthNotSet, QgsFieldConstraints::ConstraintOrigin origin=QgsFieldConstraints::ConstraintOriginNotSet)
 Tests a feature attribute value to check whether it passes all constraints which are present on the corresponding field.
 
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.
 

Detailed Description

Contains utility methods for working with QgsVectorLayers.

Definition at line 51 of file qgsvectorlayerutils.h.

Member Typedef Documentation

◆ CascadedFeatureFlags

Definition at line 381 of file qgsvectorlayerutils.h.

◆ QgsFeaturesDataList

Alias for list of QgsFeatureData.

Definition at line 118 of file qgsvectorlayerutils.h.

Member Enumeration Documentation

◆ CascadedFeatureFlag

Flags that can be used when determining cascaded features.

Since
QGIS 3.4
Enumerator
IgnoreAuxiliaryLayers 

Ignore auxiliary layers.

Definition at line 377 of file qgsvectorlayerutils.h.

Member Function Documentation

◆ attributeHasConstraints()

bool QgsVectorLayerUtils::attributeHasConstraints ( const QgsVectorLayer layer,
int  attributeIndex 
)
static

Returns true if a feature attribute has active constraints.

Parameters
layerthe vector layer from which field constraints will be checked for
attributeIndexthe attribute index
Since
QGIS 3.30

Definition at line 371 of file qgsvectorlayerutils.cpp.

◆ createFeature()

QgsFeature QgsVectorLayerUtils::createFeature ( const 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.

See also
createFeatures()

Definition at line 486 of file qgsvectorlayerutils.cpp.

◆ createFeatures()

QgsFeatureList QgsVectorLayerUtils::createFeatures ( const QgsVectorLayer layer,
const QgsFeaturesDataList featuresData,
QgsExpressionContext context = nullptr 
)
static

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
createFeature()
Since
QGIS 3.6

Definition at line 493 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 210 of file qgsvectorlayerutils.cpp.

◆ createUniqueValueFromCache()

QVariant QgsVectorLayerUtils::createUniqueValueFromCache ( const QgsVectorLayer layer,
int  fieldIndex,
const QSet< QVariant > &  existingValues,
const QVariant &  seed = QVariant() 
)
static

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.

Since
QGIS 3.6

Definition at line 290 of file qgsvectorlayerutils.cpp.

◆ duplicateFeature()

QgsFeature QgsVectorLayerUtils::duplicateFeature ( QgsVectorLayer layer,
const QgsFeature feature,
QgsProject project,
QgsDuplicateFeatureContext duplicateFeatureContext,
const int  maxDepth = 0,
int  depth = 0,
QList< QgsVectorLayer * >  referencedLayersBranch = QList<QgsVectorLayer *>() 
)
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. duplicateFeatureContext stores all the layers and the featureids of the duplicated features (incl. children) maxDepth the maximum depth to duplicate children in relations, 0 is unlimited depth (in any case, limited to 100) depth the current depth, not exposed in Python referencedLayersBranch the current branch of layers across the relations, not exposed in Python, taken by copy not reference, used to avoid infinite loop

Definition at line 635 of file qgsvectorlayerutils.cpp.

◆ fieldEditabilityDependsOnFeature()

bool QgsVectorLayerUtils::fieldEditabilityDependsOnFeature ( const QgsVectorLayer layer,
int  fieldIndex 
)
static

Returns true if the editability of the field at index fieldIndex from layer may vary feature by feature.

I.e. if the field is taken from a joined layer, the value may or may not be editable for any individual feature depending on the join's "upsert on edit" capabilities.

Since
QGIS 3.18

Definition at line 927 of file qgsvectorlayerutils.cpp.

◆ fieldIsEditable()

bool QgsVectorLayerUtils::fieldIsEditable ( const QgsVectorLayer layer,
int  fieldIndex,
const QgsFeature feature 
)
static

Tests whether a field is editable for a particular feature.

Returns
true if the field at index fieldIndex from layer is editable, false if the field is read only.
Since
QGIS 3.10

Definition at line 948 of file qgsvectorlayerutils.cpp.

◆ fieldIsReadOnly()

bool QgsVectorLayerUtils::fieldIsReadOnly ( const QgsVectorLayer layer,
int  fieldIndex 
)
static
Returns
true if the field at index fieldIndex from layer is editable, false if the field is read only.

If this function returns true then the editability of the field may still vary feature by feature. See fieldIsEditable() to determine this on a feature by feature basis.

Since
QGIS 3.18

Definition at line 900 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 cancellation
Returns
list of fetched values
See also
getValues

Definition at line 123 of file qgsvectorlayerutils.cpp.

◆ getFeatureDisplayString()

QString QgsVectorLayerUtils::getFeatureDisplayString ( const QgsVectorLayer layer,
const QgsFeature feature 
)
static
Returns
a descriptive string for a feature, suitable for displaying to the user. The definition is taken from the displayExpression property of layer.
Since
QGIS 3.12

Definition at line 1082 of file qgsvectorlayerutils.cpp.

◆ getFeatureSource()

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

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 \c 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 nullptr if the layer no longer exists or if the feedback is canceled.

Note
Requires Qt >= 5.10 to make use of the thread-safe implementation
Since
QGIS 3.4

Definition at line 709 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 cancellation
Returns
list of fetched values
See also
getDoubleValues

Definition at line 83 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

Definition at line 41 of file qgsvectorlayerutils.cpp.

◆ guessFriendlyIdentifierField()

QString QgsVectorLayerUtils::guessFriendlyIdentifierField ( const QgsFields fields,
bool *  foundFriendly = nullptr 
)
static

Given a set of fields, attempts to pick the "most useful" field for user-friendly identification of features.

For instance, if a field called "name" is present, this will be returned.

Assumes that the user has organized the data with the more "interesting" field names first. As such, "name" would be selected before "oldname", "othername", etc.

If no friendly identifier is found, the function will fallback to the first available.

An optional boolean parameter can be used to determine whether the returned field name is a friendly identifier or not.

Parameters
fieldslist of fields to pick a friendly identifier from
foundFriendlyset to true if the returned field name is a friendly identifier (since QGIS 3.22)
Returns
field name
Since
QGIS 3.18

Definition at line 1184 of file qgsvectorlayerutils.cpp.

◆ impactsCascadeFeatures()

bool QgsVectorLayerUtils::impactsCascadeFeatures ( const QgsVectorLayer layer,
const QgsFeatureIds fids,
const QgsProject project,
QgsDuplicateFeatureContext context,
QgsVectorLayerUtils::CascadedFeatureFlags  flags = QgsVectorLayerUtils::CascadedFeatureFlags() 
)
static
Returns
true if at least one feature of the fids on layer is connected as parent in at least one composition relation of the project or contains joins, where cascade delete is set. Details about cascading effects will be written to context.
Since
QGIS 3.14

Definition at line 1094 of file qgsvectorlayerutils.cpp.

◆ labelMasks()

QHash< QString, QgsMaskedLayers > QgsVectorLayerUtils::labelMasks ( const QgsVectorLayer layer)
static

Returns masks defined in labeling options of a layer.

The returned type associates a labeling rule identifier to a set of layers that are masked given by their layer id, and a set of masked symbol layers if associated to each masked layers.

Note
Not available in Python bindings
Since
QGIS 3.12

Definition at line 973 of file qgsvectorlayerutils.cpp.

◆ makeFeatureCompatible()

QgsFeatureList QgsVectorLayerUtils::makeFeatureCompatible ( const QgsFeature feature,
const QgsVectorLayer layer,
QgsFeatureSink::SinkFlags  sinkFlags = QgsFeatureSink::SinkFlags() 
)
static

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

Optionally, sinkFlags can be specified to further refine the compatibility logic.

Since
QGIS 3.4

Definition at line 769 of file qgsvectorlayerutils.cpp.

◆ makeFeaturesCompatible()

QgsFeatureList QgsVectorLayerUtils::makeFeaturesCompatible ( const QgsFeatureList features,
const QgsVectorLayer layer,
QgsFeatureSink::SinkFlags  sinkFlags = QgsFeatureSink::SinkFlags() 
)
static

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

Optionally, sinkFlags can be specified to further refine the compatibility logic.

Since
QGIS 3.4

Definition at line 831 of file qgsvectorlayerutils.cpp.

◆ matchAttributesToFields()

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

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). Finally, the feature's fields are set to fields.

Since
QGIS 3.4

Definition at line 729 of file qgsvectorlayerutils.cpp.

◆ symbolLayerMasks()

QgsMaskedLayers QgsVectorLayerUtils::symbolLayerMasks ( const QgsVectorLayer layer)
static

Returns all masks that may be defined on symbol layers for a given vector layer.

The hash key is a layer id. The hash value is the set of symbol layers masked in the key's layer.

Note
Not available in Python bindings
Since
QGIS 3.12

Definition at line 1023 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 a feature 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 385 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 155 of file qgsvectorlayerutils.cpp.


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