QGIS API Documentation  3.18.1-Zürich (202f1bf7e5)
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_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 #include "qgsfeaturerequest.h"
37 
38 typedef QList<int> QgsAttributeList SIP_SKIP;
39 typedef QSet<int> QgsAttributeIds SIP_SKIP;
40 typedef QHash<int, QString> QgsAttrPalIndexNameHash;
41 
42 class QgsFeatureIterator;
43 class QgsTransaction;
44 class QgsFeedback;
45 class QgsFeatureRenderer;
47 
48 
58 class CORE_EXPORT QgsVectorDataProvider : public QgsDataProvider, public QgsFeatureSink, public QgsFeatureSource
59 {
60  Q_OBJECT
61 
62  friend class QgsTransaction;
64 
65  public:
66 
67  // If you add to this, please also add to capabilitiesString()
68 
73  {
74  NoCapabilities = 0,
75  AddFeatures = 1,
76  DeleteFeatures = 1 << 1,
77  ChangeAttributeValues = 1 << 2,
78  AddAttributes = 1 << 3,
79  DeleteAttributes = 1 << 4,
80  CreateSpatialIndex = 1 << 6,
81  SelectAtId = 1 << 7,
82  ChangeGeometries = 1 << 8,
83  SelectEncoding = 1 << 13,
84  CreateAttributeIndex = 1 << 12,
85  SimplifyGeometries = 1 << 14,
86  SimplifyGeometriesWithTopologicalValidation = 1 << 15,
87  TransactionSupport = 1 << 16,
88  CircularGeometries = 1 << 17,
89  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  CreateLabeling = 1 << 25,
97  ReloadData = 1 << 26,
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(),
127  QgsDataProvider::ReadFlags flags = QgsDataProvider::ReadFlags() );
128 
146 
150  virtual QString storageType() const;
151 
157  QgsFeatureIterator getFeatures( const QgsFeatureRequest &request = QgsFeatureRequest() ) const override = 0;
158 
162  QgsWkbTypes::Type wkbType() const override = 0;
163 
168  long featureCount() const override = 0;
169 
175  virtual bool empty() const;
176 
188 
192  QgsFields fields() const override = 0;
193 
194  QgsCoordinateReferenceSystem sourceCrs() const override;
195  QgsRectangle sourceExtent() const override;
196  QString sourceName() const override { return QString(); }
197 
202  virtual QString dataComment() const override;
203 
212  QVariant minimumValue( int index ) const override;
213 
222  QVariant maximumValue( int index ) const override;
223 
233  virtual QStringList uniqueStringsMatching( int index, const QString &substring, int limit = -1,
234  QgsFeedback *feedback = nullptr ) const;
235 
248  virtual QVariant aggregate( QgsAggregateCalculator::Aggregate aggregate,
249  int index,
251  QgsExpressionContext *context,
252  bool &ok,
253  QgsFeatureIds *fids = nullptr ) const;
254 
261  virtual void enumValues( int index, QStringList &enumList SIP_OUT ) const { Q_UNUSED( index ) enumList.clear(); }
262 
263  bool addFeatures( QgsFeatureList &flist SIP_INOUT, QgsFeatureSink::Flags flags = QgsFeatureSink::Flags() ) override;
264  QString lastError() const override;
265 
272  virtual bool deleteFeatures( const QgsFeatureIds &id );
273 
281  virtual bool truncate();
282 
289  virtual bool cancelReload();
290 
296  virtual bool addAttributes( const QList<QgsField> &attributes );
297 
305  virtual bool deleteAttributes( const QgsAttributeIds &attributes );
306 
315  virtual bool renameAttributes( const QgsFieldNameMap &renamedAttributes );
316 
325  virtual bool changeAttributeValues( const QgsChangedAttributesMap &attr_map );
326 
338  virtual bool changeFeatures( const QgsChangedAttributesMap &attr_map,
339  const QgsGeometryMap &geometry_map );
340 
352  virtual QVariant defaultValue( int fieldIndex ) const;
353 
361  virtual QString defaultValueClause( int fieldIndex ) const;
362 
369  QgsFieldConstraints::Constraints fieldConstraints( int fieldIndex ) const;
370 
378  virtual bool skipConstraintCheck( int fieldIndex, QgsFieldConstraints::Constraint constraint, const QVariant &value = QVariant() ) const;
379 
387  virtual bool changeGeometryValues( const QgsGeometryMap &geometry_map );
388 
393  virtual bool createSpatialIndex();
394 
396  virtual bool createAttributeIndex( int field );
397 
404  Q_INVOKABLE virtual QgsVectorDataProvider::Capabilities capabilities() const;
405 
409  QString capabilitiesString() const;
410 
423  virtual void setEncoding( const QString &e );
424 
430  QString encoding() const;
431 
435  int fieldNameIndex( const QString &fieldName ) const;
436 
440  QMap<QString, int> fieldNameMap() const;
441 
445  virtual QgsAttributeList attributeIndexes() const;
446 
450  virtual QgsAttributeList pkAttributeIndexes() const;
451 
455  virtual QgsAttrPalIndexNameHash palAttributeIndexNames() const;
456 
460  bool supportedType( const QgsField &field ) const;
461 
462  struct NativeType
463  {
464  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 )
465  : mTypeDesc( typeDesc )
466  , mTypeName( typeName )
467  , mType( type )
468  , mMinLen( minLen )
469  , mMaxLen( maxLen )
470  , mMinPrec( minPrec )
471  , mMaxPrec( maxPrec )
472  , mSubType( subType )
473  {}
474 
475  QString mTypeDesc;
476  QString mTypeName;
477  QVariant::Type mType;
478  int mMinLen;
479  int mMaxLen;
480  int mMinPrec;
481  int mMaxPrec;
482  QVariant::Type mSubType;
483  };
484 
488  QList< QgsVectorDataProvider::NativeType > nativeTypes() const;
489 
494  virtual bool doesStrictFeatureTypeCheck() const { return true; }
495 
497  static QStringList availableEncodings();
498 
502  bool hasErrors() const;
503 
507  void clearErrors();
508 
512  QStringList errors() const;
513 
518  virtual bool isSaveAndLoadStyleToDatabaseSupported() const;
519 
524  virtual bool isDeleteStyleFromDatabaseSupported() const;
525 
541  virtual QgsFeatureRenderer *createRenderer( const QVariantMap &configuration = QVariantMap() ) const SIP_FACTORY;
542 
558  virtual QgsAbstractVectorLayerLabeling *createLabeling( const QVariantMap &configuration = QVariantMap() ) const SIP_FACTORY;
559 
560  static QVariant convertValue( QVariant::Type type, const QString &value );
561 
565  virtual QgsTransaction *transaction() const;
566 
570  Q_DECL_DEPRECATED virtual void forceReload() SIP_DEPRECATED { reloadData(); }
571 
575  virtual QSet<QgsMapLayerDependency> dependencies() const;
576 
584  virtual QList<QgsRelation> discoverRelations( const QgsVectorLayer *self, const QList<QgsVectorLayer *> &layers ) const;
585 
590  virtual QVariantMap metadata() const { return QVariantMap(); }
591 
597  virtual QString translateMetadataKey( const QString &mdKey ) const { return mdKey; }
598 
605  virtual QString translateMetadataValue( const QString &mdKey, const QVariant &value ) const { Q_UNUSED( mdKey ) return value.toString(); }
606 
614  virtual bool hasMetadata() const { return true; }
615 
622  virtual void handlePostCloneOperations( QgsVectorDataProvider *source );
623 
626 
627  signals:
628 
634  void raiseError( const QString &msg ) const;
635 
636  protected:
637 
642  void clearMinMaxCache();
643 
647  void fillMinMaxCache() const;
648 
658  void pushError( const QString &msg ) const;
659 
664  QgsGeometry convertToProviderType( const QgsGeometry &geom ) const;
665 
672  void setNativeTypes( const QList<QgsVectorDataProvider::NativeType> &nativeTypes );
673 
679  QTextCodec *textEncoding() const;
680 
681  private:
682  mutable bool mCacheMinMaxDirty = true;
683  mutable QMap<int, QVariant> mCacheMinValues, mCacheMaxValues;
684 
686  QTextCodec *mEncoding = nullptr;
687 
689  QgsAttributeList mAttributesToFetch;
690 
692  QList< NativeType > mNativeTypes;
693 
695  mutable QStringList mErrors;
696 
697  std::unique_ptr< QgsVectorDataProviderTemporalCapabilities > mTemporalCapabilities;
698 
699  static QStringList sEncodings;
700 
704  virtual void setTransaction( QgsTransaction * /*transaction*/ ) {}
705 };
706 
707 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsVectorDataProvider::Capabilities )
708 
709 #endif
Base class that can be used for any class that is capable of returning features.
Abstract base class - its implementations define different approaches to the labeling of a vector lay...
Aggregate
Available aggregates to calculate.
This class represents a coordinate reference system (CRS).
Abstract base class for spatial data provider implementations.
virtual QString dataComment() const
Returns a short comment for the data that this provider is providing access to (e....
virtual QgsDataProviderTemporalCapabilities * temporalCapabilities()
Returns the provider's temporal capabilities.
virtual void reloadData()
Reloads the data from the source for providers with data caches to synchronize, changes in the data s...
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Wrapper for iterator of features from vector data provider or vector layer.
This class wraps a request for features to a vector layer (or directly its vector data provider).
An interface for objects which accept features via addFeature(s) methods.
virtual QString lastError() const
Returns the most recent error encountered by the sink, e.g.
An interface for objects which provide features via a getFeatures method.
virtual QgsCoordinateReferenceSystem sourceCrs() const =0
Returns the coordinate reference system for features in the source.
virtual FeatureAvailability hasFeatures() const
Determines if there are any features available in the source.
FeatureAvailability
Possible return value for hasFeatures() to determine if a source is empty.
virtual QVariant minimumValue(int fieldIndex) const
Returns the minimum value for an attribute column or an invalid variant in case of error.
virtual QVariant maximumValue(int fieldIndex) const
Returns the maximum value for an attribute column or an invalid variant in case of error.
virtual QgsRectangle sourceExtent() const
Returns the extent of all geometries from the source.
Base class for feedback objects to be used for cancellation of something running in a worker thread.
Definition: qgsfeedback.h:45
Constraint
Constraints which may be present on a field.
Encapsulate a field in an attribute table or data source.
Definition: qgsfield.h:51
Container of fields for a vector layer.
Definition: qgsfields.h:45
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:124
A rectangle specified with double values.
Definition: qgsrectangle.h:42
This class allows including a set of layers in a database-side transaction, provided the layer data p...
Implementation of data provider temporal properties for QgsVectorDataProviders.
This is the base class for vector data providers.
virtual void enumValues(int index, QStringList &enumList) const
Returns the possible enum values of an attribute.
Capability
enumeration with capabilities that providers might implement
QString sourceName() const override
Returns a friendly display name for the source.
QgsWkbTypes::Type wkbType() const override=0
Returns the geometry type which is returned by this layer.
virtual QgsAbstractFeatureSource * featureSource() const =0
Returns feature source object that can be used for querying provider's data.
virtual QString translateMetadataValue(const QString &mdKey, const QVariant &value) const
Gets the translated metadata value.
virtual bool doesStrictFeatureTypeCheck() const
Returns true if the provider is strict about the type of inserted features (e.g.
virtual bool hasMetadata() const
Returns true if the data source has metadata, false otherwise.
long featureCount() const override=0
Number of features in the layer.
QgsFields fields() const override=0
Returns the fields associated with this data provider.
QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest()) const override=0
Query the provider for features specified in request.
virtual QString translateMetadataKey(const QString &mdKey) const
Gets the translated metadata key.
virtual QVariantMap metadata() const
Gets metadata, dependent on the provider type, that will be display in the metadata tab of the layer ...
FeatureCountState
Enumeration of feature count states.
virtual bool deleteFeatures(const QgsFeatureIds &fid)
Deletes a set of features from the layer (but does not commit it)
virtual bool addFeatures(QgsFeatureList &features)
Insert a copy of the given features into the layer (but does not commit it)
virtual bool changeAttributeValues(QgsFeatureId fid, const QgsAttributeMap &newValues, const QgsAttributeMap &oldValues)
Changes values of attributes (but does not commit it).
Represents a vector layer which manages a vector based data sets.
Type
The WKB type describes the number of dimensions a geometry has.
Definition: qgswkbtypes.h:70
std::unique_ptr< GEOSGeometry, GeosDeleter > unique_ptr
Scoped GEOS pointer.
Definition: qgsgeos.h:79
#define SIP_DEPRECATED
Definition: qgis_sip.h:106
#define SIP_SKIP
Definition: qgis_sip.h:126
#define SIP_OUT
Definition: qgis_sip.h:58
#define SIP_FACTORY
Definition: qgis_sip.h:76
#define SIP_INOUT
Definition: qgis_sip.h:71
QMap< int, QString > QgsFieldNameMap
Definition: qgsattributes.h:44
QMap< QgsFeatureId, QgsGeometry > QgsGeometryMap
Definition: qgsfeature.h:609
QMap< QgsFeatureId, QgsAttributeMap > QgsChangedAttributesMap
Definition: qgsfeature.h:600
QList< QgsFeature > QgsFeatureList
Definition: qgsfeature.h:614
QSet< QgsFeatureId > QgsFeatureIds
Definition: qgsfeatureid.h:37
QList< int > QgsAttributeList
Definition: qgsfield.h:26
const QgsField & field
Definition: qgsfield.h:472
Q_DECLARE_OPERATORS_FOR_FLAGS(QgsField::ConfigurationFlags) CORE_EXPORT QDataStream &operator<<(QDataStream &out
Writes the field to stream out. QGIS version compatibility is not guaranteed.
QSet< int > QgsAttributeIds
QHash< int, QString > QgsAttrPalIndexNameHash
const QString & typeName
const QgsAttributeList & attributeIndexes
A bundle of parameters controlling aggregate calculation.
Setting options for creating vector data providers.
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)