QGIS API Documentation  2.99.0-Master (40f86b2)
qgsfeaturerequest.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgsfeaturerequest.h
3  ---------------------
4  begin : Mai 2012
5  copyright : (C) 2012 by Martin Dobias
6  email : wonder dot sk at gmail dot com
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 QGSFEATUREREQUEST_H
16 #define QGSFEATUREREQUEST_H
17 
18 #include "qgis_core.h"
19 #include <QFlags>
20 #include <QList>
21 #include <memory>
22 
23 #include "qgsfeature.h"
24 #include "qgsrectangle.h"
25 #include "qgsexpression.h"
26 #include "qgsexpressioncontext.h"
27 #include "qgssimplifymethod.h"
28 
29 typedef QList<int> QgsAttributeList;
30 
65 class CORE_EXPORT QgsFeatureRequest
66 {
67  public:
68  enum Flag
69  {
70  NoFlags = 0,
71  NoGeometry = 1,
72  SubsetOfAttributes = 2,
73  ExactIntersect = 4
74  };
75  Q_DECLARE_FLAGS( Flags, Flag )
76 
77 
81  {
85  FilterFids
86  };
87 
110  class CORE_EXPORT OrderByClause
111  {
112  public:
113 
122  OrderByClause( const QString &expression, bool ascending = true );
123 
131  OrderByClause( const QString &expression, bool ascending, bool nullsfirst );
132 
137  QgsExpression expression() const;
138 
143  bool ascending() const;
144 
148  void setAscending( bool ascending );
149 
154  bool nullsFirst() const;
155 
159  void setNullsFirst( bool nullsFirst );
160 
164  QString dump() const;
165 
166  // friend inline int qHash(const OrderByClause &a) { return qHash(a.mExpression.expression()) ^ qHash(a.mAscending) ^ qHash( a.mNullsFirst); }
167 
168  private:
169  QgsExpression mExpression;
170  bool mAscending;
171  bool mNullsFirst;
172  };
173 
180  class OrderBy : public QList<OrderByClause>
181  {
182  public:
183 
187  CORE_EXPORT OrderBy()
188  : QList<OrderByClause>()
189  {}
190 
194  CORE_EXPORT OrderBy( const QList<OrderByClause> &other );
195 
202  QList<OrderByClause> CORE_EXPORT list() const;
203 
207  void CORE_EXPORT save( QDomElement &elem ) const;
208 
212  void CORE_EXPORT load( const QDomElement &elem );
213 
217  QSet<QString> CORE_EXPORT usedAttributes() const;
218 
222  QString CORE_EXPORT dump() const;
223  };
224 
228  static const QString ALL_ATTRIBUTES;
229 
233  explicit QgsFeatureRequest( QgsFeatureId fid );
235  explicit QgsFeatureRequest( const QgsFeatureIds &fids );
237  explicit QgsFeatureRequest( const QgsRectangle &rect );
239  explicit QgsFeatureRequest( const QgsExpression &expr, const QgsExpressionContext &context = QgsExpressionContext() );
243  QgsFeatureRequest &operator=( const QgsFeatureRequest &rh );
244 
250  FilterType filterType() const { return mFilter; }
251 
255  QgsFeatureRequest &setFilterRect( const QgsRectangle &rect );
256 
261  const QgsRectangle &filterRect() const { return mFilterRect; }
262 
264  QgsFeatureRequest &setFilterFid( QgsFeatureId fid );
266  QgsFeatureId filterFid() const { return mFilterFid; }
267 
269  QgsFeatureRequest &setFilterFids( const QgsFeatureIds &fids );
271  const QgsFeatureIds &filterFids() const { return mFilterFids; }
272 
278  QgsFeatureRequest &setFilterExpression( const QString &expression );
279 
284  QgsExpression *filterExpression() const { return mFilterExpression.get(); }
285 
291  QgsFeatureRequest &combineFilterExpression( const QString &expression );
292 
298  QgsExpressionContext *expressionContext() { return &mExpressionContext; }
299 
305  QgsFeatureRequest &setExpressionContext( const QgsExpressionContext &context );
306 
315  QgsFeatureRequest &disableFilter() { mFilter = FilterNone; return *this; }
316 
328  QgsFeatureRequest &addOrderBy( const QString &expression, bool ascending = true );
329 
339  QgsFeatureRequest &addOrderBy( const QString &expression, bool ascending, bool nullsfirst );
340 
346  OrderBy orderBy() const;
347 
353  QgsFeatureRequest &setOrderBy( const OrderBy &orderBy );
354 
360  QgsFeatureRequest &setLimit( long limit );
361 
366  long limit() const { return mLimit; }
367 
369  QgsFeatureRequest &setFlags( QgsFeatureRequest::Flags flags );
370  const Flags &flags() const { return mFlags; }
371 
374  QgsFeatureRequest &setSubsetOfAttributes( const QgsAttributeList &attrs );
375 
380  QgsAttributeList subsetOfAttributes() const { return mAttrs; }
381 
383  QgsFeatureRequest &setSubsetOfAttributes( const QStringList &attrNames, const QgsFields &fields );
384 
386  QgsFeatureRequest &setSubsetOfAttributes( const QSet<QString> &attrNames, const QgsFields &fields );
387 
390  QgsFeatureRequest &setSimplifyMethod( const QgsSimplifyMethod &simplifyMethod );
393  const QgsSimplifyMethod &simplifyMethod() const { return mSimplifyMethod; }
394 
404  bool acceptFeature( const QgsFeature &feature );
405 
406  protected:
407  FilterType mFilter = FilterNone;
409  QgsFeatureId mFilterFid = -1;
411  std::unique_ptr< QgsExpression > mFilterExpression;
413  Flags mFlags;
416  long mLimit = -1;
418 };
419 
420 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsFeatureRequest::Flags )
421 
422 
423 class QgsFeatureIterator;
425 
430 class CORE_EXPORT QgsAbstractFeatureSource
431 {
432  public:
433  virtual ~QgsAbstractFeatureSource();
434 
440  virtual QgsFeatureIterator getFeatures( const QgsFeatureRequest &request ) = 0;
441 
442  protected:
443  void iteratorOpened( QgsAbstractFeatureIterator *it );
444  void iteratorClosed( QgsAbstractFeatureIterator *it );
445 
446  QSet< QgsAbstractFeatureIterator * > mActiveIterators;
447 
448  template<typename> friend class QgsAbstractFeatureIteratorFromSource;
449 };
450 
451 #endif // QGSFEATUREREQUEST_H
Class for parsing and evaluation of expressions (formerly called "search strings").
QgsFeatureIds mFilterFids
Wrapper for iterator of features from vector data provider or vector layer.
A rectangle specified with double values.
Definition: qgsrectangle.h:36
long limit() const
Returns the maximum number of features to request, or -1 if no limit set.
Filter using feature ID.
QgsSimplifyMethod mSimplifyMethod
const Flags & flags() const
QSet< QgsFeatureId > QgsFeatureIds
Definition: qgsfeature.h:358
QgsExpressionContext * expressionContext()
Returns the expression context used to evaluate filter expressions.
QgsFeatureId filterFid() const
Get the feature ID that should be fetched.
FilterType filterType() const
Return the filter type which is currently set on this request.
Container of fields for a vector layer.
Definition: qgsfields.h:39
const QgsFeatureIds & filterFids() const
Get feature IDs that should be fetched.
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:136
const QgsRectangle & filterRect() const
Get the rectangle from which features will be taken.
QgsExpressionContext mExpressionContext
QgsExpression * filterExpression() const
Returns the filter expression if set.
QgsFeatureRequest & disableFilter()
Disables filter conditions.
std::unique_ptr< QgsExpression > mFilterExpression
Internal feature iterator to be implemented within data providers.
QSet< QgsAbstractFeatureIterator *> mActiveIterators
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
This class wraps a request for features to a vector layer (or directly its vector data provider)...
QList< int > QgsAttributeList
static const QString ALL_ATTRIBUTES
A special attribute that if set matches all attributes.
QgsRectangle mFilterRect
Base class that can be used for any class that is capable of returning features.
QgsAttributeList subsetOfAttributes() const
Return the subset of attributes which at least need to be fetched.
The OrderByClause class represents an order by clause for a QgsFeatureRequest.
No filter is applied.
FilterType
Types of filters.
QgsAttributeList mAttrs
qint64 QgsFeatureId
Definition: qgsfeature.h:33
This class contains information about how to simplify geometries fetched from a QgsFeatureIterator.
const QgsSimplifyMethod & simplifyMethod() const
Get simplification method for geometries that will be fetched.
CORE_EXPORT OrderBy()
Create a new empty order by.
Represents a list of OrderByClauses, with the most important first and the least important last...
Helper template that cares of two things: 1.