QGIS API Documentation  2.99.0-Master (0a63d1f)
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 
22 #include "qgsfeature.h"
23 #include "qgsrectangle.h"
24 #include "qgsexpression.h"
25 #include "qgsexpressioncontext.h"
26 #include "qgssimplifymethod.h"
27 
28 typedef QList<int> QgsAttributeList;
29 
64 class CORE_EXPORT QgsFeatureRequest
65 {
66  public:
67  enum Flag
68  {
69  NoFlags = 0,
70  NoGeometry = 1,
71  SubsetOfAttributes = 2,
72  ExactIntersect = 4
73  };
74  Q_DECLARE_FLAGS( Flags, Flag )
75 
76 
80  {
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 
246 
252  FilterType filterType() const { if ( mFilter == FilterNone && !mFilterRect.isNull() ) return FilterRect; else return mFilter; }
253 
257  QgsFeatureRequest& setFilterRect( const QgsRectangle& rect );
258 
262  const QgsRectangle& filterRect() const { return mFilterRect; }
263 
265  QgsFeatureRequest& setFilterFid( QgsFeatureId fid );
267  QgsFeatureId filterFid() const { return mFilterFid; }
268 
270  QgsFeatureRequest& setFilterFids( const QgsFeatureIds& fids );
272  const QgsFeatureIds& filterFids() const { return mFilterFids; }
273 
279  QgsFeatureRequest& setFilterExpression( const QString& expression );
280 
285  QgsExpression* filterExpression() const { return mFilterExpression; }
286 
292  QgsFeatureRequest& combineFilterExpression( const QString& expression );
293 
299  QgsExpressionContext* expressionContext() { return &mExpressionContext; }
300 
306  QgsFeatureRequest& setExpressionContext( const QgsExpressionContext& context );
307 
316  QgsFeatureRequest& disableFilter() { mFilter = FilterNone; return *this; }
317 
329  QgsFeatureRequest& addOrderBy( const QString &expression, bool ascending = true );
330 
340  QgsFeatureRequest& addOrderBy( const QString &expression, bool ascending, bool nullsfirst );
341 
347  OrderBy orderBy() const;
348 
354  QgsFeatureRequest& setOrderBy( const OrderBy& orderBy );
355 
361  QgsFeatureRequest& setLimit( long limit );
362 
367  long limit() const { return mLimit; }
368 
370  QgsFeatureRequest& setFlags( QgsFeatureRequest::Flags flags );
371  const Flags& flags() const { return mFlags; }
372 
375  QgsFeatureRequest& setSubsetOfAttributes( const QgsAttributeList& attrs );
376 
381  QgsAttributeList subsetOfAttributes() const { return mAttrs; }
382 
384  QgsFeatureRequest& setSubsetOfAttributes( const QStringList& attrNames, const QgsFields& fields );
385 
387  QgsFeatureRequest& setSubsetOfAttributes( const QSet<QString>& attrNames, const QgsFields& fields );
388 
391  QgsFeatureRequest& setSimplifyMethod( const QgsSimplifyMethod& simplifyMethod );
394  const QgsSimplifyMethod& simplifyMethod() const { return mSimplifyMethod; }
395 
405  bool acceptFeature( const QgsFeature& feature );
406 
407  // TODO: in future
408  // void setFilterNativeExpression(con QString& expr); // using provider's SQL (if supported)
409 
410  protected:
417  Flags mFlags;
420  long mLimit;
422 };
423 
424 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsFeatureRequest::Flags )
425 
426 
427 class QgsFeatureIterator;
429 
434 class CORE_EXPORT QgsAbstractFeatureSource
435 {
436  public:
437  virtual ~QgsAbstractFeatureSource();
438 
444  virtual QgsFeatureIterator getFeatures( const QgsFeatureRequest& request ) = 0;
445 
446  protected:
447  void iteratorOpened( QgsAbstractFeatureIterator* it );
448  void iteratorClosed( QgsAbstractFeatureIterator* it );
449 
450  QSet< QgsAbstractFeatureIterator* > mActiveIterators;
451 
452  template<typename> friend class QgsAbstractFeatureIteratorFromSource;
453 };
454 
455 #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
QgsExpression * mFilterExpression
const Flags & flags() const
QSet< QgsFeatureId > QgsFeatureIds
Definition: qgsfeature.h:348
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
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 ALL_ATTRIBUTES
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: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.