QGIS API Documentation  2.99.0-Master (ba079d8)
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;
59 
60  public:
61 
62  // If you add to this, please also add to capabilitiesString()
63 
68  {
69  NoCapabilities = 0,
70  AddFeatures = 1,
71  DeleteFeatures = 1 << 1,
72  ChangeAttributeValues = 1 << 2,
73  AddAttributes = 1 << 3,
74  DeleteAttributes = 1 << 4,
75  CreateSpatialIndex = 1 << 6,
76  SelectAtId = 1 << 7,
77  ChangeGeometries = 1 << 8,
78  SelectEncoding = 1 << 13,
79  CreateAttributeIndex = 1 << 12,
80  SimplifyGeometries = 1 << 14,
81  SimplifyGeometriesWithTopologicalValidation = 1 << 15,
82  TransactionSupport = 1 << 16,
83  CircularGeometries = 1 << 17,
84  ChangeFeatures = 1 << 18,
87  RenameAttributes = 1 << 19,
88  FastTruncate = 1 << 20,
89  };
90 
91  Q_DECLARE_FLAGS( Capabilities, Capability )
92 
93 
94  static const int EditingCapabilities = AddFeatures | DeleteFeatures |
95  ChangeAttributeValues | ChangeGeometries | AddAttributes | DeleteAttributes |
96  RenameAttributes;
97 
102  {
104  Uncounted = -2,
106  UnknownCount = -1,
107  };
108 
113  QgsVectorDataProvider( const QString &uri = QString() );
114 
131  virtual QgsAbstractFeatureSource *featureSource() const = 0 SIP_FACTORY;
132 
136  virtual QString storageType() const;
137 
143  virtual QgsFeatureIterator getFeatures( const QgsFeatureRequest &request = QgsFeatureRequest() ) const override = 0;
144 
148  virtual QgsWkbTypes::Type wkbType() const override = 0;
149 
154  virtual long featureCount() const override = 0;
155 
159  virtual QgsFields fields() const override = 0;
160 
161  QgsCoordinateReferenceSystem sourceCrs() const override;
162 
167  virtual QString dataComment() const;
168 
177  virtual QVariant minimumValue( int index ) const;
178 
187  virtual QVariant maximumValue( int index ) const;
188 
197  virtual void uniqueValues( int index, QList<QVariant> &uniqueValues SIP_OUT, int limit = -1 ) const;
198 
208  virtual QStringList uniqueStringsMatching( int index, const QString &substring, int limit = -1,
209  QgsFeedback *feedback = nullptr ) const;
210 
221  virtual QVariant aggregate( QgsAggregateCalculator::Aggregate aggregate,
222  int index,
224  QgsExpressionContext *context,
225  bool &ok ) const;
226 
233  virtual void enumValues( int index, QStringList &enumList SIP_OUT ) const { Q_UNUSED( index ); enumList.clear(); }
234 
235  virtual bool addFeatures( QgsFeatureList &flist SIP_INOUT ) override;
236 
243  virtual bool deleteFeatures( const QgsFeatureIds &id );
244 
252  virtual bool truncate();
253 
259  virtual bool addAttributes( const QList<QgsField> &attributes );
260 
268  virtual bool deleteAttributes( const QgsAttributeIds &attributes );
269 
278  virtual bool renameAttributes( const QgsFieldNameMap &renamedAttributes );
279 
286  virtual bool changeAttributeValues( const QgsChangedAttributesMap &attr_map );
287 
299  virtual bool changeFeatures( const QgsChangedAttributesMap &attr_map,
300  const QgsGeometryMap &geometry_map );
301 
313  virtual QVariant defaultValue( int fieldIndex ) const;
314 
322  virtual QString defaultValueClause( int fieldIndex ) const;
323 
330  QgsFieldConstraints::Constraints fieldConstraints( int fieldIndex ) const;
331 
339  virtual bool skipConstraintCheck( int fieldIndex, QgsFieldConstraints::Constraint constraint, const QVariant &value = QVariant() ) const;
340 
348  virtual bool changeGeometryValues( const QgsGeometryMap &geometry_map );
349 
354  virtual bool createSpatialIndex();
355 
357  virtual bool createAttributeIndex( int field );
358 
364  virtual QgsVectorDataProvider::Capabilities capabilities() const;
365 
369  QString capabilitiesString() const;
370 
374  virtual void setEncoding( const QString &e );
375 
379  QString encoding() const;
380 
384  int fieldNameIndex( const QString &fieldName ) const;
385 
389  QMap<QString, int> fieldNameMap() const;
390 
394  virtual QgsAttributeList attributeIndexes() const;
395 
399  virtual QgsAttributeList pkAttributeIndexes() const;
400 
404  virtual QgsAttrPalIndexNameHash palAttributeIndexNames() const;
405 
409  bool supportedType( const QgsField &field ) const;
410 
411  struct NativeType
412  {
413  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 )
414  : mTypeDesc( typeDesc )
415  , mTypeName( typeName )
416  , mType( type )
417  , mMinLen( minLen )
418  , mMaxLen( maxLen )
419  , mMinPrec( minPrec )
420  , mMaxPrec( maxPrec )
421  , mSubType( subType )
422  {}
423 
424  QString mTypeDesc;
425  QString mTypeName;
426  QVariant::Type mType;
427  int mMinLen;
428  int mMaxLen;
429  int mMinPrec;
430  int mMaxPrec;
431  QVariant::Type mSubType;
432  };
433 
437  QList< QgsVectorDataProvider::NativeType > nativeTypes() const;
438 
443  virtual bool doesStrictFeatureTypeCheck() const { return true; }
444 
446  static QStringList availableEncodings();
447 
451  bool hasErrors() const;
452 
456  void clearErrors();
457 
461  QStringList errors() const;
462 
467  virtual bool isSaveAndLoadStyleToDatabaseSupported() const;
468 
473  virtual bool isDeleteStyleFromDatabaseSupported() const;
474 
475  static QVariant convertValue( QVariant::Type type, const QString &value );
476 
480  virtual QgsTransaction *transaction() const;
481 
489  virtual void forceReload();
490 
494  virtual QSet<QgsMapLayerDependency> dependencies() const;
495 
503  virtual QList<QgsRelation> discoverRelations( const QgsVectorLayer *self, const QList<QgsVectorLayer *> &layers ) const;
504 
509  virtual QVariantMap metadata() const { return QVariantMap(); }
510 
516  virtual QString translateMetadataKey( const QString &mdKey ) const { return mdKey; }
517 
524  virtual QString translateMetadataValue( const QString &mdKey, const QVariant &value ) const { Q_UNUSED( mdKey ); return value.toString(); }
525 
526  signals:
527 
533  void raiseError( const QString &msg ) const;
534 
535  protected:
536 
541  void clearMinMaxCache();
542 
546  void fillMinMaxCache() const;
547 
557  void pushError( const QString &msg ) const;
558 
563  QgsGeometry *convertToProviderType( const QgsGeometry &geom ) const SIP_FACTORY;
564 
571  void setNativeTypes( const QList<QgsVectorDataProvider::NativeType> &nativeTypes );
572 
578  QTextCodec *textEncoding() const;
579 
580  private:
581  mutable bool mCacheMinMaxDirty;
582  mutable QMap<int, QVariant> mCacheMinValues, mCacheMaxValues;
583 
585  QTextCodec *mEncoding = nullptr;
586 
588  QgsAttributeList mAttributesToFetch;
589 
591  QList< NativeType > mNativeTypes;
592 
594  QMap<QString, QVariant::Type> mOldTypeList;
595 
597  mutable QStringList mErrors;
598 
599  static QStringList sEncodings;
600 
604  virtual void setTransaction( QgsTransaction * /*transaction*/ ) {}
605 
606 };
607 
608 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsVectorDataProvider::Capabilities )
609 
610 #endif
Wrapper for iterator of features from vector data provider or vector layer.
QMap< QgsFeatureId, QgsGeometry > QgsGeometryMap
Definition: qgsfeature.h:512
virtual QgsFields fields() const =0
Returns the fields associated with features in the source.
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:52
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
#define SIP_SKIP
Definition: qgis_sip.h:102
virtual bool addFeatures(QgsFeatureList &features)=0
Adds a list of features to the sink.
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)...
#define SIP_FACTORY
Definition: qgis_sip.h:57
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:39
This class represents a coordinate reference system (CRS).
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...