QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
qgsvectordataprovider.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgsvectordataprovider.h - DataProvider Interface for vector layers
3  --------------------------------------
4  Date : 23-Sep-2004
5  Copyright : (C) 2004 by Marco Hugentobler
6  email : [email protected]
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 #ifndef QGSVECTORDATAPROVIDER_H
16 #define QGSVECTORDATAPROVIDER_H
17 
18 class QTextCodec;
19 
20 #include "qgis_core.h"
21 #include <QList>
22 #include <QSet>
23 #include <QMap>
24 #include <QHash>
25 
26 //QGIS Includes
27 #include "qgis_sip.h"
28 #include "qgsdataprovider.h"
29 #include "qgsfeature.h"
30 #include "qgsaggregatecalculator.h"
31 #include "qgsmaplayerdependency.h"
32 #include "qgsrelation.h"
33 #include "qgsfeaturesink.h"
34 #include "qgsfeaturesource.h"
35 #include "qgsfeaturerequest.h"
37 
38 typedef QList<int> QgsAttributeList SIP_SKIP;
39 typedef QSet<int> QgsAttributeIds SIP_SKIP;
40 typedef QHash<int, QString> QgsAttrPalIndexNameHash;
41 
42 class QgsFeatureIterator;
43 class QgsTransaction;
44 class QgsFeedback;
45 class QgsFeatureRenderer;
47 
48 
58 class CORE_EXPORT QgsVectorDataProvider : public QgsDataProvider, public QgsFeatureSink, public QgsFeatureSource
59 {
60  Q_OBJECT
61 
62  friend class QgsTransaction;
64 
65  public:
66 
67  // If you add to this, please also add to capabilitiesString()
68 
73  {
74  NoCapabilities = 0,
75  AddFeatures = 1,
76  DeleteFeatures = 1 << 1,
77  ChangeAttributeValues = 1 << 2,
78  AddAttributes = 1 << 3,
79  DeleteAttributes = 1 << 4,
80  CreateSpatialIndex = 1 << 6,
81  SelectAtId = 1 << 7,
82  ChangeGeometries = 1 << 8,
83  SelectEncoding = 1 << 13,
84  CreateAttributeIndex = 1 << 12,
85  SimplifyGeometries = 1 << 14,
86  SimplifyGeometriesWithTopologicalValidation = 1 << 15,
87  TransactionSupport = 1 << 16,
88  CircularGeometries = 1 << 17,
89  ChangeFeatures = 1 << 18,
90  RenameAttributes = 1 << 19,
91  FastTruncate = 1 << 20,
92  ReadLayerMetadata = 1 << 21,
93  WriteLayerMetadata = 1 << 22,
94  CancelSupport = 1 << 23,
95  CreateRenderer = 1 << 24,
96  CreateLabeling = 1 << 25,
97  };
98 
99  Q_DECLARE_FLAGS( Capabilities, Capability )
100 
101 
102  static const int EditingCapabilities = AddFeatures | DeleteFeatures |
103  ChangeAttributeValues | ChangeGeometries | AddAttributes | DeleteAttributes |
104  RenameAttributes;
105 
110  {
112  Uncounted = -2,
114  UnknownCount = -1,
115  };
116 
124  QgsVectorDataProvider( const QString &uri = QString(),
126  QgsDataProvider::ReadFlags flags = QgsDataProvider::ReadFlags() );
127 
145 
149  virtual QString storageType() const;
150 
156  QgsFeatureIterator getFeatures( const QgsFeatureRequest &request = QgsFeatureRequest() ) const override = 0;
157 
161  QgsWkbTypes::Type wkbType() const override = 0;
162 
167  long featureCount() const override = 0;
168 
174  virtual bool empty() const;
175 
187 
191  QgsFields fields() const override = 0;
192 
193  QgsCoordinateReferenceSystem sourceCrs() const override;
194  QgsRectangle sourceExtent() const override;
195  QString sourceName() const override { return QString(); }
196 
201  virtual QString dataComment() const override;
202 
211  QVariant minimumValue( int index ) const override;
212 
221  QVariant maximumValue( int index ) const override;
222 
232  virtual QStringList uniqueStringsMatching( int index, const QString &substring, int limit = -1,
233  QgsFeedback *feedback = nullptr ) const;
234 
247  virtual QVariant aggregate( QgsAggregateCalculator::Aggregate aggregate,
248  int index,
250  QgsExpressionContext *context,
251  bool &ok,
252  QgsFeatureIds *fids = nullptr ) const;
253 
260  virtual void enumValues( int index, QStringList &enumList SIP_OUT ) const { Q_UNUSED( index ) enumList.clear(); }
261 
262  bool addFeatures( QgsFeatureList &flist SIP_INOUT, QgsFeatureSink::Flags flags = QgsFeatureSink::Flags() ) override;
263  QString lastError() const override;
264 
271  virtual bool deleteFeatures( const QgsFeatureIds &id );
272 
280  virtual bool truncate();
281 
288  virtual bool cancelReload();
289 
295  virtual bool addAttributes( const QList<QgsField> &attributes );
296 
304  virtual bool deleteAttributes( const QgsAttributeIds &attributes );
305 
314  virtual bool renameAttributes( const QgsFieldNameMap &renamedAttributes );
315 
324  virtual bool changeAttributeValues( const QgsChangedAttributesMap &attr_map );
325 
337  virtual bool changeFeatures( const QgsChangedAttributesMap &attr_map,
338  const QgsGeometryMap &geometry_map );
339 
351  virtual QVariant defaultValue( int fieldIndex ) const;
352 
360  virtual QString defaultValueClause( int fieldIndex ) const;
361 
368  QgsFieldConstraints::Constraints fieldConstraints( int fieldIndex ) const;
369 
377  virtual bool skipConstraintCheck( int fieldIndex, QgsFieldConstraints::Constraint constraint, const QVariant &value = QVariant() ) const;
378 
386  virtual bool changeGeometryValues( const QgsGeometryMap &geometry_map );
387 
392  virtual bool createSpatialIndex();
393 
395  virtual bool createAttributeIndex( int field );
396 
403  virtual QgsVectorDataProvider::Capabilities capabilities() const;
404 
408  QString capabilitiesString() const;
409 
422  virtual void setEncoding( const QString &e );
423 
429  QString encoding() const;
430 
434  int fieldNameIndex( const QString &fieldName ) const;
435 
439  QMap<QString, int> fieldNameMap() const;
440 
444  virtual QgsAttributeList attributeIndexes() const;
445 
449  virtual QgsAttributeList pkAttributeIndexes() const;
450 
454  virtual QgsAttrPalIndexNameHash palAttributeIndexNames() const;
455 
459  bool supportedType( const QgsField &field ) const;
460 
461  struct NativeType
462  {
463  NativeType( const QString &typeDesc, const QString &typeName, QVariant::Type type, int minLen = 0, int maxLen = 0, int minPrec = 0, int maxPrec = 0, QVariant::Type subType = QVariant::Invalid )
464  : mTypeDesc( typeDesc )
465  , mTypeName( typeName )
466  , mType( type )
467  , mMinLen( minLen )
468  , mMaxLen( maxLen )
469  , mMinPrec( minPrec )
470  , mMaxPrec( maxPrec )
471  , mSubType( subType )
472  {}
473 
474  QString mTypeDesc;
475  QString mTypeName;
476  QVariant::Type mType;
477  int mMinLen;
478  int mMaxLen;
479  int mMinPrec;
480  int mMaxPrec;
481  QVariant::Type mSubType;
482  };
483 
487  QList< QgsVectorDataProvider::NativeType > nativeTypes() const;
488 
493  virtual bool doesStrictFeatureTypeCheck() const { return true; }
494 
496  static QStringList availableEncodings();
497 
501  bool hasErrors() const;
502 
506  void clearErrors();
507 
511  QStringList errors() const;
512 
517  virtual bool isSaveAndLoadStyleToDatabaseSupported() const;
518 
523  virtual bool isDeleteStyleFromDatabaseSupported() const;
524 
540  virtual QgsFeatureRenderer *createRenderer( const QVariantMap &configuration = QVariantMap() ) const SIP_FACTORY;
541 
557  virtual QgsAbstractVectorLayerLabeling *createLabeling( const QVariantMap &configuration = QVariantMap() ) const SIP_FACTORY;
558 
559  static QVariant convertValue( QVariant::Type type, const QString &value );
560 
564  virtual QgsTransaction *transaction() const;
565 
569  Q_DECL_DEPRECATED virtual void forceReload() SIP_DEPRECATED { reloadData(); }
570 
574  virtual QSet<QgsMapLayerDependency> dependencies() const;
575 
583  virtual QList<QgsRelation> discoverRelations( const QgsVectorLayer *self, const QList<QgsVectorLayer *> &layers ) const;
584 
589  virtual QVariantMap metadata() const { return QVariantMap(); }
590 
596  virtual QString translateMetadataKey( const QString &mdKey ) const { return mdKey; }
597 
604  virtual QString translateMetadataValue( const QString &mdKey, const QVariant &value ) const { Q_UNUSED( mdKey ) return value.toString(); }
605 
613  virtual bool hasMetadata() const { return true; }
614 
621  virtual void handlePostCloneOperations( QgsVectorDataProvider *source );
622 
625 
626  signals:
627 
633  void raiseError( const QString &msg ) const;
634 
635  protected:
636 
641  void clearMinMaxCache();
642 
646  void fillMinMaxCache() const;
647 
657  void pushError( const QString &msg ) const;
658 
663  QgsGeometry convertToProviderType( const QgsGeometry &geom ) const;
664 
671  void setNativeTypes( const QList<QgsVectorDataProvider::NativeType> &nativeTypes );
672 
678  QTextCodec *textEncoding() const;
679 
680  private:
681  mutable bool mCacheMinMaxDirty = true;
682  mutable QMap<int, QVariant> mCacheMinValues, mCacheMaxValues;
683 
685  QTextCodec *mEncoding = nullptr;
686 
688  QgsAttributeList mAttributesToFetch;
689 
691  QList< NativeType > mNativeTypes;
692 
694  mutable QStringList mErrors;
695 
696  std::unique_ptr< QgsVectorDataProviderTemporalCapabilities > mTemporalCapabilities;
697 
698  static QStringList sEncodings;
699 
703  virtual void setTransaction( QgsTransaction * /*transaction*/ ) {}
704 };
705 
706 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsVectorDataProvider::Capabilities )
707 
708 #endif
QgsExpressionContext
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Definition: qgsexpressioncontext.h:370
qgsvectordataprovidertemporalcapabilities.h
QgsAttributeIds
QSet< int > QgsAttributeIds
Definition: qgsvectordataprovider.h:39
QgsFeatureSource::minimumValue
virtual QVariant minimumValue(int fieldIndex) const
Returns the minimum value for an attribute column or an invalid variant in case of error.
Definition: qgsfeaturesource.cpp:52
QgsVectorDataProvider::NativeType::mMaxPrec
int mMaxPrec
Definition: qgsvectordataprovider.h:480
QgsFeatureSource::sourceCrs
virtual QgsCoordinateReferenceSystem sourceCrs() const =0
Returns the coordinate reference system for features in the source.
qgsfeaturerequest.h
QgsVectorDataProvider::NativeType::mMinPrec
int mMinPrec
Definition: qgsvectordataprovider.h:479
QgsDataProvider::ProviderOptions
Setting options for creating vector data providers.
Definition: qgsdataprovider.h:105
QgsDataProvider
Abstract base class for spatial data provider implementations.
Definition: qgsdataprovider.h:42
QgsVectorDataProvider::FeatureCountState
FeatureCountState
Enumeration of feature count states.
Definition: qgsvectordataprovider.h:110
QgsVectorDataProvider::metadata
virtual QVariantMap metadata() const
Gets metadata, dependent on the provider type, that will be display in the metadata tab of the layer ...
Definition: qgsvectordataprovider.h:589
qgsmaplayerdependency.h
QgsAggregateCalculator::AggregateParameters
A bundle of parameters controlling aggregate calculation.
Definition: qgsaggregatecalculator.h:92
QgsVectorDataProvider::fields
QgsFields fields() const override=0
Returns the fields associated with this data provider.
SIP_OUT
#define SIP_OUT
Definition: qgis_sip.h:58
QgsVectorDataProvider::enumValues
virtual void enumValues(int index, QStringList &enumList) const
Returns the possible enum values of an attribute.
Definition: qgsvectordataprovider.h:260
QgsFields
Container of fields for a vector layer.
Definition: qgsfields.h:45
QgsVectorDataProvider::NativeType::mMinLen
int mMinLen
Definition: qgsvectordataprovider.h:477
qgsfeaturesink.h
qgsfeature.h
QgsFeatureSource
An interface for objects which provide features via a getFeatures method.
Definition: qgsfeaturesource.h:38
QgsWkbTypes::Type
Type
The WKB type describes the number of dimensions a geometry has.
Definition: qgswkbtypes.h:70
QgsVectorDataProvider::doesStrictFeatureTypeCheck
virtual bool doesStrictFeatureTypeCheck() const
Returns true if the provider is strict about the type of inserted features (e.g.
Definition: qgsvectordataprovider.h:493
QgsTransaction
This class allows including a set of layers in a database-side transaction, provided the layer data p...
Definition: qgstransaction.h:57
QgsChangedAttributesMap
QMap< QgsFeatureId, QgsAttributeMap > QgsChangedAttributesMap
Definition: qgsfeature.h:569
field
const QgsField & field
Definition: qgsfield.h:456
QgsFieldConstraints::Constraint
Constraint
Constraints which may be present on a field.
Definition: qgsfieldconstraints.h:44
QgsVectorDataProvider::NativeType
Definition: qgsvectordataprovider.h:462
QgsVectorDataProvider::hasMetadata
virtual bool hasMetadata() const
Returns true if the data source has metadata, false otherwise.
Definition: qgsvectordataprovider.h:613
QgsAttributeList
QList< int > QgsAttributeList
Definition: qgsfield.h:26
QgsRectangle
A rectangle specified with double values.
Definition: qgsrectangle.h:42
SIP_FACTORY
#define SIP_FACTORY
Definition: qgis_sip.h:76
qgsaggregatecalculator.h
geos::unique_ptr
std::unique_ptr< GEOSGeometry, GeosDeleter > unique_ptr
Scoped GEOS pointer.
Definition: qgsgeos.h:79
SIP_DEPRECATED
#define SIP_DEPRECATED
Definition: qgis_sip.h:106
QgsFeatureSource::hasFeatures
virtual FeatureAvailability hasFeatures() const
Determines if there are any features available in the source.
Definition: qgsfeaturesource.cpp:26
QgsVectorDataProvider::Capability
Capability
enumeration with capabilities that providers might implement
Definition: qgsvectordataprovider.h:73
QgsFeatureRequest
This class wraps a request for features to a vector layer (or directly its vector data provider).
Definition: qgsfeaturerequest.h:76
QgsVectorDataProvider::translateMetadataValue
virtual QString translateMetadataValue(const QString &mdKey, const QVariant &value) const
Gets the translated metadata value.
Definition: qgsvectordataprovider.h:604
QgsFeatureSource::maximumValue
virtual QVariant maximumValue(int fieldIndex) const
Returns the maximum value for an attribute column or an invalid variant in case of error.
Definition: qgsfeaturesource.cpp:75
SIP_SKIP
#define SIP_SKIP
Definition: qgis_sip.h:126
QgsDataProvider::reloadData
virtual void reloadData()
Reloads the data from the source by calling reloadProviderData() implemented by providers with data c...
Definition: qgsdataprovider.cpp:40
QgsVectorDataProvider::featureCount
long featureCount() const override=0
Number of features in the layer.
QgsVectorDataProvider::NativeType::mTypeDesc
QString mTypeDesc
Definition: qgsvectordataprovider.h:474
QgsVectorDataProvider::featureSource
virtual QgsAbstractFeatureSource * featureSource() const =0
Returns feature source object that can be used for querying provider's data.
QgsFeedback
Base class for feedback objects to be used for cancellation of something running in a worker thread.
Definition: qgsfeedback.h:44
QgsVectorDataProvider::NativeType::mMaxLen
int mMaxLen
Definition: qgsvectordataprovider.h:478
qgis_sip.h
QgsAbstractFeatureSource
Base class that can be used for any class that is capable of returning features.
Definition: qgsfeaturerequest.h:743
QgsVectorDataProvider::NativeType::NativeType
NativeType(const QString &typeDesc, const QString &typeName, QVariant::Type type, int minLen=0, int maxLen=0, int minPrec=0, int maxPrec=0, QVariant::Type subType=QVariant::Invalid)
Definition: qgsvectordataprovider.h:463
QgsVectorDataProvider::sourceName
QString sourceName() const override
Returns a friendly display name for the source.
Definition: qgsvectordataprovider.h:195
qgsfeaturesource.h
Q_DECLARE_OPERATORS_FOR_FLAGS
Q_DECLARE_OPERATORS_FOR_FLAGS(QgsField::ConfigurationFlags) CORE_EXPORT QDataStream &operator<<(QDataStream &out
Writes the field to stream out. QGIS version compatibility is not guaranteed.
QgsFeatureList
QList< QgsFeature > QgsFeatureList
Definition: qgsfeature.h:583
QgsVectorDataProvider::NativeType::mTypeName
QString mTypeName
Definition: qgsvectordataprovider.h:475
QgsVectorDataProvider::NativeType::mSubType
QVariant::Type mSubType
Definition: qgsvectordataprovider.h:481
typeName
const QString & typeName
Definition: qgswfsgetfeature.cpp:55
QgsFeatureSource::sourceExtent
virtual QgsRectangle sourceExtent() const
Returns the extent of all geometries from the source.
Definition: qgsfeaturesource.cpp:98
qgsrelation.h
QgsVectorDataProvider::translateMetadataKey
virtual QString translateMetadataKey(const QString &mdKey) const
Gets the translated metadata key.
Definition: qgsvectordataprovider.h:596
QgsVectorDataProvider::getFeatures
QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest()) const override=0
Query the provider for features specified in request.
QgsFeatureIds
QSet< QgsFeatureId > QgsFeatureIds
Definition: qgsfeatureid.h:37
QgsCoordinateReferenceSystem
This class represents a coordinate reference system (CRS).
Definition: qgscoordinatereferencesystem.h:206
QgsAbstractVectorLayerLabeling
Abstract base class - its implementations define different approaches to the labeling of a vector lay...
Definition: qgsvectorlayerlabeling.h:42
SIP_INOUT
#define SIP_INOUT
Definition: qgis_sip.h:71
QgsFeatureSource::FeatureAvailability
FeatureAvailability
Possible return value for hasFeatures() to determine if a source is empty.
Definition: qgsfeaturesource.h:51
QgsVectorDataProvider::NativeType::mType
QVariant::Type mType
Definition: qgsvectordataprovider.h:476
QgsAggregateCalculator::Aggregate
Aggregate
Available aggregates to calculate.
Definition: qgsaggregatecalculator.h:65
QgsFeatureRenderer
Definition: qgsrenderer.h:103
QgsDataProvider::temporalCapabilities
virtual QgsDataProviderTemporalCapabilities * temporalCapabilities()
Returns the provider's temporal capabilities.
Definition: qgsdataprovider.cpp:30
QgsFeatureSink::lastError
virtual QString lastError() const
Returns the most recent error encountered by the sink, e.g.
Definition: qgsfeaturesink.h:115
QgsGeometry
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:124
QgsVectorLayerEditBuffer
Definition: qgsvectorlayereditbuffer.h:37
QgsVectorDataProviderTemporalCapabilities
Implementation of data provider temporal properties for QgsVectorDataProviders.
Definition: qgsvectordataprovidertemporalcapabilities.h:38
QgsVectorLayer
Represents a vector layer which manages a vector based data sets.
Definition: qgsvectorlayer.h:387
QgsFieldNameMap
QMap< int, QString > QgsFieldNameMap
Definition: qgsattributes.h:44
qgsdataprovider.h
QgsAttrPalIndexNameHash
QHash< int, QString > QgsAttrPalIndexNameHash
Definition: qgsvectordataprovider.h:40
QgsGeometryMap
QMap< QgsFeatureId, QgsGeometry > QgsGeometryMap
Definition: qgsfeature.h:578
QgsVectorDataProvider
This is the base class for vector data providers.
Definition: qgsvectordataprovider.h:59
QgsVectorLayerEditBuffer::deleteFeatures
virtual bool deleteFeatures(const QgsFeatureIds &fid)
Deletes a set of features from the layer (but does not commit it)
Definition: qgsvectorlayereditbuffer.cpp:182
QgsDataProvider::dataComment
virtual QString dataComment() const
Returns a short comment for the data that this provider is providing access to (e....
Definition: qgsdataprovider.h:179
QgsFeatureIterator
Wrapper for iterator of features from vector data provider or vector layer.
Definition: qgsfeatureiterator.h:265
QgsVectorLayerEditBuffer::changeAttributeValues
virtual bool changeAttributeValues(QgsFeatureId fid, const QgsAttributeMap &newValues, const QgsAttributeMap &oldValues)
Changes values of attributes (but does not commit it).
Definition: qgsvectorlayereditbuffer.cpp:220
attributeIndexes
const QgsAttributeList & attributeIndexes
Definition: qgswfsgetfeature.cpp:53
QgsFeatureSink
An interface for objects which accept features via addFeature(s) methods.
Definition: qgsfeaturesink.h:34
QgsVectorLayerEditBuffer::addFeatures
virtual bool addFeatures(QgsFeatureList &features)
Insert a copy of the given features into the layer (but does not commit it)
Definition: qgsvectorlayereditbuffer.cpp:136
QgsVectorDataProvider::wkbType
QgsWkbTypes::Type wkbType() const override=0
Returns the geometry type which is returned by this layer.
QgsField
Encapsulate a field in an attribute table or data source.
Definition: qgsfield.h:50