QGIS API Documentation  2.99.0-Master (13a3f2f)
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 "qgis_sip.h"
20 #include <QFlags>
21 #include <QList>
22 #include <memory>
23 
24 #include "qgsfeature.h"
25 #include "qgsrectangle.h"
26 #include "qgsexpression.h"
27 #include "qgsexpressioncontext.h"
28 #include "qgssimplifymethod.h"
29 
30 
31 
66 class CORE_EXPORT QgsFeatureRequest
67 {
68  public:
69  enum Flag
70  {
71  NoFlags = 0,
72  NoGeometry = 1,
73  SubsetOfAttributes = 2,
74  ExactIntersect = 4
75  };
76  Q_DECLARE_FLAGS( Flags, Flag )
77 
78 
82  {
86  FilterFids
87  };
88 
91  {
92  GeometryNoCheck = 0,
93  GeometrySkipInvalid = 1,
94  GeometryAbortOnInvalid = 2,
95  };
96 
119  class CORE_EXPORT OrderByClause
120  {
121  public:
122 
131  OrderByClause( const QString &expression, bool ascending = true );
132 
140  OrderByClause( const QString &expression, bool ascending, bool nullsfirst );
141 
150  OrderByClause( const QgsExpression &expression, bool ascending = true );
151 
159  OrderByClause( const QgsExpression &expression, bool ascending, bool nullsfirst );
160 
165  QgsExpression expression() const;
166 
174  bool prepare( QgsExpressionContext *context );
175 
180  bool ascending() const;
181 
185  void setAscending( bool ascending );
186 
191  bool nullsFirst() const;
192 
196  void setNullsFirst( bool nullsFirst );
197 
201  QString dump() const;
202 
203  // friend inline int qHash(const OrderByClause &a) { return qHash(a.mExpression.expression()) ^ qHash(a.mAscending) ^ qHash( a.mNullsFirst); }
204 
205  private:
206  QgsExpression mExpression;
207  bool mAscending;
208  bool mNullsFirst;
209  };
210 
211 
218  class OrderBy : public QList<QgsFeatureRequest::OrderByClause>
219  {
220  public:
221 
225  CORE_EXPORT OrderBy()
226  : QList<QgsFeatureRequest::OrderByClause>()
227  {}
228 
232  CORE_EXPORT OrderBy( const QList<QgsFeatureRequest::OrderByClause> &other );
233 
240  QList<QgsFeatureRequest::OrderByClause> CORE_EXPORT list() const;
241 
245  void CORE_EXPORT save( QDomElement &elem ) const;
246 
250  void CORE_EXPORT load( const QDomElement &elem );
251 
255  QSet<QString> CORE_EXPORT usedAttributes() const;
256 
260  QString CORE_EXPORT dump() const;
261  };
262 
266  static const QString ALL_ATTRIBUTES;
267 
271  explicit QgsFeatureRequest( QgsFeatureId fid );
273  explicit QgsFeatureRequest( const QgsFeatureIds &fids );
275  explicit QgsFeatureRequest( const QgsRectangle &rect );
277  explicit QgsFeatureRequest( const QgsExpression &expr, const QgsExpressionContext &context = QgsExpressionContext() );
281  QgsFeatureRequest &operator=( const QgsFeatureRequest &rh );
282 
288  FilterType filterType() const { return mFilter; }
289 
293  QgsFeatureRequest &setFilterRect( const QgsRectangle &rect );
294 
299  const QgsRectangle &filterRect() const { return mFilterRect; }
300 
302  QgsFeatureRequest &setFilterFid( QgsFeatureId fid );
304  QgsFeatureId filterFid() const { return mFilterFid; }
305 
307  QgsFeatureRequest &setFilterFids( const QgsFeatureIds &fids );
309  const QgsFeatureIds &filterFids() const { return mFilterFids; }
310 
318  QgsFeatureRequest &setInvalidGeometryCheck( InvalidGeometryCheck check );
319 
325  InvalidGeometryCheck invalidGeometryCheck() const { return mInvalidGeometryFilter; }
326 
334 #ifndef SIP_RUN
335  QgsFeatureRequest &setInvalidGeometryCallback( std::function< void( const QgsFeature & ) > callback );
336 #else
337  QgsFeatureRequest &setInvalidGeometryCallback( SIP_PYCALLABLE / AllowNone / );
338  % MethodCode
339  Py_BEGIN_ALLOW_THREADS
340 
341  sipCpp->setInvalidGeometryCallback( [a0]( const QgsFeature &arg )
342  {
343  SIP_BLOCK_THREADS
344  Py_XDECREF( sipCallMethod( NULL, a0, "D", &arg, sipType_QgsFeature, NULL ) );
345  SIP_UNBLOCK_THREADS
346  } );
347 
348  sipRes = sipCpp;
349 
350  Py_END_ALLOW_THREADS
351  % End
352 #endif
353 
361  std::function< void( const QgsFeature & ) > invalidGeometryCallback() const { return mInvalidGeometryCallback; } SIP_SKIP
362 
368  QgsFeatureRequest &setFilterExpression( const QString &expression );
369 
374  QgsExpression *filterExpression() const { return mFilterExpression.get(); }
375 
381  QgsFeatureRequest &combineFilterExpression( const QString &expression );
382 
388  QgsExpressionContext *expressionContext() { return &mExpressionContext; }
389 
395  QgsFeatureRequest &setExpressionContext( const QgsExpressionContext &context );
396 
405  QgsFeatureRequest &disableFilter() { mFilter = FilterNone; return *this; }
406 
418  QgsFeatureRequest &addOrderBy( const QString &expression, bool ascending = true );
419 
429  QgsFeatureRequest &addOrderBy( const QString &expression, bool ascending, bool nullsfirst );
430 
436  OrderBy orderBy() const;
437 
443  QgsFeatureRequest &setOrderBy( const OrderBy &orderBy );
444 
450  QgsFeatureRequest &setLimit( long limit );
451 
456  long limit() const { return mLimit; }
457 
459  QgsFeatureRequest &setFlags( QgsFeatureRequest::Flags flags );
460  const Flags &flags() const { return mFlags; }
461 
464  QgsFeatureRequest &setSubsetOfAttributes( const QgsAttributeList &attrs );
465 
470  QgsAttributeList subsetOfAttributes() const { return mAttrs; }
471 
473  QgsFeatureRequest &setSubsetOfAttributes( const QStringList &attrNames, const QgsFields &fields );
474 
476  QgsFeatureRequest &setSubsetOfAttributes( const QSet<QString> &attrNames, const QgsFields &fields );
477 
480  QgsFeatureRequest &setSimplifyMethod( const QgsSimplifyMethod &simplifyMethod );
483  const QgsSimplifyMethod &simplifyMethod() const { return mSimplifyMethod; }
484 
494  bool acceptFeature( const QgsFeature &feature );
495 
496  protected:
497  FilterType mFilter = FilterNone;
499  QgsFeatureId mFilterFid = -1;
501  std::unique_ptr< QgsExpression > mFilterExpression;
503  Flags mFlags;
506  long mLimit = -1;
508  InvalidGeometryCheck mInvalidGeometryFilter = GeometryNoCheck;
509  std::function< void( const QgsFeature & ) > mInvalidGeometryCallback;
510 };
511 
512 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsFeatureRequest::Flags )
513 
514 
515 class QgsFeatureIterator;
517 
522 class CORE_EXPORT QgsAbstractFeatureSource
523 {
524  public:
525  virtual ~QgsAbstractFeatureSource();
526 
532  virtual QgsFeatureIterator getFeatures( const QgsFeatureRequest &request = QgsFeatureRequest() ) = 0;
533 
534  protected:
535  void iteratorOpened( QgsAbstractFeatureIterator *it );
536  void iteratorClosed( QgsAbstractFeatureIterator *it );
537 
538  QSet< QgsAbstractFeatureIterator * > mActiveIterators;
539 
540  template<typename> friend class QgsAbstractFeatureIteratorFromSource;
541 };
542 
543 #endif // QGSFEATUREREQUEST_H
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:519
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:41
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.
#define SIP_SKIP
Definition: qgis_sip.h:102
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)...
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.
QList< int > QgsAttributeList
Definition: qgsfield.h:27
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.