QGIS API Documentation  3.21.0-Master (2d5a580dfc)
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  ReloadData = 1 << 26,
98  FeatureSymbology = 1 << 27,
99  };
100 
101  Q_DECLARE_FLAGS( Capabilities, Capability )
102 
103 
104  static const int EditingCapabilities = AddFeatures | DeleteFeatures |
105  ChangeAttributeValues | ChangeGeometries | AddAttributes | DeleteAttributes |
106  RenameAttributes;
107 
115  QgsVectorDataProvider( const QString &uri = QString(),
116  const QgsDataProvider::ProviderOptions &providerOptions = QgsDataProvider::ProviderOptions(),
117  QgsDataProvider::ReadFlags flags = QgsDataProvider::ReadFlags() );
118 
135  virtual QgsAbstractFeatureSource *featureSource() const = 0 SIP_FACTORY;
136 
140  virtual QString storageType() const;
141 
147  QgsFeatureIterator getFeatures( const QgsFeatureRequest &request = QgsFeatureRequest() ) const override = 0;
148 
152  QgsWkbTypes::Type wkbType() const override = 0;
153 
158  long long featureCount() const override = 0;
159 
165  virtual bool empty() const;
166 
177  QgsFeatureSource::FeatureAvailability hasFeatures() const override;
178 
182  QgsFields fields() const override = 0;
183 
184  QgsCoordinateReferenceSystem sourceCrs() const override;
185  QgsRectangle sourceExtent() const override;
186  QString sourceName() const override { return QString(); }
187 
192  virtual QString dataComment() const override;
193 
202  QVariant minimumValue( int index ) const override;
203 
212  QVariant maximumValue( int index ) const override;
213 
223  virtual QStringList uniqueStringsMatching( int index, const QString &substring, int limit = -1,
224  QgsFeedback *feedback = nullptr ) const;
225 
238  virtual QVariant aggregate( QgsAggregateCalculator::Aggregate aggregate,
239  int index,
241  QgsExpressionContext *context,
242  bool &ok,
243  QgsFeatureIds *fids = nullptr ) const;
244 
251  virtual void enumValues( int index, QStringList &enumList SIP_OUT ) const { Q_UNUSED( index ) enumList.clear(); }
252 
253  bool addFeatures( QgsFeatureList &flist SIP_INOUT, QgsFeatureSink::Flags flags = QgsFeatureSink::Flags() ) override;
254  QString lastError() const override;
255 
262  virtual bool deleteFeatures( const QgsFeatureIds &id );
263 
271  virtual bool truncate();
272 
279  virtual bool cancelReload();
280 
286  virtual bool addAttributes( const QList<QgsField> &attributes );
287 
295  virtual bool deleteAttributes( const QgsAttributeIds &attributes );
296 
305  virtual bool renameAttributes( const QgsFieldNameMap &renamedAttributes );
306 
315  virtual bool changeAttributeValues( const QgsChangedAttributesMap &attr_map );
316 
328  virtual bool changeFeatures( const QgsChangedAttributesMap &attr_map,
329  const QgsGeometryMap &geometry_map );
330 
342  virtual QVariant defaultValue( int fieldIndex ) const;
343 
351  virtual QString defaultValueClause( int fieldIndex ) const;
352 
359  QgsFieldConstraints::Constraints fieldConstraints( int fieldIndex ) const;
360 
368  virtual bool skipConstraintCheck( int fieldIndex, QgsFieldConstraints::Constraint constraint, const QVariant &value = QVariant() ) const;
369 
377  virtual bool changeGeometryValues( const QgsGeometryMap &geometry_map );
378 
383  virtual bool createSpatialIndex();
384 
386  virtual bool createAttributeIndex( int field );
387 
394  Q_INVOKABLE virtual QgsVectorDataProvider::Capabilities capabilities() const;
395 
399  QString capabilitiesString() const;
400 
413  virtual void setEncoding( const QString &e );
414 
420  QString encoding() const;
421 
425  int fieldNameIndex( const QString &fieldName ) const;
426 
430  QMap<QString, int> fieldNameMap() const;
431 
435  virtual QgsAttributeList attributeIndexes() const;
436 
440  virtual QgsAttributeList pkAttributeIndexes() const;
441 
445  virtual QgsAttrPalIndexNameHash palAttributeIndexNames() const;
446 
450  bool supportedType( const QgsField &field ) const;
451 
452  struct NativeType
453  {
454  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 )
455  : mTypeDesc( typeDesc )
456  , mTypeName( typeName )
457  , mType( type )
458  , mMinLen( minLen )
459  , mMaxLen( maxLen )
460  , mMinPrec( minPrec )
461  , mMaxPrec( maxPrec )
462  , mSubType( subType )
463  {}
464 
465  QString mTypeDesc;
466  QString mTypeName;
467  QVariant::Type mType;
468  int mMinLen;
469  int mMaxLen;
470  int mMinPrec;
471  int mMaxPrec;
472  QVariant::Type mSubType;
473  };
474 
478  QList< QgsVectorDataProvider::NativeType > nativeTypes() const;
479 
484  virtual bool doesStrictFeatureTypeCheck() const { return true; }
485 
487  static QStringList availableEncodings();
488 
492  bool hasErrors() const;
493 
497  void clearErrors();
498 
502  QStringList errors() const;
503 
508  virtual bool isSaveAndLoadStyleToDatabaseSupported() const;
509 
514  virtual bool isDeleteStyleFromDatabaseSupported() const;
515 
531  virtual QgsFeatureRenderer *createRenderer( const QVariantMap &configuration = QVariantMap() ) const SIP_FACTORY;
532 
548  virtual QgsAbstractVectorLayerLabeling *createLabeling( const QVariantMap &configuration = QVariantMap() ) const SIP_FACTORY;
549 
550  static QVariant convertValue( QVariant::Type type, const QString &value );
551 
555  virtual QgsTransaction *transaction() const;
556 
560  Q_DECL_DEPRECATED virtual void forceReload() SIP_DEPRECATED { reloadData(); }
561 
565  virtual QSet<QgsMapLayerDependency> dependencies() const;
566 
574  virtual QList<QgsRelation> discoverRelations( const QgsVectorLayer *self, const QList<QgsVectorLayer *> &layers ) const;
575 
580  virtual QVariantMap metadata() const { return QVariantMap(); }
581 
587  virtual QString translateMetadataKey( const QString &mdKey ) const { return mdKey; }
588 
595  virtual QString translateMetadataValue( const QString &mdKey, const QVariant &value ) const { Q_UNUSED( mdKey ) return value.toString(); }
596 
604  virtual bool hasMetadata() const { return true; }
605 
612  virtual void handlePostCloneOperations( QgsVectorDataProvider *source );
613 
616 
617  signals:
618 
624  void raiseError( const QString &msg ) const;
625 
626  protected:
627 
632  void clearMinMaxCache();
633 
637  void fillMinMaxCache() const;
638 
648  void pushError( const QString &msg ) const;
649 
654  QgsGeometry convertToProviderType( const QgsGeometry &geom ) const;
655 
662  void setNativeTypes( const QList<QgsVectorDataProvider::NativeType> &nativeTypes );
663 
669  QTextCodec *textEncoding() const;
670 
671  private:
672  mutable bool mCacheMinMaxDirty = true;
673  mutable QMap<int, QVariant> mCacheMinValues, mCacheMaxValues;
674 
676  QTextCodec *mEncoding = nullptr;
677 
679  QgsAttributeList mAttributesToFetch;
680 
682  QList< NativeType > mNativeTypes;
683 
685  mutable QStringList mErrors;
686 
687  std::unique_ptr< QgsVectorDataProviderTemporalCapabilities > mTemporalCapabilities;
688 
689  static QStringList sEncodings;
690 
694  virtual void setTransaction( QgsTransaction * /*transaction*/ ) {}
695 };
696 
697 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsVectorDataProvider::Capabilities )
698 
699 #endif
Base class that can be used for any class that is capable of returning features.
Abstract base class - its implementations define different approaches to the labeling of a vector lay...
Aggregate
Available aggregates to calculate.
This class represents a coordinate reference system (CRS).
Abstract base class for spatial data provider implementations.
virtual QString dataComment() const
Returns a short comment for the data that this provider is providing access to (e....
virtual QgsDataProviderTemporalCapabilities * temporalCapabilities()
Returns the provider's temporal capabilities.
virtual void reloadData()
Reloads the data from the source for providers with data caches to synchronize, changes in the data s...
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Wrapper for iterator of features from vector data provider or vector layer.
This class wraps a request for features to a vector layer (or directly its vector data provider).
An interface for objects which accept features via addFeature(s) methods.
virtual QString lastError() const
Returns the most recent error encountered by the sink, e.g.
An interface for objects which provide features via a getFeatures method.
FeatureAvailability
Possible return value for hasFeatures() to determine if a source is empty.
virtual QVariant minimumValue(int fieldIndex) const
Returns the minimum value for an attribute column or an invalid variant in case of error.
virtual QVariant maximumValue(int fieldIndex) const
Returns the maximum value for an attribute column or an invalid variant in case of error.
Base class for feedback objects to be used for cancellation of something running in a worker thread.
Definition: qgsfeedback.h:45
Constraint
Constraints which may be present on a field.
Encapsulate a field in an attribute table or data source.
Definition: qgsfield.h:51
Container of fields for a vector layer.
Definition: qgsfields.h:45
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:124
A rectangle specified with double values.
Definition: qgsrectangle.h:42
This class allows including a set of layers in a database-side transaction, provided the layer data p...
Implementation of data provider temporal properties for QgsVectorDataProviders.
This is the base class for vector data providers.
virtual void enumValues(int index, QStringList &enumList) const
Returns the possible enum values of an attribute.
Capability
enumeration with capabilities that providers might implement
virtual QString translateMetadataValue(const QString &mdKey, const QVariant &value) const
Gets the translated metadata value.
virtual bool doesStrictFeatureTypeCheck() const
Returns true if the provider is strict about the type of inserted features (e.g.
virtual bool hasMetadata() const
Returns true if the data source has metadata, false otherwise.
virtual QString translateMetadataKey(const QString &mdKey) const
Gets the translated metadata key.
virtual QVariantMap metadata() const
Gets metadata, dependent on the provider type, that will be display in the metadata tab of the layer ...
virtual bool deleteFeatures(const QgsFeatureIds &fid)
Deletes a set of features from the layer (but does not commit it)
virtual bool addFeatures(QgsFeatureList &features)
Insert a copy of the given features into the layer (but does not commit it)
virtual bool changeAttributeValues(QgsFeatureId fid, const QgsAttributeMap &newValues, const QgsAttributeMap &oldValues)
Changes values of attributes (but does not commit it).
Represents a vector layer which manages a vector based data sets.
Handles storage of information regarding WKB types and their properties.
Definition: qgswkbtypes.h:42
std::unique_ptr< GEOSGeometry, GeosDeleter > unique_ptr
Scoped GEOS pointer.
Definition: qgsgeos.h:79
#define SIP_DEPRECATED
Definition: qgis_sip.h:106
#define SIP_SKIP
Definition: qgis_sip.h:126
#define SIP_OUT
Definition: qgis_sip.h:58
#define SIP_FACTORY
Definition: qgis_sip.h:76
#define SIP_INOUT
Definition: qgis_sip.h:71
QMap< int, QString > QgsFieldNameMap
Definition: qgsattributes.h:44
QMap< QgsFeatureId, QgsGeometry > QgsGeometryMap
Definition: qgsfeature.h:826
QMap< QgsFeatureId, QgsAttributeMap > QgsChangedAttributesMap
Definition: qgsfeature.h:817
QList< QgsFeature > QgsFeatureList
Definition: qgsfeature.h:831
QSet< QgsFeatureId > QgsFeatureIds
Definition: qgsfeatureid.h:37
QList< int > QgsAttributeList
Definition: qgsfield.h:26
const QgsField & field
Definition: qgsfield.h:463
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.
QSet< int > QgsAttributeIds
QHash< int, QString > QgsAttrPalIndexNameHash
const QString & typeName
const QgsAttributeList & attributeIndexes
A bundle of parameters controlling aggregate calculation.
Setting options for creating vector data providers.
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)