QGIS API Documentation  3.2.0-Bonn (bc43194)
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 
169  QgsFields fields() const override = 0;
170 
171  QgsCoordinateReferenceSystem sourceCrs() const override;
172  QgsRectangle sourceExtent() const override;
173  QString sourceName() const override { return QString(); }
174 
179  virtual QString dataComment() const;
180 
189  QVariant minimumValue( int index ) const override;
190 
199  QVariant maximumValue( int index ) const override;
200 
210  virtual QStringList uniqueStringsMatching( int index, const QString &substring, int limit = -1,
211  QgsFeedback *feedback = nullptr ) const;
212 
224  virtual QVariant aggregate( QgsAggregateCalculator::Aggregate aggregate,
225  int index,
227  QgsExpressionContext *context,
228  bool &ok ) const;
229 
236  virtual void enumValues( int index, QStringList &enumList SIP_OUT ) const { Q_UNUSED( index ); enumList.clear(); }
237 
238  bool addFeatures( QgsFeatureList &flist SIP_INOUT, QgsFeatureSink::Flags flags = nullptr ) override;
239 
246  virtual bool deleteFeatures( const QgsFeatureIds &id );
247 
255  virtual bool truncate();
256 
263  virtual bool cancelReload();
264 
270  virtual bool addAttributes( const QList<QgsField> &attributes );
271 
279  virtual bool deleteAttributes( const QgsAttributeIds &attributes );
280 
289  virtual bool renameAttributes( const QgsFieldNameMap &renamedAttributes );
290 
297  virtual bool changeAttributeValues( const QgsChangedAttributesMap &attr_map );
298 
310  virtual bool changeFeatures( const QgsChangedAttributesMap &attr_map,
311  const QgsGeometryMap &geometry_map );
312 
324  virtual QVariant defaultValue( int fieldIndex ) const;
325 
333  virtual QString defaultValueClause( int fieldIndex ) const;
334 
341  QgsFieldConstraints::Constraints fieldConstraints( int fieldIndex ) const;
342 
350  virtual bool skipConstraintCheck( int fieldIndex, QgsFieldConstraints::Constraint constraint, const QVariant &value = QVariant() ) const;
351 
359  virtual bool changeGeometryValues( const QgsGeometryMap &geometry_map );
360 
365  virtual bool createSpatialIndex();
366 
368  virtual bool createAttributeIndex( int field );
369 
376  virtual QgsVectorDataProvider::Capabilities capabilities() const;
377 
381  QString capabilitiesString() const;
382 
386  virtual void setEncoding( const QString &e );
387 
391  QString encoding() const;
392 
396  int fieldNameIndex( const QString &fieldName ) const;
397 
401  QMap<QString, int> fieldNameMap() const;
402 
406  virtual QgsAttributeList attributeIndexes() const;
407 
411  virtual QgsAttributeList pkAttributeIndexes() const;
412 
416  virtual QgsAttrPalIndexNameHash palAttributeIndexNames() const;
417 
421  bool supportedType( const QgsField &field ) const;
422 
423  struct NativeType
424  {
425  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 )
426  : mTypeDesc( typeDesc )
427  , mTypeName( typeName )
428  , mType( type )
429  , mMinLen( minLen )
430  , mMaxLen( maxLen )
431  , mMinPrec( minPrec )
432  , mMaxPrec( maxPrec )
433  , mSubType( subType )
434  {}
435 
436  QString mTypeDesc;
437  QString mTypeName;
438  QVariant::Type mType;
439  int mMinLen;
440  int mMaxLen;
441  int mMinPrec;
442  int mMaxPrec;
443  QVariant::Type mSubType;
444  };
445 
449  QList< QgsVectorDataProvider::NativeType > nativeTypes() const;
450 
455  virtual bool doesStrictFeatureTypeCheck() const { return true; }
456 
458  static QStringList availableEncodings();
459 
463  bool hasErrors() const;
464 
468  void clearErrors();
469 
473  QStringList errors() const;
474 
479  virtual bool isSaveAndLoadStyleToDatabaseSupported() const;
480 
485  virtual bool isDeleteStyleFromDatabaseSupported() const;
486 
502  virtual QgsFeatureRenderer *createRenderer( const QVariantMap &configuration = QVariantMap() ) const;
503 
504  static QVariant convertValue( QVariant::Type type, const QString &value );
505 
509  virtual QgsTransaction *transaction() const;
510 
518  virtual void forceReload();
519 
523  virtual QSet<QgsMapLayerDependency> dependencies() const;
524 
532  virtual QList<QgsRelation> discoverRelations( const QgsVectorLayer *self, const QList<QgsVectorLayer *> &layers ) const;
533 
538  virtual QVariantMap metadata() const { return QVariantMap(); }
539 
545  virtual QString translateMetadataKey( const QString &mdKey ) const { return mdKey; }
546 
553  virtual QString translateMetadataValue( const QString &mdKey, const QVariant &value ) const { Q_UNUSED( mdKey ); return value.toString(); }
554 
562  virtual bool hasMetadata() const { return true; }
563 
564  signals:
565 
571  void raiseError( const QString &msg ) const;
572 
573  protected:
574 
579  void clearMinMaxCache();
580 
584  void fillMinMaxCache() const;
585 
595  void pushError( const QString &msg ) const;
596 
601  QgsGeometry convertToProviderType( const QgsGeometry &geom ) const;
602 
609  void setNativeTypes( const QList<QgsVectorDataProvider::NativeType> &nativeTypes );
610 
616  QTextCodec *textEncoding() const;
617 
618  private:
619  mutable bool mCacheMinMaxDirty = true;
620  mutable QMap<int, QVariant> mCacheMinValues, mCacheMaxValues;
621 
623  QTextCodec *mEncoding = nullptr;
624 
626  QgsAttributeList mAttributesToFetch;
627 
629  QList< NativeType > mNativeTypes;
630 
632  mutable QStringList mErrors;
633 
634  static QStringList sEncodings;
635 
639  virtual void setTransaction( QgsTransaction * /*transaction*/ ) {}
640 
641 };
642 
643 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsVectorDataProvider::Capabilities )
644 
645 #endif
Wrapper for iterator of features from vector data provider or vector layer.
QMap< QgsFeatureId, QgsGeometry > QgsGeometryMap
Definition: qgsfeature.h:537
virtual QgsRectangle sourceExtent() const
Returns the extent of all geometries from the source.
A rectangle specified with double values.
Definition: qgsrectangle.h:40
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:544
QList< QgsFeature > QgsFeatureList
Definition: qgsfeature.h:549
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:104
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
Gets the translated metadata value.
Base class for feedback objects to be used for cancelation of something running in a worker thread...
Definition: qgsfeedback.h:44
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:67
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
Gets 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: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:45
virtual QgsCoordinateReferenceSystem sourceCrs() const =0
Returns the coordinate reference system for features in the source.
Setting options for creating vector data providers.
QMap< QgsFeatureId, QgsAttributeMap > QgsChangedAttributesMap
Definition: qgsfeature.h:528
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
virtual bool hasMetadata() const
Returns true if the data source has metadata, false otherwise.
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
Gets metadata, dependent on the provider type, that will be display in the metadata tab of the layer ...