|
3 | 3 |
|
4 | 4 | #include <QFlags>
|
5 | 5 |
|
| 6 | +#include "qgsfeature.h" |
6 | 7 | #include "qgsrectangle.h"
|
7 | 8 |
|
8 | 9 | #include <QList>
|
9 | 10 | typedef QList<int> QgsAttributeList;
|
10 | 11 |
|
11 | 12 | /**
|
12 | 13 | * This class wraps a request for features to a vector layer (or directly its vector data provider).
|
| 14 | + * The request may apply a filter to fetch only a particular subset of features. Currently supported filters: |
| 15 | + * - no filter - all features are returned |
| 16 | + * - feature id - only feature that matches given feature id is returned |
| 17 | + * - rectangle - only features that intersect given rectangle should be fetched. For the sake of speed, |
| 18 | + * the intersection is often done only using feature's bounding box. There is a flag |
| 19 | + * ExactIntersect that makes sure that only intersecting features will be returned. |
| 20 | + * |
| 21 | + * For efficiency, it is also possible to tell provider that some data is not required: |
| 22 | + * - NoGeometry flag |
| 23 | + * - SubsetOfAttributes flag |
13 | 24 | *
|
14 | 25 | * The options may be chained, e.g.:
|
15 |
| - * QgsFeatureRequest().setExtent(QgsRectangle(0,0,1,1)).setFlags(QgsFeatureRequest::ExactIntersect) |
| 26 | + * QgsFeatureRequest().setFilterRect(QgsRectangle(0,0,1,1)).setFlags(QgsFeatureRequest::ExactIntersect) |
| 27 | + * |
| 28 | + * Examples: |
| 29 | + * - fetch all features: |
| 30 | + * QgsFeatureRequest() |
| 31 | + * - fetch all features, only one attribute |
| 32 | + * QgsFeatureRequest().setSubsetOfAttributes(QStringList("myfield"), provider->fieldMap()) |
| 33 | + * - fetch all features, without geometries |
| 34 | + * QgsFeatureRequest().setFlags(QgsFeatureRequest::NoGeometry) |
| 35 | + * - fetch only features from particular extent |
| 36 | + * QgsFeatureRequest().setFilterRect(QgsRectangle(0,0,1,1)) |
| 37 | + * - fetch only one feature |
| 38 | + * QgsFeatureRequest().setFilterFid(45) |
| 39 | + * |
16 | 40 | */
|
17 | 41 | class QgsFeatureRequest
|
18 | 42 | {
|
19 | 43 | public:
|
20 | 44 | enum Flag
|
21 | 45 | {
|
22 |
| - NoGeometry = 0x01, //!< Do not fetch geometry |
23 |
| - NoAttributes = 0x02, //!< Do not fetch any attributes |
24 |
| - ExactIntersect = 0x04 //!< Use exact geometry intersection (slower) instead of bounding boxes |
| 46 | + NoGeometry = 0x01, //!< Do not fetch geometry |
| 47 | + SubsetOfAttributes = 0x02, //!< Fetch only a subset of attributes (setSubsetOfAttributes sets this flag) |
| 48 | + ExactIntersect = 0x04 //!< Use exact geometry intersection (slower) instead of bounding boxes |
25 | 49 | };
|
26 | 50 | Q_DECLARE_FLAGS( Flags, Flag )
|
27 | 51 |
|
| 52 | + enum FilterType |
| 53 | + { |
| 54 | + FilterNone, //!< No filter is applied |
| 55 | + FilterRect, //!< Filter using a rectangle |
| 56 | + FilterFid //!< Filter using feature ID |
| 57 | + }; |
| 58 | + |
28 | 59 | //! construct a default request: for all features get attributes and geometries
|
29 | 60 | QgsFeatureRequest();
|
30 | 61 |
|
| 62 | + FilterType filterType() const { return mFilter; } |
| 63 | + |
31 | 64 | //! Set rectangle from which features will be taken. Empty rectangle removes the filter.
|
32 |
| - QgsFeatureRequest& setExtent( const QgsRectangle& rect ) { mRect = rect; return *this; } |
33 |
| - const QgsRectangle& extent() const { return mRect; } |
| 65 | + //! |
| 66 | + QgsFeatureRequest& setFilterRect( const QgsRectangle& rect ) { mFilter = FilterRect; mFilterRect = rect; return *this; } |
| 67 | + const QgsRectangle& filterRect() const { return mFilterRect; } |
| 68 | + |
| 69 | + //! Set feature ID that should be fetched. |
| 70 | + QgsFeatureRequest& setFilterFid( QgsFeatureId fid ) { mFilterFid = FilterFid; mFilterFid = fid; return *this; } |
| 71 | + const QgsFeatureId& filterFid() const { return mFilterFid; } |
34 | 72 |
|
35 | 73 | //! Set flags that affect how features will be fetched
|
36 | 74 | QgsFeatureRequest& setFlags( Flags flags ) { mFlags = flags; return *this; }
|
37 | 75 | const Flags& flags() const { return mFlags; }
|
38 | 76 |
|
39 | 77 | //! Set a subset of attributes that will be fetched. Empty list means that all attributes are used.
|
40 | 78 | //! To disable fetching attributes, reset the FetchAttributes flag (which is set by default)
|
41 |
| - QgsFeatureRequest& setAttributes( const QgsAttributeList& attrs ) { mAttrs = attrs; return *this; } |
42 |
| - const QgsAttributeList& attributes() const { return mAttrs; } |
| 79 | + QgsFeatureRequest& setSubsetOfAttributes( const QgsAttributeList& attrs ) { mFlags |= SubsetOfAttributes; mAttrs = attrs; return *this; } |
| 80 | + const QgsAttributeList& subsetOfAttributes() const { return mAttrs; } |
43 | 81 |
|
44 |
| - // TODO: maybe set attributes as a list of strings? |
| 82 | + //! Set a subset of attributes by names that will be fetched |
| 83 | + QgsFeatureRequest& setSubsetOfAttributes( const QStringList& attrNames, const QgsFieldMap& fields ); |
45 | 84 |
|
46 | 85 | // TODO: in future
|
47 |
| - // void setExpression(const QString& expression); |
| 86 | + // void setFilterExpression(const QString& expression); // using QgsExpression |
| 87 | + // void setFilterNativeExpression(con QString& expr); // using provider's SQL (if supported) |
48 | 88 | // void setLimit(int limit);
|
49 | 89 |
|
50 | 90 | protected:
|
51 |
| - QgsRectangle mRect; |
| 91 | + FilterType mFilter; |
| 92 | + QgsRectangle mFilterRect; |
| 93 | + QgsFeatureId mFilterFid; |
52 | 94 | Flags mFlags;
|
53 | 95 | QgsAttributeList mAttrs;
|
54 | 96 | };
|
|
0 commit comments