QGIS API Documentation  2.99.0-Master (9caa722)
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 );
274 
282  explicit QgsFeatureRequest( const QgsRectangle &rectangle );
283 
285  explicit QgsFeatureRequest( const QgsExpression &expr, const QgsExpressionContext &context = QgsExpressionContext() );
289  QgsFeatureRequest &operator=( const QgsFeatureRequest &rh );
290 
296  FilterType filterType() const { return mFilter; }
297 
307  QgsFeatureRequest &setFilterRect( const QgsRectangle &rectangle );
308 
319  const QgsRectangle &filterRect() const { return mFilterRect; }
320 
322  QgsFeatureRequest &setFilterFid( QgsFeatureId fid );
324  QgsFeatureId filterFid() const { return mFilterFid; }
325 
327  QgsFeatureRequest &setFilterFids( const QgsFeatureIds &fids );
329  const QgsFeatureIds &filterFids() const { return mFilterFids; }
330 
338  QgsFeatureRequest &setInvalidGeometryCheck( InvalidGeometryCheck check );
339 
345  InvalidGeometryCheck invalidGeometryCheck() const { return mInvalidGeometryFilter; }
346 
354 #ifndef SIP_RUN
355  QgsFeatureRequest &setInvalidGeometryCallback( const std::function< void( const QgsFeature & )> &callback );
356 #else
357  QgsFeatureRequest &setInvalidGeometryCallback( SIP_PYCALLABLE / AllowNone / );
358  % MethodCode
359  Py_BEGIN_ALLOW_THREADS
360 
361  sipCpp->setInvalidGeometryCallback( [a0]( const QgsFeature &arg )
362  {
363  SIP_BLOCK_THREADS
364  Py_XDECREF( sipCallMethod( NULL, a0, "D", &arg, sipType_QgsFeature, NULL ) );
365  SIP_UNBLOCK_THREADS
366  } );
367 
368  sipRes = sipCpp;
369 
370  Py_END_ALLOW_THREADS
371  % End
372 #endif
373 
381  std::function< void( const QgsFeature & ) > invalidGeometryCallback() const { return mInvalidGeometryCallback; } SIP_SKIP
382 
388  QgsFeatureRequest &setFilterExpression( const QString &expression );
389 
394  QgsExpression *filterExpression() const { return mFilterExpression.get(); }
395 
401  QgsFeatureRequest &combineFilterExpression( const QString &expression );
402 
408  QgsExpressionContext *expressionContext() { return &mExpressionContext; }
409 
415  QgsFeatureRequest &setExpressionContext( const QgsExpressionContext &context );
416 
425  QgsFeatureRequest &disableFilter() { mFilter = FilterNone; return *this; }
426 
438  QgsFeatureRequest &addOrderBy( const QString &expression, bool ascending = true );
439 
449  QgsFeatureRequest &addOrderBy( const QString &expression, bool ascending, bool nullsfirst );
450 
456  OrderBy orderBy() const;
457 
463  QgsFeatureRequest &setOrderBy( const OrderBy &orderBy );
464 
470  QgsFeatureRequest &setLimit( long limit );
471 
476  long limit() const { return mLimit; }
477 
479  QgsFeatureRequest &setFlags( QgsFeatureRequest::Flags flags );
480  const Flags &flags() const { return mFlags; }
481 
484  QgsFeatureRequest &setSubsetOfAttributes( const QgsAttributeList &attrs );
485 
490  QgsAttributeList subsetOfAttributes() const { return mAttrs; }
491 
493  QgsFeatureRequest &setSubsetOfAttributes( const QStringList &attrNames, const QgsFields &fields );
494 
496  QgsFeatureRequest &setSubsetOfAttributes( const QSet<QString> &attrNames, const QgsFields &fields );
497 
500  QgsFeatureRequest &setSimplifyMethod( const QgsSimplifyMethod &simplifyMethod );
503  const QgsSimplifyMethod &simplifyMethod() const { return mSimplifyMethod; }
504 
512  QgsCoordinateReferenceSystem destinationCrs() const;
513 
537  QgsFeatureRequest &setDestinationCrs( const QgsCoordinateReferenceSystem &crs );
538 
547 #ifndef SIP_RUN
548  QgsFeatureRequest &setTransformErrorCallback( const std::function< void( const QgsFeature & )> &callback );
549 #else
550  QgsFeatureRequest &setTransformErrorCallback( SIP_PYCALLABLE / AllowNone / );
551  % MethodCode
552  Py_BEGIN_ALLOW_THREADS
553 
554  sipCpp->setTransformErrorCallback( [a0]( const QgsFeature &arg )
555  {
556  SIP_BLOCK_THREADS
557  Py_XDECREF( sipCallMethod( NULL, a0, "D", &arg, sipType_QgsFeature, NULL ) );
558  SIP_UNBLOCK_THREADS
559  } );
560 
561  sipRes = sipCpp;
562 
563  Py_END_ALLOW_THREADS
564  % End
565 #endif
566 
575  std::function< void( const QgsFeature & ) > transformErrorCallback() const { return mTransformErrorCallback; } SIP_SKIP
576 
577 
587  bool acceptFeature( const QgsFeature &feature );
588 
589  protected:
590  FilterType mFilter = FilterNone;
592  QgsFeatureId mFilterFid = -1;
594  std::unique_ptr< QgsExpression > mFilterExpression;
596  Flags mFlags;
599  long mLimit = -1;
601  InvalidGeometryCheck mInvalidGeometryFilter = GeometryNoCheck;
602  std::function< void( const QgsFeature & ) > mInvalidGeometryCallback;
603  std::function< void( const QgsFeature & ) > mTransformErrorCallback;
605 };
606 
607 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsFeatureRequest::Flags )
608 
609 
610 class QgsFeatureIterator;
612 
617 class CORE_EXPORT QgsAbstractFeatureSource
618 {
619  public:
620  virtual ~QgsAbstractFeatureSource();
621 
627  virtual QgsFeatureIterator getFeatures( const QgsFeatureRequest &request = QgsFeatureRequest() ) = 0;
628 
629  protected:
630  void iteratorOpened( QgsAbstractFeatureIterator *it );
631  void iteratorClosed( QgsAbstractFeatureIterator *it );
632 
633  QSet< QgsAbstractFeatureIterator * > mActiveIterators;
634 
635  template<typename> friend class QgsAbstractFeatureIteratorFromSource;
636 };
637 
638 #endif // QGSFEATUREREQUEST_H
QgsCoordinateReferenceSystem mCrs
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
Returns 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:119
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.
std::function< void(const QgsFeature &) > mTransformErrorCallback
QgsAttributeList mAttrs
This class represents a coordinate reference system (CRS).
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
std::function< void(const QgsFeature &) > transformErrorCallback() const
Returns the callback function to use when encountering a transform error when iterating features and ...
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.