QGIS API Documentation  2.99.0-Master (5b186ae)
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 
44 #include "qgsfeaturerequest.h"
45 
54 class CORE_EXPORT QgsVectorDataProvider : public QgsDataProvider, public QgsFeatureSink, public QgsFeatureSource
55 {
56  Q_OBJECT
57 
58  friend class QgsTransaction;
60 
61  public:
62 
63  // If you add to this, please also add to capabilitiesString()
64 
69  {
70  NoCapabilities = 0,
71  AddFeatures = 1,
72  DeleteFeatures = 1 << 1,
73  ChangeAttributeValues = 1 << 2,
74  AddAttributes = 1 << 3,
75  DeleteAttributes = 1 << 4,
76  CreateSpatialIndex = 1 << 6,
77  SelectAtId = 1 << 7,
78  ChangeGeometries = 1 << 8,
79  SelectEncoding = 1 << 13,
80  CreateAttributeIndex = 1 << 12,
81  SimplifyGeometries = 1 << 14,
82  SimplifyGeometriesWithTopologicalValidation = 1 << 15,
83  TransactionSupport = 1 << 16,
84  CircularGeometries = 1 << 17,
85  ChangeFeatures = 1 << 18,
88  RenameAttributes = 1 << 19,
89  FastTruncate = 1 << 20,
90  };
91 
92  Q_DECLARE_FLAGS( Capabilities, Capability )
93 
94 
95  static const int EditingCapabilities = AddFeatures | DeleteFeatures |
96  ChangeAttributeValues | ChangeGeometries | AddAttributes | DeleteAttributes |
97  RenameAttributes;
98 
103  {
105  Uncounted = -2,
107  UnknownCount = -1,
108  };
109 
114  QgsVectorDataProvider( const QString &uri = QString() );
115 
132  virtual QgsAbstractFeatureSource *featureSource() const = 0 SIP_FACTORY;
133 
137  virtual QString storageType() const;
138 
144  virtual QgsFeatureIterator getFeatures( const QgsFeatureRequest &request = QgsFeatureRequest() ) const override = 0;
145 
149  virtual QgsWkbTypes::Type wkbType() const override = 0;
150 
155  virtual long featureCount() const override = 0;
156 
160  virtual QgsFields fields() const override = 0;
161 
162  QgsCoordinateReferenceSystem sourceCrs() const override;
163  QgsRectangle sourceExtent() const override;
164  QString sourceName() const override { return QString(); }
165 
170  virtual QString dataComment() const;
171 
180  virtual QVariant minimumValue( int index ) const override;
181 
190  virtual QVariant maximumValue( int index ) const override;
191 
201  virtual QStringList uniqueStringsMatching( int index, const QString &substring, int limit = -1,
202  QgsFeedback *feedback = nullptr ) const;
203 
214  virtual QVariant aggregate( QgsAggregateCalculator::Aggregate aggregate,
215  int index,
217  QgsExpressionContext *context,
218  bool &ok ) const;
219 
226  virtual void enumValues( int index, QStringList &enumList SIP_OUT ) const { Q_UNUSED( index ); enumList.clear(); }
227 
228  virtual bool addFeatures( QgsFeatureList &flist SIP_INOUT, QgsFeatureSink::Flags flags = 0 ) override;
229 
236  virtual bool deleteFeatures( const QgsFeatureIds &id );
237 
245  virtual bool truncate();
246 
252  virtual bool addAttributes( const QList<QgsField> &attributes );
253 
261  virtual bool deleteAttributes( const QgsAttributeIds &attributes );
262 
271  virtual bool renameAttributes( const QgsFieldNameMap &renamedAttributes );
272 
279  virtual bool changeAttributeValues( const QgsChangedAttributesMap &attr_map );
280 
292  virtual bool changeFeatures( const QgsChangedAttributesMap &attr_map,
293  const QgsGeometryMap &geometry_map );
294 
306  virtual QVariant defaultValue( int fieldIndex ) const;
307 
315  virtual QString defaultValueClause( int fieldIndex ) const;
316 
323  QgsFieldConstraints::Constraints fieldConstraints( int fieldIndex ) const;
324 
332  virtual bool skipConstraintCheck( int fieldIndex, QgsFieldConstraints::Constraint constraint, const QVariant &value = QVariant() ) const;
333 
341  virtual bool changeGeometryValues( const QgsGeometryMap &geometry_map );
342 
347  virtual bool createSpatialIndex();
348 
350  virtual bool createAttributeIndex( int field );
351 
357  virtual QgsVectorDataProvider::Capabilities capabilities() const;
358 
362  QString capabilitiesString() const;
363 
367  virtual void setEncoding( const QString &e );
368 
372  QString encoding() const;
373 
377  int fieldNameIndex( const QString &fieldName ) const;
378 
382  QMap<QString, int> fieldNameMap() const;
383 
387  virtual QgsAttributeList attributeIndexes() const;
388 
392  virtual QgsAttributeList pkAttributeIndexes() const;
393 
397  virtual QgsAttrPalIndexNameHash palAttributeIndexNames() const;
398 
402  bool supportedType( const QgsField &field ) const;
403 
404  struct NativeType
405  {
406  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 )
407  : mTypeDesc( typeDesc )
408  , mTypeName( typeName )
409  , mType( type )
410  , mMinLen( minLen )
411  , mMaxLen( maxLen )
412  , mMinPrec( minPrec )
413  , mMaxPrec( maxPrec )
414  , mSubType( subType )
415  {}
416 
417  QString mTypeDesc;
418  QString mTypeName;
419  QVariant::Type mType;
420  int mMinLen;
421  int mMaxLen;
422  int mMinPrec;
423  int mMaxPrec;
424  QVariant::Type mSubType;
425  };
426 
430  QList< QgsVectorDataProvider::NativeType > nativeTypes() const;
431 
436  virtual bool doesStrictFeatureTypeCheck() const { return true; }
437 
439  static QStringList availableEncodings();
440 
444  bool hasErrors() const;
445 
449  void clearErrors();
450 
454  QStringList errors() const;
455 
460  virtual bool isSaveAndLoadStyleToDatabaseSupported() const;
461 
466  virtual bool isDeleteStyleFromDatabaseSupported() const;
467 
468  static QVariant convertValue( QVariant::Type type, const QString &value );
469 
473  virtual QgsTransaction *transaction() const;
474 
482  virtual void forceReload();
483 
487  virtual QSet<QgsMapLayerDependency> dependencies() const;
488 
496  virtual QList<QgsRelation> discoverRelations( const QgsVectorLayer *self, const QList<QgsVectorLayer *> &layers ) const;
497 
502  virtual QVariantMap metadata() const { return QVariantMap(); }
503 
509  virtual QString translateMetadataKey( const QString &mdKey ) const { return mdKey; }
510 
517  virtual QString translateMetadataValue( const QString &mdKey, const QVariant &value ) const { Q_UNUSED( mdKey ); return value.toString(); }
518 
519  signals:
520 
526  void raiseError( const QString &msg ) const;
527 
528  protected:
529 
534  void clearMinMaxCache();
535 
539  void fillMinMaxCache() const;
540 
550  void pushError( const QString &msg ) const;
551 
556  QgsGeometry convertToProviderType( const QgsGeometry &geom ) const;
557 
564  void setNativeTypes( const QList<QgsVectorDataProvider::NativeType> &nativeTypes );
565 
571  QTextCodec *textEncoding() const;
572 
573  private:
574  mutable bool mCacheMinMaxDirty;
575  mutable QMap<int, QVariant> mCacheMinValues, mCacheMaxValues;
576 
578  QTextCodec *mEncoding = nullptr;
579 
581  QgsAttributeList mAttributesToFetch;
582 
584  QList< NativeType > mNativeTypes;
585 
587  QMap<QString, QVariant::Type> mOldTypeList;
588 
590  mutable QStringList mErrors;
591 
592  static QStringList sEncodings;
593 
597  virtual void setTransaction( QgsTransaction * /*transaction*/ ) {}
598 
599 };
600 
601 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsVectorDataProvider::Capabilities )
602 
603 #endif
Wrapper for iterator of features from vector data provider or vector layer.
QMap< QgsFeatureId, QgsGeometry > QgsGeometryMap
Definition: qgsfeature.h:512
virtual QgsRectangle sourceExtent() const
Returns the extent of all geometries from the source.
A rectangle specified with double values.
Definition: qgsrectangle.h:38
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)
Constraint
Constraints which may be present on a field.
QSet< QgsFeatureId > QgsFeatureIds
Definition: qgsfeature.h:519
QList< QgsFeature > QgsFeatureList
Definition: qgsfeature.h:524
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:41
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:96
Abstract base class for spatial data provider implementations.
#define SIP_INOUT
Definition: qgis_sip.h:64
virtual QString translateMetadataValue(const QString &mdKey, const QVariant &value) const
Get the translated metadata value.
Base class for feedback objects to be used for cancelation of something running in a worker thread...
Definition: qgsfeedback.h:43
virtual bool doesStrictFeatureTypeCheck() const
Returns true if the provider is strict about the type of inserted features (e.g.
QSet< int > QgsAttributeIds
Type
The WKB type describes the number of dimensions a geometry has.
Definition: qgswkbtypes.h:66
virtual QVariant minimumValue(int fieldIndex) const
Returns the minimum value for an attribute column or an invalid variant in case of error...
QString sourceName() const override
Returns a friendly display name for the source.
#define SIP_SKIP
Definition: qgis_sip.h:119
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...
virtual QString translateMetadataKey(const QString &mdKey) const
Get the translated metadata key.
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:46
Base class that can be used for any class that is capable of returning features.
QMap< int, QString > QgsFieldNameMap
Definition: qgsattributes.h:45
virtual QgsCoordinateReferenceSystem sourceCrs() const =0
Returns the coordinate reference system for features in the source.
QMap< QgsFeatureId, QgsAttributeMap > QgsChangedAttributesMap
Definition: qgsfeature.h:503
An interface for objects which provide features via a getFeatures method.
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).
virtual QVariant maximumValue(int fieldIndex) const
Returns the maximum value for an attribute column or an invalid variant in case of error...
virtual void enumValues(int index, QStringList &enumList) const
Returns the possible enum values of an attribute.
QList< int > QgsAttributeList
Definition: qgsfield.h:27
This is the base class for vector data providers.
Represents a vector layer which manages a vector based data sets.
QHash< int, QString > QgsAttrPalIndexNameHash
Aggregate
Available aggregates to calculate.
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...
virtual QVariantMap metadata() const
Get metadata, dependent on the provider type, that will be display in the metadata tab of the layer p...