QGIS API Documentation  2.99.0-Master (6a61179)
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 <QFlags>
19 #include <QList>
20 
21 #include "qgsfeature.h"
22 #include "qgsrectangle.h"
23 #include "qgsexpression.h"
24 #include "qgsexpressioncontext.h"
25 #include "qgssimplifymethod.h"
26 
27 typedef QList<int> QgsAttributeList;
28 
63 class CORE_EXPORT QgsFeatureRequest
64 {
65  public:
66  enum Flag
67  {
68  NoFlags = 0,
69  NoGeometry = 1,
70  SubsetOfAttributes = 2,
71  ExactIntersect = 4
72  };
73  Q_DECLARE_FLAGS( Flags, Flag )
74 
75 
79  {
84  FilterFids
85  };
86 
109  class CORE_EXPORT OrderByClause
110  {
111  public:
112 
121  OrderByClause( const QString &expression, bool ascending = true );
122 
130  OrderByClause( const QString &expression, bool ascending, bool nullsfirst );
131 
136  QgsExpression expression() const;
137 
142  bool ascending() const;
143 
147  void setAscending( bool ascending );
148 
153  bool nullsFirst() const;
154 
158  void setNullsFirst( bool nullsFirst );
159 
163  QString dump() const;
164 
165  // friend inline int qHash(const OrderByClause &a) { return qHash(a.mExpression.expression()) ^ qHash(a.mAscending) ^ qHash( a.mNullsFirst); }
166 
167  private:
168  QgsExpression mExpression;
169  bool mAscending;
170  bool mNullsFirst;
171  };
172 
179  class OrderBy : public QList<OrderByClause>
180  {
181  public:
182 
186  CORE_EXPORT OrderBy()
187  : QList<OrderByClause>()
188  {}
189 
193  CORE_EXPORT OrderBy( const QList<OrderByClause>& other );
194 
201  QList<OrderByClause> CORE_EXPORT list() const;
202 
206  void CORE_EXPORT save( QDomElement& elem ) const;
207 
211  void CORE_EXPORT load( const QDomElement& elem );
212 
216  QSet<QString> CORE_EXPORT usedAttributes() const;
217 
221  QString CORE_EXPORT dump() const;
222  };
223 
227  static const QString AllAttributes;
228 
232  explicit QgsFeatureRequest( QgsFeatureId fid );
234  explicit QgsFeatureRequest( const QgsFeatureIds &fids );
236  explicit QgsFeatureRequest( const QgsRectangle& rect );
238  explicit QgsFeatureRequest( const QgsExpression& expr, const QgsExpressionContext& context = QgsExpressionContext() );
242  QgsFeatureRequest& operator=( const QgsFeatureRequest& rh );
243 
245 
251  FilterType filterType() const { if ( mFilter == FilterNone && !mFilterRect.isNull() ) return FilterRect; else return mFilter; }
252 
256  QgsFeatureRequest& setFilterRect( const QgsRectangle& rect );
257 
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; }
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  // TODO: in future
407  // void setFilterNativeExpression(con QString& expr); // using provider's SQL (if supported)
408 
409  protected:
416  Flags mFlags;
419  long mLimit;
421 };
422 
423 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsFeatureRequest::Flags )
424 
425 
426 class QgsFeatureIterator;
428 
433 class CORE_EXPORT QgsAbstractFeatureSource
434 {
435  public:
436  virtual ~QgsAbstractFeatureSource();
437 
443  virtual QgsFeatureIterator getFeatures( const QgsFeatureRequest& request ) = 0;
444 
445  protected:
446  void iteratorOpened( QgsAbstractFeatureIterator* it );
447  void iteratorClosed( QgsAbstractFeatureIterator* it );
448 
449  QSet< QgsAbstractFeatureIterator* > mActiveIterators;
450 
451  template<typename> friend class QgsAbstractFeatureIteratorFromSource;
452 };
453 
454 #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:35
long limit() const
Returns the maximum number of features to request, or -1 if no limit set.
Filter using feature ID.
QgsSimplifyMethod mSimplifyMethod
QgsExpression * mFilterExpression
const Flags & flags() const
QSet< QgsFeatureId > QgsFeatureIds
Definition: qgsfeature.h:355
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:36
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:135
const QgsRectangle & filterRect() const
Get the rectangle from which features will be taken.
QgsExpressionContext mExpressionContext
QSet< QgsAbstractFeatureIterator *> mActiveIterators
QgsExpression * filterExpression() const
Returns the filter expression if set.
QgsFeatureRequest & disableFilter()
Disables filter conditions.
Internal feature iterator to be implemented within data providers.
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 AllAttributes
A special attribute that if set matches all attributes.
Obsolete, will be ignored. If a filterRect is set it will be used anyway. Filter using a rectangle...
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.
QgsFeatureId mFilterFid
No filter is applied.
FilterType
Types of filters.
QgsAttributeList mAttrs
qint64 QgsFeatureId
Definition: qgsfeature.h:32
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.