QGIS API Documentation  3.17.0-Master (a035f434f4)
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 
75 class CORE_EXPORT QgsFeatureRequest
76 {
77  public:
78  enum Flag
79  {
80  NoFlags = 0,
81  NoGeometry = 1,
82  SubsetOfAttributes = 2,
83  ExactIntersect = 4
84  };
85  Q_DECLARE_FLAGS( Flags, Flag )
86 
87 
91  {
95  FilterFids
96  };
97 
100  {
101  GeometryNoCheck = 0,
102  GeometrySkipInvalid = 1,
103  GeometryAbortOnInvalid = 2,
104  };
105 
129  class CORE_EXPORT OrderByClause
130  {
131  public:
132 
141  OrderByClause( const QString &expression, bool ascending = true );
142 
150  OrderByClause( const QString &expression, bool ascending, bool nullsfirst );
151 
160  OrderByClause( const QgsExpression &expression, bool ascending = true );
161 
169  OrderByClause( const QgsExpression &expression, bool ascending, bool nullsfirst );
170 
175  QgsExpression expression() const;
176 
184  bool prepare( QgsExpressionContext *context );
185 
190  bool ascending() const;
191 
195  void setAscending( bool ascending );
196 
201  bool nullsFirst() const;
202 
206  void setNullsFirst( bool nullsFirst );
207 
211  QString dump() const;
212 
213  // friend inline int qHash(const OrderByClause &a) { return qHash(a.mExpression.expression()) ^ qHash(a.mAscending) ^ qHash( a.mNullsFirst); }
214 
215  private:
216  QgsExpression mExpression;
217  bool mAscending;
218  bool mNullsFirst;
219  };
220 
221 
229  class OrderBy : public QList<QgsFeatureRequest::OrderByClause>
230  {
231  public:
232 
236  CORE_EXPORT OrderBy();
237 
241  CORE_EXPORT OrderBy( const QList<QgsFeatureRequest::OrderByClause> &other );
242 
249  QList<QgsFeatureRequest::OrderByClause> CORE_EXPORT list() const;
250 
254  void CORE_EXPORT save( QDomElement &elem ) const;
255 
259  void CORE_EXPORT load( const QDomElement &elem );
260 
265  QSet<QString> CORE_EXPORT usedAttributes() const;
266 
271  QSet<int> CORE_EXPORT usedAttributeIndices( const QgsFields &fields ) const;
272 
276  QString CORE_EXPORT dump() const;
277  };
278 
282  static const QString ALL_ATTRIBUTES;
283 
287  explicit QgsFeatureRequest( QgsFeatureId fid );
289  explicit QgsFeatureRequest( const QgsFeatureIds &fids );
290 
298  explicit QgsFeatureRequest( const QgsRectangle &rectangle );
299 
301  explicit QgsFeatureRequest( const QgsExpression &expr, const QgsExpressionContext &context = QgsExpressionContext() );
305  QgsFeatureRequest &operator=( const QgsFeatureRequest &rh );
306 
312  FilterType filterType() const { return mFilter; }
313 
323  QgsFeatureRequest &setFilterRect( const QgsRectangle &rectangle );
324 
335  const QgsRectangle &filterRect() const { return mFilterRect; }
336 
338  QgsFeatureRequest &setFilterFid( QgsFeatureId fid );
340  QgsFeatureId filterFid() const { return mFilterFid; }
341 
343  QgsFeatureRequest &setFilterFids( const QgsFeatureIds &fids );
345  const QgsFeatureIds &filterFids() const { return mFilterFids; }
346 
354  QgsFeatureRequest &setInvalidGeometryCheck( InvalidGeometryCheck check );
355 
361  InvalidGeometryCheck invalidGeometryCheck() const { return mInvalidGeometryFilter; }
362 
370 #ifndef SIP_RUN
371  QgsFeatureRequest &setInvalidGeometryCallback( const std::function< void( const QgsFeature & )> &callback );
372 #else
373  QgsFeatureRequest &setInvalidGeometryCallback( SIP_PYCALLABLE / AllowNone / );
374  % MethodCode
375  Py_BEGIN_ALLOW_THREADS
376 
377  sipCpp->setInvalidGeometryCallback( [a0]( const QgsFeature &arg )
378  {
379  SIP_BLOCK_THREADS
380  Py_XDECREF( sipCallMethod( NULL, a0, "D", &arg, sipType_QgsFeature, NULL ) );
381  SIP_UNBLOCK_THREADS
382  } );
383 
384  sipRes = sipCpp;
385 
386  Py_END_ALLOW_THREADS
387  % End
388 #endif
389 
397  std::function< void( const QgsFeature & ) > invalidGeometryCallback() const { return mInvalidGeometryCallback; } SIP_SKIP
398 
405  QgsFeatureRequest &setFilterExpression( const QString &expression );
406 
412  QgsExpression *filterExpression() const { return mFilterExpression.get(); }
413 
420  QgsFeatureRequest &combineFilterExpression( const QString &expression );
421 
428  QgsExpressionContext *expressionContext() { return &mExpressionContext; }
429 
436  QgsFeatureRequest &setExpressionContext( const QgsExpressionContext &context );
437 
446  QgsFeatureRequest &disableFilter() { mFilter = FilterNone; mFilterExpression.reset(); return *this; }
447 
459  QgsFeatureRequest &addOrderBy( const QString &expression, bool ascending = true );
460 
470  QgsFeatureRequest &addOrderBy( const QString &expression, bool ascending, bool nullsfirst );
471 
477  OrderBy orderBy() const;
478 
484  QgsFeatureRequest &setOrderBy( const OrderBy &orderBy );
485 
492  QgsFeatureRequest &setLimit( long limit );
493 
499  long limit() const { return mLimit; }
500 
502  QgsFeatureRequest &setFlags( QgsFeatureRequest::Flags flags );
503  const Flags &flags() const { return mFlags; }
504 
512  QgsFeatureRequest &setSubsetOfAttributes( const QgsAttributeList &attrs );
513 
520  QgsFeatureRequest &setNoAttributes();
521 
526  QgsAttributeList subsetOfAttributes() const { return mAttrs; }
527 
529  QgsFeatureRequest &setSubsetOfAttributes( const QStringList &attrNames, const QgsFields &fields );
530 
532  QgsFeatureRequest &setSubsetOfAttributes( const QSet<QString> &attrNames, const QgsFields &fields );
533 
538  QgsFeatureRequest &setSimplifyMethod( const QgsSimplifyMethod &simplifyMethod );
539 
544  const QgsSimplifyMethod &simplifyMethod() const { return mSimplifyMethod; }
545 
554  QgsCoordinateReferenceSystem destinationCrs() const;
555 
563  QgsCoordinateTransformContext transformContext() const;
564 
588  QgsFeatureRequest &setDestinationCrs( const QgsCoordinateReferenceSystem &crs, const QgsCoordinateTransformContext &context );
589 
598 #ifndef SIP_RUN
599  QgsFeatureRequest &setTransformErrorCallback( const std::function< void( const QgsFeature & )> &callback );
600 #else
601  QgsFeatureRequest &setTransformErrorCallback( SIP_PYCALLABLE / AllowNone / );
602  % MethodCode
603  Py_BEGIN_ALLOW_THREADS
604 
605  sipCpp->setTransformErrorCallback( [a0]( const QgsFeature &arg )
606  {
607  SIP_BLOCK_THREADS
608  Py_XDECREF( sipCallMethod( NULL, a0, "D", &arg, sipType_QgsFeature, NULL ) );
609  SIP_UNBLOCK_THREADS
610  } );
611 
612  sipRes = sipCpp;
613 
614  Py_END_ALLOW_THREADS
615  % End
616 #endif
617 
626  std::function< void( const QgsFeature & ) > transformErrorCallback() const { return mTransformErrorCallback; } SIP_SKIP
627 
628 
638  bool acceptFeature( const QgsFeature &feature );
639 
649  Q_DECL_DEPRECATED int connectionTimeout() const SIP_DEPRECATED;
650 
660  Q_DECL_DEPRECATED QgsFeatureRequest &setConnectionTimeout( int connectionTimeout ) SIP_DEPRECATED;
661 
670  int timeout() const;
671 
680  QgsFeatureRequest &setTimeout( int timeout );
681 
694  bool requestMayBeNested() const;
695 
708  QgsFeatureRequest &setRequestMayBeNested( bool requestMayBeNested );
709 
710  protected:
711  FilterType mFilter = FilterNone;
713  QgsFeatureId mFilterFid = -1;
715  std::unique_ptr< QgsExpression > mFilterExpression;
717  Flags mFlags = Flags();
720  long mLimit = -1;
722  InvalidGeometryCheck mInvalidGeometryFilter = GeometryNoCheck;
723  std::function< void( const QgsFeature & ) > mInvalidGeometryCallback;
724  std::function< void( const QgsFeature & ) > mTransformErrorCallback;
727  int mTimeout = -1;
728  int mRequestMayBeNested = false;
729 };
730 
731 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsFeatureRequest::Flags )
732 
733 
734 class QgsFeatureIterator;
736 
742 class CORE_EXPORT QgsAbstractFeatureSource
743 {
744  public:
745  virtual ~QgsAbstractFeatureSource();
746 
752  virtual QgsFeatureIterator getFeatures( const QgsFeatureRequest &request = QgsFeatureRequest() ) = 0;
753 
754  protected:
755  void iteratorOpened( QgsAbstractFeatureIterator *it );
756  void iteratorClosed( QgsAbstractFeatureIterator *it );
757 
758  QSet< QgsAbstractFeatureIterator * > mActiveIterators;
759 
760  template<typename> friend class QgsAbstractFeatureIteratorFromSource;
761 };
762 
763 #endif // QGSFEATUREREQUEST_H
Class for parsing and evaluation of expressions (formerly called "search strings").
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:41
long limit() const
Returns the maximum number of features to request, or -1 if no limit set.
Filter using feature ID.
QgsSimplifyMethod mSimplifyMethod
QSet< QgsFeatureId > QgsFeatureIds
Definition: qgsfeatureid.h:37
const Flags & flags() const
QgsExpressionContext * expressionContext()
Returns the expression context used to evaluate filter expressions.
QgsFeatureId filterFid() const
Gets the feature ID that should be fetched.
qint64 QgsFeatureId
64 bit feature ids negative numbers are used for uncommitted/newly added features ...
Definition: qgsfeatureid.h:28
Q_DECLARE_OPERATORS_FOR_FLAGS(QgsField::ConfigurationFlags) CORE_EXPORT QDataStream &operator<<(QDataStream &out
Writes the field to stream out. QGIS version compatibility is not guaranteed.
FilterType filterType() const
Returns the filter type which is currently set on this request.
Container of fields for a vector layer.
Definition: qgsfields.h:44
const QgsFeatureIds & filterFids() const
Gets 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:55
const QgsCoordinateReferenceSystem & crs
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:126
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
Contains information about the context in which a coordinate transform is executed.
Base class that can be used for any class that is capable of returning features.
#define SIP_DEPRECATED
Definition: qgis_sip.h:106
QgsAttributeList subsetOfAttributes() const
Returns 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.
QgsCoordinateTransformContext mTransformContext
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.
This class contains information about how to simplify geometries fetched from a QgsFeatureIterator.
QList< int > QgsAttributeList
Definition: qgsfield.h:26
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
Gets 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()...
Represents a list of OrderByClauses, with the most important first and the least important last...
Helper template that cares of two things: 1.