QGIS API Documentation  2.99.0-Master (5169e0d)
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 
90  {
91  GeometryNoCheck = 0,
92  GeometrySkipInvalid = 1,
93  GeometryAbortOnInvalid = 2,
94  };
95 
118  class CORE_EXPORT OrderByClause
119  {
120  public:
121 
130  OrderByClause( const QString &expression, bool ascending = true );
131 
139  OrderByClause( const QString &expression, bool ascending, bool nullsfirst );
140 
145  QgsExpression expression() const;
146 
151  bool ascending() const;
152 
156  void setAscending( bool ascending );
157 
162  bool nullsFirst() const;
163 
167  void setNullsFirst( bool nullsFirst );
168 
172  QString dump() const;
173 
174  // friend inline int qHash(const OrderByClause &a) { return qHash(a.mExpression.expression()) ^ qHash(a.mAscending) ^ qHash( a.mNullsFirst); }
175 
176  private:
177  QgsExpression mExpression;
178  bool mAscending;
179  bool mNullsFirst;
180  };
181 
188  class OrderBy : public QList<OrderByClause>
189  {
190  public:
191 
195  CORE_EXPORT OrderBy()
196  : QList<OrderByClause>()
197  {}
198 
202  CORE_EXPORT OrderBy( const QList<OrderByClause> &other );
203 
210  QList<OrderByClause> CORE_EXPORT list() const;
211 
215  void CORE_EXPORT save( QDomElement &elem ) const;
216 
220  void CORE_EXPORT load( const QDomElement &elem );
221 
225  QSet<QString> CORE_EXPORT usedAttributes() const;
226 
230  QString CORE_EXPORT dump() const;
231  };
232 
236  static const QString ALL_ATTRIBUTES;
237 
241  explicit QgsFeatureRequest( QgsFeatureId fid );
243  explicit QgsFeatureRequest( const QgsFeatureIds &fids );
245  explicit QgsFeatureRequest( const QgsRectangle &rect );
247  explicit QgsFeatureRequest( const QgsExpression &expr, const QgsExpressionContext &context = QgsExpressionContext() );
251  QgsFeatureRequest &operator=( const QgsFeatureRequest &rh );
252 
258  FilterType filterType() const { return mFilter; }
259 
263  QgsFeatureRequest &setFilterRect( const QgsRectangle &rect );
264 
269  const QgsRectangle &filterRect() const { return mFilterRect; }
270 
272  QgsFeatureRequest &setFilterFid( QgsFeatureId fid );
274  QgsFeatureId filterFid() const { return mFilterFid; }
275 
277  QgsFeatureRequest &setFilterFids( const QgsFeatureIds &fids );
279  const QgsFeatureIds &filterFids() const { return mFilterFids; }
280 
288  QgsFeatureRequest &setInvalidGeometryCheck( InvalidGeometryCheck check );
289 
295  InvalidGeometryCheck invalidGeometryCheck() const { return mInvalidGeometryFilter; }
296 
304  QgsFeatureRequest &setInvalidGeometryCallback( std::function< void( const QgsFeature & ) > callback );
305 
313  std::function< void( const QgsFeature & ) > invalidGeometryCallback() const { return mInvalidGeometryCallback; }
314 
320  QgsFeatureRequest &setFilterExpression( const QString &expression );
321 
326  QgsExpression *filterExpression() const { return mFilterExpression.get(); }
327 
333  QgsFeatureRequest &combineFilterExpression( const QString &expression );
334 
340  QgsExpressionContext *expressionContext() { return &mExpressionContext; }
341 
347  QgsFeatureRequest &setExpressionContext( const QgsExpressionContext &context );
348 
357  QgsFeatureRequest &disableFilter() { mFilter = FilterNone; return *this; }
358 
370  QgsFeatureRequest &addOrderBy( const QString &expression, bool ascending = true );
371 
381  QgsFeatureRequest &addOrderBy( const QString &expression, bool ascending, bool nullsfirst );
382 
388  OrderBy orderBy() const;
389 
395  QgsFeatureRequest &setOrderBy( const OrderBy &orderBy );
396 
402  QgsFeatureRequest &setLimit( long limit );
403 
408  long limit() const { return mLimit; }
409 
411  QgsFeatureRequest &setFlags( QgsFeatureRequest::Flags flags );
412  const Flags &flags() const { return mFlags; }
413 
416  QgsFeatureRequest &setSubsetOfAttributes( const QgsAttributeList &attrs );
417 
422  QgsAttributeList subsetOfAttributes() const { return mAttrs; }
423 
425  QgsFeatureRequest &setSubsetOfAttributes( const QStringList &attrNames, const QgsFields &fields );
426 
428  QgsFeatureRequest &setSubsetOfAttributes( const QSet<QString> &attrNames, const QgsFields &fields );
429 
432  QgsFeatureRequest &setSimplifyMethod( const QgsSimplifyMethod &simplifyMethod );
435  const QgsSimplifyMethod &simplifyMethod() const { return mSimplifyMethod; }
436 
446  bool acceptFeature( const QgsFeature &feature );
447 
448  protected:
449  FilterType mFilter = FilterNone;
451  QgsFeatureId mFilterFid = -1;
453  std::unique_ptr< QgsExpression > mFilterExpression;
455  Flags mFlags;
458  long mLimit = -1;
460  InvalidGeometryCheck mInvalidGeometryFilter = GeometryNoCheck;
461  std::function< void( const QgsFeature & ) > mInvalidGeometryCallback;
462 };
463 
464 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsFeatureRequest::Flags )
465 
466 
467 class QgsFeatureIterator;
469 
474 class CORE_EXPORT QgsAbstractFeatureSource
475 {
476  public:
477  virtual ~QgsAbstractFeatureSource();
478 
484  virtual QgsFeatureIterator getFeatures( const QgsFeatureRequest &request = QgsFeatureRequest() ) = 0;
485 
486  protected:
487  void iteratorOpened( QgsAbstractFeatureIterator *it );
488  void iteratorClosed( QgsAbstractFeatureIterator *it );
489 
490  QSet< QgsAbstractFeatureIterator * > mActiveIterators;
491 
492  template<typename> friend class QgsAbstractFeatureIteratorFromSource;
493 };
494 
495 #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:38
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:517
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:61
const QgsRectangle & filterRect() const
Get the rectangle from which features will be taken.
QgsExpressionContext mExpressionContext
InvalidGeometryCheck
Handling of features with invalid geometries.
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.
std::function< void(const QgsFeature &) > mInvalidGeometryCallback
FilterType
Types of filters.
QgsAttributeList mAttrs
InvalidGeometryCheck invalidGeometryCheck() const
Returns the invalid geometry checking behavior.
qint64 QgsFeatureId
Definition: qgsfeature.h:37
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.
std::function< void(const QgsFeature &) > invalidGeometryCallback() const
Returns the callback function to use when encountering an invalid geometry and invalidGeometryCheck()...
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.