QGIS API Documentation  2.99.0-Master (25b0421)
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 
67 class CORE_EXPORT QgsFeatureRequest
68 {
69  public:
70  enum Flag
71  {
72  NoFlags = 0,
73  NoGeometry = 1,
74  SubsetOfAttributes = 2,
75  ExactIntersect = 4
76  };
77  Q_DECLARE_FLAGS( Flags, Flag )
78 
79 
83  {
87  FilterFids
88  };
89 
92  {
93  GeometryNoCheck = 0,
94  GeometrySkipInvalid = 1,
95  GeometryAbortOnInvalid = 2,
96  };
97 
121  class CORE_EXPORT OrderByClause
122  {
123  public:
124 
133  OrderByClause( const QString &expression, bool ascending = true );
134 
142  OrderByClause( const QString &expression, bool ascending, bool nullsfirst );
143 
152  OrderByClause( const QgsExpression &expression, bool ascending = true );
153 
161  OrderByClause( const QgsExpression &expression, bool ascending, bool nullsfirst );
162 
167  QgsExpression expression() const;
168 
176  bool prepare( QgsExpressionContext *context );
177 
182  bool ascending() const;
183 
187  void setAscending( bool ascending );
188 
193  bool nullsFirst() const;
194 
198  void setNullsFirst( bool nullsFirst );
199 
203  QString dump() const;
204 
205  // friend inline int qHash(const OrderByClause &a) { return qHash(a.mExpression.expression()) ^ qHash(a.mAscending) ^ qHash( a.mNullsFirst); }
206 
207  private:
208  QgsExpression mExpression;
209  bool mAscending;
210  bool mNullsFirst;
211  };
212 
213 
221  class OrderBy : public QList<QgsFeatureRequest::OrderByClause>
222  {
223  public:
224 
228  CORE_EXPORT OrderBy() {}
229 
233  CORE_EXPORT OrderBy( const QList<QgsFeatureRequest::OrderByClause> &other );
234 
241  QList<QgsFeatureRequest::OrderByClause> CORE_EXPORT list() const;
242 
246  void CORE_EXPORT save( QDomElement &elem ) const;
247 
251  void CORE_EXPORT load( const QDomElement &elem );
252 
256  QSet<QString> CORE_EXPORT usedAttributes() const;
257 
261  QString CORE_EXPORT dump() const;
262  };
263 
267  static const QString ALL_ATTRIBUTES;
268 
272  explicit QgsFeatureRequest( QgsFeatureId fid );
274  explicit QgsFeatureRequest( const QgsFeatureIds &fids );
275 
283  explicit QgsFeatureRequest( const QgsRectangle &rectangle );
284 
286  explicit QgsFeatureRequest( const QgsExpression &expr, const QgsExpressionContext &context = QgsExpressionContext() );
290  QgsFeatureRequest &operator=( const QgsFeatureRequest &rh );
291 
297  FilterType filterType() const { return mFilter; }
298 
308  QgsFeatureRequest &setFilterRect( const QgsRectangle &rectangle );
309 
320  const QgsRectangle &filterRect() const { return mFilterRect; }
321 
323  QgsFeatureRequest &setFilterFid( QgsFeatureId fid );
325  QgsFeatureId filterFid() const { return mFilterFid; }
326 
328  QgsFeatureRequest &setFilterFids( const QgsFeatureIds &fids );
330  const QgsFeatureIds &filterFids() const { return mFilterFids; }
331 
339  QgsFeatureRequest &setInvalidGeometryCheck( InvalidGeometryCheck check );
340 
346  InvalidGeometryCheck invalidGeometryCheck() const { return mInvalidGeometryFilter; }
347 
355 #ifndef SIP_RUN
356  QgsFeatureRequest &setInvalidGeometryCallback( const std::function< void( const QgsFeature & )> &callback );
357 #else
358  QgsFeatureRequest &setInvalidGeometryCallback( SIP_PYCALLABLE / AllowNone / );
359  % MethodCode
360  Py_BEGIN_ALLOW_THREADS
361 
362  sipCpp->setInvalidGeometryCallback( [a0]( const QgsFeature &arg )
363  {
364  SIP_BLOCK_THREADS
365  Py_XDECREF( sipCallMethod( NULL, a0, "D", &arg, sipType_QgsFeature, NULL ) );
366  SIP_UNBLOCK_THREADS
367  } );
368 
369  sipRes = sipCpp;
370 
371  Py_END_ALLOW_THREADS
372  % End
373 #endif
374 
382  std::function< void( const QgsFeature & ) > invalidGeometryCallback() const { return mInvalidGeometryCallback; } SIP_SKIP
383 
390  QgsFeatureRequest &setFilterExpression( const QString &expression );
391 
397  QgsExpression *filterExpression() const { return mFilterExpression.get(); }
398 
405  QgsFeatureRequest &combineFilterExpression( const QString &expression );
406 
413  QgsExpressionContext *expressionContext() { return &mExpressionContext; }
414 
421  QgsFeatureRequest &setExpressionContext( const QgsExpressionContext &context );
422 
431  QgsFeatureRequest &disableFilter() { mFilter = FilterNone; return *this; }
432 
444  QgsFeatureRequest &addOrderBy( const QString &expression, bool ascending = true );
445 
455  QgsFeatureRequest &addOrderBy( const QString &expression, bool ascending, bool nullsfirst );
456 
462  OrderBy orderBy() const;
463 
469  QgsFeatureRequest &setOrderBy( const OrderBy &orderBy );
470 
477  QgsFeatureRequest &setLimit( long limit );
478 
484  long limit() const { return mLimit; }
485 
487  QgsFeatureRequest &setFlags( QgsFeatureRequest::Flags flags );
488  const Flags &flags() const { return mFlags; }
489 
494  QgsFeatureRequest &setSubsetOfAttributes( const QgsAttributeList &attrs );
495 
500  QgsAttributeList subsetOfAttributes() const { return mAttrs; }
501 
503  QgsFeatureRequest &setSubsetOfAttributes( const QStringList &attrNames, const QgsFields &fields );
504 
506  QgsFeatureRequest &setSubsetOfAttributes( const QSet<QString> &attrNames, const QgsFields &fields );
507 
512  QgsFeatureRequest &setSimplifyMethod( const QgsSimplifyMethod &simplifyMethod );
513 
518  const QgsSimplifyMethod &simplifyMethod() const { return mSimplifyMethod; }
519 
527  QgsCoordinateReferenceSystem destinationCrs() const;
528 
552  QgsFeatureRequest &setDestinationCrs( const QgsCoordinateReferenceSystem &crs );
553 
562 #ifndef SIP_RUN
563  QgsFeatureRequest &setTransformErrorCallback( const std::function< void( const QgsFeature & )> &callback );
564 #else
565  QgsFeatureRequest &setTransformErrorCallback( SIP_PYCALLABLE / AllowNone / );
566  % MethodCode
567  Py_BEGIN_ALLOW_THREADS
568 
569  sipCpp->setTransformErrorCallback( [a0]( const QgsFeature &arg )
570  {
571  SIP_BLOCK_THREADS
572  Py_XDECREF( sipCallMethod( NULL, a0, "D", &arg, sipType_QgsFeature, NULL ) );
573  SIP_UNBLOCK_THREADS
574  } );
575 
576  sipRes = sipCpp;
577 
578  Py_END_ALLOW_THREADS
579  % End
580 #endif
581 
590  std::function< void( const QgsFeature & ) > transformErrorCallback() const { return mTransformErrorCallback; } SIP_SKIP
591 
592 
602  bool acceptFeature( const QgsFeature &feature );
603 
604  protected:
605  FilterType mFilter = FilterNone;
607  QgsFeatureId mFilterFid = -1;
609  std::unique_ptr< QgsExpression > mFilterExpression;
611  Flags mFlags;
614  long mLimit = -1;
616  InvalidGeometryCheck mInvalidGeometryFilter = GeometryNoCheck;
617  std::function< void( const QgsFeature & ) > mInvalidGeometryCallback;
618  std::function< void( const QgsFeature & ) > mTransformErrorCallback;
620 };
621 
622 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsFeatureRequest::Flags )
623 
624 
625 class QgsFeatureIterator;
627 
633 class CORE_EXPORT QgsAbstractFeatureSource
634 {
635  public:
636  virtual ~QgsAbstractFeatureSource();
637 
643  virtual QgsFeatureIterator getFeatures( const QgsFeatureRequest &request = QgsFeatureRequest() ) = 0;
644 
645  protected:
646  void iteratorOpened( QgsAbstractFeatureIterator *it );
647  void iteratorClosed( QgsAbstractFeatureIterator *it );
648 
649  QSet< QgsAbstractFeatureIterator * > mActiveIterators;
650 
651  template<typename> friend class QgsAbstractFeatureIteratorFromSource;
652 };
653 
654 #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:39
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:544
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:42
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:62
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.