QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
qgsvectorlayerutils.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgsvectorlayerutils.h
3  ---------------------
4  Date : October 2016
5  Copyright : (C) 2016 by Nyall Dawson
6  Email : nyall dot dawson at gmail dot com
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 
16 #ifndef QGSVECTORLAYERUTILS_H
17 #define QGSVECTORLAYERUTILS_H
18 
19 #include "qgis_core.h"
20 #include "qgsgeometry.h"
22 #include "qgsfeaturesink.h"
23 
24 class QgsFeatureRenderer;
25 class QgsSymbolLayer;
26 class QgsSymbolLayerId;
27 
28 
29 #ifndef SIP_RUN
30 
32 {
33  bool hasEffects = false;
34 
35  // masked symbol layers
36  QSet<QgsSymbolLayerId> symbolLayerIds;
37 };
38 
39 typedef QHash<QString, QgsMaskedLayer> QgsMaskedLayers;
40 
41 #endif
42 
51 class CORE_EXPORT QgsVectorLayerUtils
52 {
53  public:
54 
62  class CORE_EXPORT QgsDuplicateFeatureContext
63  {
64  public:
65 
67  QgsDuplicateFeatureContext() = default;
68 
73  QList<QgsVectorLayer *> layers() const;
74 
79  QgsFeatureIds duplicatedFeatures( QgsVectorLayer *layer ) const;
80 
81 
82  private:
83  QMap<QgsVectorLayer *, QgsFeatureIds> mDuplicatedFeatures;
84  friend class QgsVectorLayerUtils;
85 
90  void setDuplicatedFeatures( QgsVectorLayer *layer, const QgsFeatureIds &ids );
91  };
92 
100  class CORE_EXPORT QgsFeatureData
101  {
102  public:
103 
107  QgsFeatureData( const QgsGeometry &geometry = QgsGeometry(), const QgsAttributeMap &attributes = QgsAttributeMap() );
108 
110  QgsGeometry geometry() const;
111 
113  QgsAttributeMap attributes() const;
114 
115  private:
116  QgsGeometry mGeometry;
117  QgsAttributeMap mAttributes;
118  };
119 
120  // SIP does not like "using", use legacy typedef
122  typedef QList<QgsVectorLayerUtils::QgsFeatureData> QgsFeaturesDataList;
123 
133  static QgsFeatureIterator getValuesIterator( const QgsVectorLayer *layer, const QString &fieldOrExpression, bool &ok, bool selectedOnly );
134 
146  static QList< QVariant > getValues( const QgsVectorLayer *layer, const QString &fieldOrExpression, bool &ok, bool selectedOnly = false, QgsFeedback *feedback = nullptr );
147 
161  static QList< double > getDoubleValues( const QgsVectorLayer *layer, const QString &fieldOrExpression, bool &ok, bool selectedOnly = false, int *nullCount = nullptr, QgsFeedback *feedback = nullptr );
162 
169  static bool valueExists( const QgsVectorLayer *layer, int fieldIndex, const QVariant &value, const QgsFeatureIds &ignoreIds = QgsFeatureIds() );
170 
176  static QVariant createUniqueValue( const QgsVectorLayer *layer, int fieldIndex, const QVariant &seed = QVariant() );
177 
184  static QVariant createUniqueValueFromCache( const QgsVectorLayer *layer, int fieldIndex, const QSet<QVariant> &existingValues, const QVariant &seed = QVariant() );
185 
191  static bool validateAttribute( const QgsVectorLayer *layer, const QgsFeature &feature, int attributeIndex, QStringList &errors SIP_OUT,
194 
203  static QgsFeature createFeature( const QgsVectorLayer *layer,
204  const QgsGeometry &geometry = QgsGeometry(),
205  const QgsAttributeMap &attributes = QgsAttributeMap(),
206  QgsExpressionContext *context = nullptr );
207 
215  static QgsFeatureList createFeatures( const QgsVectorLayer *layer,
216  const QgsFeaturesDataList &featuresData,
217  QgsExpressionContext *context = nullptr );
218 
229  static QgsFeature duplicateFeature( QgsVectorLayer *layer, const QgsFeature &feature, QgsProject *project, QgsDuplicateFeatureContext &duplicateFeatureContext SIP_OUT, const int maxDepth = 0, int depth SIP_PYARGREMOVE = 0, QList<QgsVectorLayer *> referencedLayersBranch SIP_PYARGREMOVE = QList<QgsVectorLayer *>() );
230 
231 
246  static std::unique_ptr<QgsVectorLayerFeatureSource> getFeatureSource( QPointer<QgsVectorLayer> layer, QgsFeedback *feedback = nullptr ) SIP_SKIP;
247 
267  static void matchAttributesToFields( QgsFeature &feature, const QgsFields &fields );
268 
290  static QgsFeatureList makeFeatureCompatible( const QgsFeature &feature, const QgsVectorLayer *layer, QgsFeatureSink::SinkFlags sinkFlags = QgsFeatureSink::SinkFlags() );
291 
313  static QgsFeatureList makeFeaturesCompatible( const QgsFeatureList &features, const QgsVectorLayer *layer, QgsFeatureSink::SinkFlags sinkFlags = QgsFeatureSink::SinkFlags() );
314 
323  static bool fieldIsEditable( const QgsVectorLayer *layer, int fieldIndex, const QgsFeature &feature );
324 
334  static bool fieldIsReadOnly( const QgsVectorLayer *layer, int fieldIndex );
335 
345  static bool fieldEditabilityDependsOnFeature( const QgsVectorLayer *layer, int fieldIndex );
346 
354  static QHash<QString, QgsMaskedLayers> labelMasks( const QgsVectorLayer * ) SIP_SKIP;
355 
363  static QgsMaskedLayers symbolLayerMasks( const QgsVectorLayer * ) SIP_SKIP;
364 
370  static QString getFeatureDisplayString( const QgsVectorLayer *layer, const QgsFeature &feature );
371 
378  {
379  IgnoreAuxiliaryLayers = 1 << 1,
380  };
381  Q_DECLARE_FLAGS( CascadedFeatureFlags, CascadedFeatureFlag )
382 
383 
389  static bool impactsCascadeFeatures( const QgsVectorLayer *layer, const QgsFeatureIds &fids, const QgsProject *project, QgsDuplicateFeatureContext &context SIP_OUT, QgsVectorLayerUtils::CascadedFeatureFlags flags = QgsVectorLayerUtils::CascadedFeatureFlags() );
390 
391 #ifndef SIP_RUN
392 
413 #else
414 
432 #endif
433  static QString guessFriendlyIdentifierField( const QgsFields &fields, bool *foundFriendly SIP_OUT = nullptr ) SIP_PYNAME( guessFriendlyIdentifierFieldV2 );
434 
435 #ifdef SIP_RUN
436 
453  static QString guessFriendlyIdentifierField( const QgsFields &fields );
454 #endif
455 
456 };
457 
458 
459 #endif // QGSVECTORLAYERUTILS_H
QgsExpressionContext
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Definition: qgsexpressioncontext.h:406
SIP_PYNAME
#define SIP_PYNAME(name)
Definition: qgis_sip.h:81
QgsMaskedLayer::symbolLayerIds
QSet< QgsSymbolLayerId > symbolLayerIds
Definition: qgsvectorlayerutils.h:36
SIP_OUT
#define SIP_OUT
Definition: qgis_sip.h:58
QgsFields
Container of fields for a vector layer.
Definition: qgsfields.h:44
qgsfeaturesink.h
QgsMaskedLayer
Definition: qgsvectorlayerutils.h:31
SIP_PYARGREMOVE
#define SIP_PYARGREMOVE
Definition: qgis_sip.h:151
QgsVectorLayerUtils::QgsFeatureData
Encapsulate geometry and attributes for new features, to be passed to createFeatures.
Definition: qgsvectorlayerutils.h:100
QgsProject
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
Definition: qgsproject.h:103
QgsVectorLayerUtils::CascadedFeatureFlag
CascadedFeatureFlag
Flags that can be used when determining cascaded features.
Definition: qgsvectorlayerutils.h:377
QgsSymbolLayer
Definition: qgssymbollayer.h:54
SIP_SKIP
#define SIP_SKIP
Definition: qgis_sip.h:126
QgsVectorLayerUtils::QgsFeaturesDataList
QList< QgsVectorLayerUtils::QgsFeatureData > QgsFeaturesDataList
Alias for list of QgsFeatureData.
Definition: qgsvectorlayerutils.h:122
QgsFieldConstraints::ConstraintOrigin
ConstraintOrigin
Origin of constraints.
Definition: qgsfieldconstraints.h:54
QgsFeedback
Base class for feedback objects to be used for cancellation of something running in a worker thread.
Definition: qgsfeedback.h:44
QgsVectorLayerUtils::QgsDuplicateFeatureContext
Contains mainly the QMap with QgsVectorLayer and QgsFeatureIds do list all the duplicated features.
Definition: qgsvectorlayerutils.h:62
QgsFeatureList
QList< QgsFeature > QgsFeatureList
Definition: qgsfeature.h:882
QgsAttributeMap
QMap< int, QVariant > QgsAttributeMap
Definition: qgsattributes.h:38
QgsFieldConstraints::ConstraintStrengthNotSet
@ ConstraintStrengthNotSet
Constraint is not set.
Definition: qgsfieldconstraints.h:66
qgsvectorlayerfeatureiterator.h
QgsFeatureIds
QSet< QgsFeatureId > QgsFeatureIds
Definition: qgsfeatureid.h:37
QgsFieldConstraints::ConstraintOriginNotSet
@ ConstraintOriginNotSet
Constraint is not set.
Definition: qgsfieldconstraints.h:56
QgsMaskedLayer::hasEffects
bool hasEffects
Definition: qgsvectorlayerutils.h:33
qgsgeometry.h
QgsFeatureRenderer
Definition: qgsrenderer.h:101
QgsGeometry
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:124
QgsVectorLayer
Represents a vector layer which manages a vector based data sets.
Definition: qgsvectorlayer.h:391
QgsMaskedLayers
QHash< QString, QgsMaskedLayer > QgsMaskedLayers
Definition: qgsvectorlayerutils.h:39
QgsSymbolLayerId
We may need stable references to symbol layers, when pointers to symbol layers is not usable (when a ...
Definition: qgssymbollayerreference.h:53
QgsFeature
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition: qgsfeature.h:55
QgsVectorLayerUtils
Contains utility methods for working with QgsVectorLayers.
Definition: qgsvectorlayerutils.h:51
QgsFieldConstraints::ConstraintStrength
ConstraintStrength
Strength of constraints.
Definition: qgsfieldconstraints.h:64
QgsFeatureIterator
Wrapper for iterator of features from vector data provider or vector layer.
Definition: qgsfeatureiterator.h:289
QgsFeatureSink
An interface for objects which accept features via addFeature(s) methods.
Definition: qgsfeaturesink.h:33