QGIS API Documentation  3.8.0-Zanzibar (11aff65)
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 : marco.hugentobler@autoform.ch
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 
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;
45 
46 #include "qgsfeaturerequest.h"
47 
57 class CORE_EXPORT QgsVectorDataProvider : public QgsDataProvider, public QgsFeatureSink, public QgsFeatureSource
58 {
59  Q_OBJECT
60 
61  friend class QgsTransaction;
63 
64  public:
65 
66  // If you add to this, please also add to capabilitiesString()
67 
72  {
73  NoCapabilities = 0,
74  AddFeatures = 1,
75  DeleteFeatures = 1 << 1,
76  ChangeAttributeValues = 1 << 2,
77  AddAttributes = 1 << 3,
78  DeleteAttributes = 1 << 4,
79  CreateSpatialIndex = 1 << 6,
80  SelectAtId = 1 << 7,
81  ChangeGeometries = 1 << 8,
82  SelectEncoding = 1 << 13,
83  CreateAttributeIndex = 1 << 12,
84  SimplifyGeometries = 1 << 14,
85  SimplifyGeometriesWithTopologicalValidation = 1 << 15,
86  TransactionSupport = 1 << 16,
87  CircularGeometries = 1 << 17,
88  ChangeFeatures = 1 << 18,
91  RenameAttributes = 1 << 19,
92  FastTruncate = 1 << 20,
93  ReadLayerMetadata = 1 << 21,
94  WriteLayerMetadata = 1 << 22,
95  CancelSupport = 1 << 23,
96  CreateRenderer = 1 << 24,
97  CreateLabeling = 1 << 25,
98  };
99 
100  Q_DECLARE_FLAGS( Capabilities, Capability )
101 
102 
103  static const int EditingCapabilities = AddFeatures | DeleteFeatures |
104  ChangeAttributeValues | ChangeGeometries | AddAttributes | DeleteAttributes |
105  RenameAttributes;
106 
111  {
113  Uncounted = -2,
115  UnknownCount = -1,
116  };
117 
125  QgsVectorDataProvider( const QString &uri = QString(), const QgsDataProvider::ProviderOptions &providerOptions = QgsDataProvider::ProviderOptions() );
126 
143  virtual QgsAbstractFeatureSource *featureSource() const = 0 SIP_FACTORY;
144 
148  virtual QString storageType() const;
149 
155  QgsFeatureIterator getFeatures( const QgsFeatureRequest &request = QgsFeatureRequest() ) const override = 0;
156 
160  QgsWkbTypes::Type wkbType() const override = 0;
161 
166  long featureCount() const override = 0;
167 
173  virtual bool empty() const;
174 
186 
190  QgsFields fields() const override = 0;
191 
192  QgsCoordinateReferenceSystem sourceCrs() const override;
193  QgsRectangle sourceExtent() const override;
194  QString sourceName() const override { return QString(); }
195 
200  virtual QString dataComment() const;
201 
210  QVariant minimumValue( int index ) const override;
211 
220  QVariant maximumValue( int index ) const override;
221 
231  virtual QStringList uniqueStringsMatching( int index, const QString &substring, int limit = -1,
232  QgsFeedback *feedback = nullptr ) const;
233 
246  virtual QVariant aggregate( QgsAggregateCalculator::Aggregate aggregate,
247  int index,
249  QgsExpressionContext *context,
250  bool &ok,
251  QgsFeatureIds *fids = nullptr ) const;
252 
259  virtual void enumValues( int index, QStringList &enumList SIP_OUT ) const { Q_UNUSED( index ) enumList.clear(); }
260 
261  bool addFeatures( QgsFeatureList &flist SIP_INOUT, QgsFeatureSink::Flags flags = nullptr ) override;
262 
269  virtual bool deleteFeatures( const QgsFeatureIds &id );
270 
278  virtual bool truncate();
279 
286  virtual bool cancelReload();
287 
293  virtual bool addAttributes( const QList<QgsField> &attributes );
294 
302  virtual bool deleteAttributes( const QgsAttributeIds &attributes );
303 
312  virtual bool renameAttributes( const QgsFieldNameMap &renamedAttributes );
313 
320  virtual bool changeAttributeValues( const QgsChangedAttributesMap &attr_map );
321 
333  virtual bool changeFeatures( const QgsChangedAttributesMap &attr_map,
334  const QgsGeometryMap &geometry_map );
335 
347  virtual QVariant defaultValue( int fieldIndex ) const;
348 
356  virtual QString defaultValueClause( int fieldIndex ) const;
357 
364  QgsFieldConstraints::Constraints fieldConstraints( int fieldIndex ) const;
365 
373  virtual bool skipConstraintCheck( int fieldIndex, QgsFieldConstraints::Constraint constraint, const QVariant &value = QVariant() ) const;
374 
382  virtual bool changeGeometryValues( const QgsGeometryMap &geometry_map );
383 
388  virtual bool createSpatialIndex();
389 
391  virtual bool createAttributeIndex( int field );
392 
399  virtual QgsVectorDataProvider::Capabilities capabilities() const;
400 
404  QString capabilitiesString() const;
405 
409  virtual void setEncoding( const QString &e );
410 
414  QString encoding() const;
415 
419  int fieldNameIndex( const QString &fieldName ) const;
420 
424  QMap<QString, int> fieldNameMap() const;
425 
429  virtual QgsAttributeList attributeIndexes() const;
430 
434  virtual QgsAttributeList pkAttributeIndexes() const;
435 
439  virtual QgsAttrPalIndexNameHash palAttributeIndexNames() const;
440 
444  bool supportedType( const QgsField &field ) const;
445 
446  struct NativeType
447  {
448  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 )
449  : mTypeDesc( typeDesc )
450  , mTypeName( typeName )
451  , mType( type )
452  , mMinLen( minLen )
453  , mMaxLen( maxLen )
454  , mMinPrec( minPrec )
455  , mMaxPrec( maxPrec )
456  , mSubType( subType )
457  {}
458 
459  QString mTypeDesc;
460  QString mTypeName;
461  QVariant::Type mType;
462  int mMinLen;
463  int mMaxLen;
464  int mMinPrec;
465  int mMaxPrec;
466  QVariant::Type mSubType;
467  };
468 
472  QList< QgsVectorDataProvider::NativeType > nativeTypes() const;
473 
478  virtual bool doesStrictFeatureTypeCheck() const { return true; }
479 
481  static QStringList availableEncodings();
482 
486  bool hasErrors() const;
487 
491  void clearErrors();
492 
496  QStringList errors() const;
497 
502  virtual bool isSaveAndLoadStyleToDatabaseSupported() const;
503 
508  virtual bool isDeleteStyleFromDatabaseSupported() const;
509 
525  virtual QgsFeatureRenderer *createRenderer( const QVariantMap &configuration = QVariantMap() ) const SIP_FACTORY;
526 
542  virtual QgsAbstractVectorLayerLabeling *createLabeling( const QVariantMap &configuration = QVariantMap() ) const SIP_FACTORY;
543 
544  static QVariant convertValue( QVariant::Type type, const QString &value );
545 
549  virtual QgsTransaction *transaction() const;
550 
558  virtual void forceReload();
559 
563  virtual QSet<QgsMapLayerDependency> dependencies() const;
564 
572  virtual QList<QgsRelation> discoverRelations( const QgsVectorLayer *self, const QList<QgsVectorLayer *> &layers ) const;
573 
578  virtual QVariantMap metadata() const { return QVariantMap(); }
579 
585  virtual QString translateMetadataKey( const QString &mdKey ) const { return mdKey; }
586 
593  virtual QString translateMetadataValue( const QString &mdKey, const QVariant &value ) const { Q_UNUSED( mdKey ) return value.toString(); }
594 
602  virtual bool hasMetadata() const { return true; }
603 
604  signals:
605 
611  void raiseError( const QString &msg ) const;
612 
613  protected:
614 
619  void clearMinMaxCache();
620 
624  void fillMinMaxCache() const;
625 
635  void pushError( const QString &msg ) const;
636 
641  QgsGeometry convertToProviderType( const QgsGeometry &geom ) const;
642 
649  void setNativeTypes( const QList<QgsVectorDataProvider::NativeType> &nativeTypes );
650 
656  QTextCodec *textEncoding() const;
657 
658  private:
659  mutable bool mCacheMinMaxDirty = true;
660  mutable QMap<int, QVariant> mCacheMinValues, mCacheMaxValues;
661 
663  QTextCodec *mEncoding = nullptr;
664 
666  QgsAttributeList mAttributesToFetch;
667 
669  QList< NativeType > mNativeTypes;
670 
672  mutable QStringList mErrors;
673 
674  static QStringList sEncodings;
675 
679  virtual void setTransaction( QgsTransaction * /*transaction*/ ) {}
680 
681 };
682 
683 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsVectorDataProvider::Capabilities )
684 
685 #endif
Wrapper for iterator of features from vector data provider or vector layer.
QMap< QgsFeatureId, QgsGeometry > QgsGeometryMap
Definition: qgsfeature.h:566
virtual QgsRectangle sourceExtent() const
Returns the extent of all geometries from the source.
A rectangle specified with double values.
Definition: qgsrectangle.h:41
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:111
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.
const QgsAttributeList & attributeIndexes
Base class for feedback objects to be used for cancellation 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:68
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
const QString & typeName
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:44
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:557
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).
Abstract base class - its implementations define different approaches to the labeling of a vector lay...
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.
virtual FeatureAvailability hasFeatures() const
Determines if there are any features available in the source.
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 ...