QGIS API Documentation  3.4.15-Madeira (e83d02e274)
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.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 
36 typedef QList<int> QgsAttributeList SIP_SKIP;
37 typedef QSet<int> QgsAttributeIds SIP_SKIP;
38 typedef QHash<int, QString> QgsAttrPalIndexNameHash;
39 
40 class QgsFeatureIterator;
41 class QgsTransaction;
42 class QgsFeedback;
43 class QgsFeatureRenderer;
44 
45 #include "qgsfeaturerequest.h"
46 
56 class CORE_EXPORT QgsVectorDataProvider : public QgsDataProvider, public QgsFeatureSink, public QgsFeatureSource
57 {
58  Q_OBJECT
59 
60  friend class QgsTransaction;
62 
63  public:
64 
65  // If you add to this, please also add to capabilitiesString()
66 
71  {
72  NoCapabilities = 0,
73  AddFeatures = 1,
74  DeleteFeatures = 1 << 1,
75  ChangeAttributeValues = 1 << 2,
76  AddAttributes = 1 << 3,
77  DeleteAttributes = 1 << 4,
78  CreateSpatialIndex = 1 << 6,
79  SelectAtId = 1 << 7,
80  ChangeGeometries = 1 << 8,
81  SelectEncoding = 1 << 13,
82  CreateAttributeIndex = 1 << 12,
83  SimplifyGeometries = 1 << 14,
84  SimplifyGeometriesWithTopologicalValidation = 1 << 15,
85  TransactionSupport = 1 << 16,
86  CircularGeometries = 1 << 17,
87  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  };
97 
98  Q_DECLARE_FLAGS( Capabilities, Capability )
99 
100 
101  static const int EditingCapabilities = AddFeatures | DeleteFeatures |
102  ChangeAttributeValues | ChangeGeometries | AddAttributes | DeleteAttributes |
103  RenameAttributes;
104 
109  {
111  Uncounted = -2,
113  UnknownCount = -1,
114  };
115 
123  QgsVectorDataProvider( const QString &uri = QString(), const QgsDataProvider::ProviderOptions &options = QgsDataProvider::ProviderOptions() );
124 
141  virtual QgsAbstractFeatureSource *featureSource() const = 0 SIP_FACTORY;
142 
146  virtual QString storageType() const;
147 
153  QgsFeatureIterator getFeatures( const QgsFeatureRequest &request = QgsFeatureRequest() ) const override = 0;
154 
158  QgsWkbTypes::Type wkbType() const override = 0;
159 
164  long featureCount() const override = 0;
165 
171  virtual bool empty() const;
172 
184 
188  QgsFields fields() const override = 0;
189 
190  QgsCoordinateReferenceSystem sourceCrs() const override;
191  QgsRectangle sourceExtent() const override;
192  QString sourceName() const override { return QString(); }
193 
198  virtual QString dataComment() const;
199 
208  QVariant minimumValue( int index ) const override;
209 
218  QVariant maximumValue( int index ) const override;
219 
229  virtual QStringList uniqueStringsMatching( int index, const QString &substring, int limit = -1,
230  QgsFeedback *feedback = nullptr ) const;
231 
243  virtual QVariant aggregate( QgsAggregateCalculator::Aggregate aggregate,
244  int index,
246  QgsExpressionContext *context,
247  bool &ok ) const;
248 
255  virtual void enumValues( int index, QStringList &enumList SIP_OUT ) const { Q_UNUSED( index ); enumList.clear(); }
256 
257  bool addFeatures( QgsFeatureList &flist SIP_INOUT, QgsFeatureSink::Flags flags = nullptr ) override;
258 
265  virtual bool deleteFeatures( const QgsFeatureIds &id );
266 
274  virtual bool truncate();
275 
282  virtual bool cancelReload();
283 
289  virtual bool addAttributes( const QList<QgsField> &attributes );
290 
298  virtual bool deleteAttributes( const QgsAttributeIds &attributes );
299 
308  virtual bool renameAttributes( const QgsFieldNameMap &renamedAttributes );
309 
316  virtual bool changeAttributeValues( const QgsChangedAttributesMap &attr_map );
317 
329  virtual bool changeFeatures( const QgsChangedAttributesMap &attr_map,
330  const QgsGeometryMap &geometry_map );
331 
343  virtual QVariant defaultValue( int fieldIndex ) const;
344 
352  virtual QString defaultValueClause( int fieldIndex ) const;
353 
360  QgsFieldConstraints::Constraints fieldConstraints( int fieldIndex ) const;
361 
369  virtual bool skipConstraintCheck( int fieldIndex, QgsFieldConstraints::Constraint constraint, const QVariant &value = QVariant() ) const;
370 
378  virtual bool changeGeometryValues( const QgsGeometryMap &geometry_map );
379 
384  virtual bool createSpatialIndex();
385 
387  virtual bool createAttributeIndex( int field );
388 
395  virtual QgsVectorDataProvider::Capabilities capabilities() const;
396 
400  QString capabilitiesString() const;
401 
405  virtual void setEncoding( const QString &e );
406 
410  QString encoding() const;
411 
415  int fieldNameIndex( const QString &fieldName ) const;
416 
420  QMap<QString, int> fieldNameMap() const;
421 
425  virtual QgsAttributeList attributeIndexes() const;
426 
430  virtual QgsAttributeList pkAttributeIndexes() const;
431 
435  virtual QgsAttrPalIndexNameHash palAttributeIndexNames() const;
436 
440  bool supportedType( const QgsField &field ) const;
441 
442  struct NativeType
443  {
444  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 )
445  : mTypeDesc( typeDesc )
446  , mTypeName( typeName )
447  , mType( type )
448  , mMinLen( minLen )
449  , mMaxLen( maxLen )
450  , mMinPrec( minPrec )
451  , mMaxPrec( maxPrec )
452  , mSubType( subType )
453  {}
454 
455  QString mTypeDesc;
456  QString mTypeName;
457  QVariant::Type mType;
458  int mMinLen;
459  int mMaxLen;
460  int mMinPrec;
461  int mMaxPrec;
462  QVariant::Type mSubType;
463  };
464 
468  QList< QgsVectorDataProvider::NativeType > nativeTypes() const;
469 
474  virtual bool doesStrictFeatureTypeCheck() const { return true; }
475 
477  static QStringList availableEncodings();
478 
482  bool hasErrors() const;
483 
487  void clearErrors();
488 
492  QStringList errors() const;
493 
498  virtual bool isSaveAndLoadStyleToDatabaseSupported() const;
499 
504  virtual bool isDeleteStyleFromDatabaseSupported() const;
505 
521  virtual QgsFeatureRenderer *createRenderer( const QVariantMap &configuration = QVariantMap() ) const SIP_FACTORY;
522 
523  static QVariant convertValue( QVariant::Type type, const QString &value );
524 
528  virtual QgsTransaction *transaction() const;
529 
537  virtual void forceReload();
538 
542  virtual QSet<QgsMapLayerDependency> dependencies() const;
543 
551  virtual QList<QgsRelation> discoverRelations( const QgsVectorLayer *self, const QList<QgsVectorLayer *> &layers ) const;
552 
557  virtual QVariantMap metadata() const { return QVariantMap(); }
558 
564  virtual QString translateMetadataKey( const QString &mdKey ) const { return mdKey; }
565 
572  virtual QString translateMetadataValue( const QString &mdKey, const QVariant &value ) const { Q_UNUSED( mdKey ); return value.toString(); }
573 
581  virtual bool hasMetadata() const { return true; }
582 
583  signals:
584 
590  void raiseError( const QString &msg ) const;
591 
592  protected:
593 
598  void clearMinMaxCache();
599 
603  void fillMinMaxCache() const;
604 
614  void pushError( const QString &msg ) const;
615 
620  QgsGeometry convertToProviderType( const QgsGeometry &geom ) const;
621 
628  void setNativeTypes( const QList<QgsVectorDataProvider::NativeType> &nativeTypes );
629 
635  QTextCodec *textEncoding() const;
636 
637  private:
638  mutable bool mCacheMinMaxDirty = true;
639  mutable QMap<int, QVariant> mCacheMinValues, mCacheMaxValues;
640 
642  QTextCodec *mEncoding = nullptr;
643 
645  QgsAttributeList mAttributesToFetch;
646 
648  QList< NativeType > mNativeTypes;
649 
651  mutable QStringList mErrors;
652 
653  static QStringList sEncodings;
654 
658  virtual void setTransaction( QgsTransaction * /*transaction*/ ) {}
659 
660 };
661 
662 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsVectorDataProvider::Capabilities )
663 
664 #endif
Wrapper for iterator of features from vector data provider or vector layer.
QMap< QgsFeatureId, QgsGeometry > QgsGeometryMap
Definition: qgsfeature.h:566
virtual QVariant maximumValue(int fieldIndex) const
Returns the maximum value for an attribute column or an invalid variant in case of error...
A rectangle specified with double values.
Definition: qgsrectangle.h:40
virtual QString translateMetadataKey(const QString &mdKey) const
Gets the translated metadata key.
QSet< QgsFeatureId > QgsFeatureIds
Definition: qgsfeatureid.h:34
virtual QgsFields fields() const =0
Returns the fields associated with features in the source.
virtual bool addFeatures(QgsFeatureList &features)
Insert a copy of the given features into the layer (but does not commit it)
FeatureAvailability
Possible return value for hasFeatures() to determine if a source is empty.
Constraint
Constraints which may be present on a field.
QList< QgsFeature > QgsFeatureList
Definition: qgsfeature.h:571
virtual QgsWkbTypes::Type wkbType() const =0
Returns the geometry type for features returned by this source.
An interface for objects which accept features via addFeature(s) methods.
Container of fields for a vector layer.
Definition: qgsfields.h:42
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:106
Abstract base class for spatial data provider implementations.
#define SIP_INOUT
Definition: qgis_sip.h:64
const QgsAttributeList & attributeIndexes
Base class for feedback objects to be used for cancellation of something running in a worker thread...
Definition: qgsfeedback.h:44
QSet< int > QgsAttributeIds
Type
The WKB type describes the number of dimensions a geometry has.
Definition: qgswkbtypes.h:68
QString sourceName() const override
Returns a friendly display name for the source.
#define SIP_SKIP
Definition: qgis_sip.h:119
const QString & typeName
virtual QgsRectangle sourceExtent() const
Returns the extent of all geometries from the source.
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)
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Capability
enumeration with capabilities that providers might implement
This class wraps a request for features to a vector layer (or directly its vector data provider)...
virtual bool deleteFeatures(const QgsFeatureIds &fid)
Deletes a set of features from the layer (but does not commit it)
#define SIP_FACTORY
Definition: qgis_sip.h:69
Encapsulate a field in an attribute table or data source.
Definition: qgsfield.h:48
virtual bool changeAttributeValues(QgsFeatureId fid, const QgsAttributeMap &newValues, const QgsAttributeMap &oldValues)
Changes values of attributes (but does not commit it).
Base class that can be used for any class that is capable of returning features.
QMap< int, QString > QgsFieldNameMap
Definition: qgsattributes.h:44
virtual QgsCoordinateReferenceSystem sourceCrs() const =0
Returns the coordinate reference system for features in the source.
Setting options for creating vector data providers.
virtual QVariant minimumValue(int fieldIndex) const
Returns the minimum value for an attribute column or an invalid variant in case of error...
QMap< QgsFeatureId, QgsAttributeMap > QgsChangedAttributesMap
Definition: qgsfeature.h:557
An interface for objects which provide features via a getFeatures method.
virtual bool doesStrictFeatureTypeCheck() const
Returns true if the provider is strict about the type of inserted features (e.g.
This class allows including a set of layers in a database-side transaction, provided the layer data p...
#define SIP_OUT
Definition: qgis_sip.h:51
This class represents a coordinate reference system (CRS).
QList< int > QgsAttributeList
Definition: qgsfield.h:27
virtual QString translateMetadataValue(const QString &mdKey, const QVariant &value) const
Gets the translated metadata value.
This is the base class for vector data providers.
virtual bool hasMetadata() const
Returns true if the data source has metadata, false otherwise.
Represents a vector layer which manages a vector based data sets.
QHash< int, QString > QgsAttrPalIndexNameHash
virtual void enumValues(int index, QStringList &enumList) const
Returns the possible enum values of an attribute.
virtual FeatureAvailability hasFeatures() const
Determines if there are any features available in the source.
Aggregate
Available aggregates to calculate.
virtual QVariantMap metadata() const
Gets metadata, dependent on the provider type, that will be display in the metadata tab of the layer ...
virtual QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest()) const =0
Returns an iterator for the features in the source.
A bundle of parameters controlling aggregate calculation.
FeatureCountState
Enumeration of feature count states.
virtual long featureCount() const =0
Returns the number of features contained in the source, or -1 if the feature count is unknown...