QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
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 
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:
120  OrderByClause( const QString &expression, bool ascending = true );
128  OrderByClause( const QString &expression, bool ascending, bool nullsfirst );
129 
134  QgsExpression expression() const;
135 
140  bool ascending() const;
141 
145  void setAscending( bool ascending );
146 
151  bool nullsFirst() const;
152 
156  void setNullsFirst( bool nullsFirst );
157 
161  QString dump() const;
162 
163  // friend inline int qHash(const OrderByClause &a) { return qHash(a.mExpression.expression()) ^ qHash(a.mAscending) ^ qHash( a.mNullsFirst); }
164 
165  private:
166  QgsExpression mExpression;
167  bool mAscending;
168  bool mNullsFirst;
169  };
170 
177  class OrderBy : public QList<OrderByClause>
178  {
179  public:
183  CORE_EXPORT OrderBy()
184  : QList<OrderByClause>()
185  {}
186 
190  CORE_EXPORT OrderBy( const QList<OrderByClause>& other );
191 
198  QList<OrderByClause> CORE_EXPORT list() const;
199 
203  void CORE_EXPORT save( QDomElement& elem ) const;
204 
208  void CORE_EXPORT load( const QDomElement& elem );
209 
213  QSet<QString> CORE_EXPORT usedAttributes() const;
214 
218  QString CORE_EXPORT dump() const;
219  };
220 
224  static const QString AllAttributes;
225 
229  explicit QgsFeatureRequest( QgsFeatureId fid );
231  explicit QgsFeatureRequest( const QgsRectangle& rect );
233  explicit QgsFeatureRequest( const QgsExpression& expr, const QgsExpressionContext& context = QgsExpressionContext() );
237  QgsFeatureRequest& operator=( const QgsFeatureRequest& rh );
238 
240 
246  FilterType filterType() const { if ( mFilter == FilterNone && !mFilterRect.isNull() ) return FilterRect; else return mFilter; }
247 
251  QgsFeatureRequest& setFilterRect( const QgsRectangle& rect );
252 
256  const QgsRectangle& filterRect() const { return mFilterRect; }
257 
259  QgsFeatureRequest& setFilterFid( QgsFeatureId fid );
261  QgsFeatureId filterFid() const { return mFilterFid; }
262 
264  QgsFeatureRequest& setFilterFids( const QgsFeatureIds& fids );
266  const QgsFeatureIds& filterFids() const { return mFilterFids; }
267 
273  QgsFeatureRequest& setFilterExpression( const QString& expression );
274 
279  QgsExpression* filterExpression() const { return mFilterExpression; }
280 
286  QgsFeatureRequest& combineFilterExpression( const QString& expression );
287 
293  QgsExpressionContext* expressionContext() { return &mExpressionContext; }
294 
300  QgsFeatureRequest& setExpressionContext( const QgsExpressionContext& context );
301 
310  QgsFeatureRequest& disableFilter() { mFilter = FilterNone; return *this; }
311 
323  QgsFeatureRequest& addOrderBy( const QString &expression, bool ascending = true );
333  QgsFeatureRequest& addOrderBy( const QString &expression, bool ascending, bool nullsfirst );
334 
340  OrderBy orderBy() const;
341 
347  QgsFeatureRequest& setOrderBy( const OrderBy& orderBy );
348 
354  QgsFeatureRequest& setLimit( long limit );
355 
360  long limit() const { return mLimit; }
361 
363  QgsFeatureRequest& setFlags( const QgsFeatureRequest::Flags& flags );
364  const Flags& flags() const { return mFlags; }
365 
368  QgsFeatureRequest& setSubsetOfAttributes( const QgsAttributeList& attrs );
373  QgsAttributeList subsetOfAttributes() const { return mAttrs; }
374 
376  QgsFeatureRequest& setSubsetOfAttributes( const QStringList& attrNames, const QgsFields& fields );
377 
380  QgsFeatureRequest& setSimplifyMethod( const QgsSimplifyMethod& simplifyMethod );
383  const QgsSimplifyMethod& simplifyMethod() const { return mSimplifyMethod; }
384 
394  bool acceptFeature( const QgsFeature& feature );
395 
396  // TODO: in future
397  // void setFilterNativeExpression(con QString& expr); // using provider's SQL (if supported)
398 
399  protected:
406  Flags mFlags;
409  long mLimit;
411 };
412 
413 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsFeatureRequest::Flags )
414 
415 
416 class QgsFeatureIterator;
418 
423 class CORE_EXPORT QgsAbstractFeatureSource
424 {
425  public:
426  virtual ~QgsAbstractFeatureSource();
427 
433  virtual QgsFeatureIterator getFeatures( const QgsFeatureRequest& request ) = 0;
434 
435  protected:
436  void iteratorOpened( QgsAbstractFeatureIterator* it );
437  void iteratorClosed( QgsAbstractFeatureIterator* it );
438 
440 
441  template<typename> friend class QgsAbstractFeatureIteratorFromSource;
442 };
443 
444 #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
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: qgsfield.h:252
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:187
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:31
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.