QGIS API Documentation  3.10.0-A Coruña (6c816b4204)
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 
72 class CORE_EXPORT QgsFeatureRequest
73 {
74  public:
75  enum Flag
76  {
77  NoFlags = 0,
78  NoGeometry = 1,
79  SubsetOfAttributes = 2,
80  ExactIntersect = 4
81  };
82  Q_DECLARE_FLAGS( Flags, Flag )
83 
84 
88  {
92  FilterFids
93  };
94 
97  {
98  GeometryNoCheck = 0,
99  GeometrySkipInvalid = 1,
100  GeometryAbortOnInvalid = 2,
101  };
102 
126  class CORE_EXPORT OrderByClause
127  {
128  public:
129 
138  OrderByClause( const QString &expression, bool ascending = true );
139 
147  OrderByClause( const QString &expression, bool ascending, bool nullsfirst );
148 
157  OrderByClause( const QgsExpression &expression, bool ascending = true );
158 
166  OrderByClause( const QgsExpression &expression, bool ascending, bool nullsfirst );
167 
172  QgsExpression expression() const;
173 
181  bool prepare( QgsExpressionContext *context );
182 
187  bool ascending() const;
188 
192  void setAscending( bool ascending );
193 
198  bool nullsFirst() const;
199 
203  void setNullsFirst( bool nullsFirst );
204 
208  QString dump() const;
209 
210  // friend inline int qHash(const OrderByClause &a) { return qHash(a.mExpression.expression()) ^ qHash(a.mAscending) ^ qHash( a.mNullsFirst); }
211 
212  private:
213  QgsExpression mExpression;
214  bool mAscending;
215  bool mNullsFirst;
216  };
217 
218 
226  class OrderBy : public QList<QgsFeatureRequest::OrderByClause>
227  {
228  public:
229 
233  CORE_EXPORT OrderBy();
234 
238  CORE_EXPORT OrderBy( const QList<QgsFeatureRequest::OrderByClause> &other );
239 
246  QList<QgsFeatureRequest::OrderByClause> CORE_EXPORT list() const;
247 
251  void CORE_EXPORT save( QDomElement &elem ) const;
252 
256  void CORE_EXPORT load( const QDomElement &elem );
257 
262  QSet<QString> CORE_EXPORT usedAttributes() const;
263 
268  QSet<int> CORE_EXPORT usedAttributeIndices( const QgsFields &fields ) const;
269 
273  QString CORE_EXPORT dump() const;
274  };
275 
279  static const QString ALL_ATTRIBUTES;
280 
284  explicit QgsFeatureRequest( QgsFeatureId fid );
286  explicit QgsFeatureRequest( const QgsFeatureIds &fids );
287 
295  explicit QgsFeatureRequest( const QgsRectangle &rectangle );
296 
298  explicit QgsFeatureRequest( const QgsExpression &expr, const QgsExpressionContext &context = QgsExpressionContext() );
302  QgsFeatureRequest &operator=( const QgsFeatureRequest &rh );
303 
309  FilterType filterType() const { return mFilter; }
310 
320  QgsFeatureRequest &setFilterRect( const QgsRectangle &rectangle );
321 
332  const QgsRectangle &filterRect() const { return mFilterRect; }
333 
335  QgsFeatureRequest &setFilterFid( QgsFeatureId fid );
337  QgsFeatureId filterFid() const { return mFilterFid; }
338 
340  QgsFeatureRequest &setFilterFids( const QgsFeatureIds &fids );
342  const QgsFeatureIds &filterFids() const { return mFilterFids; }
343 
351  QgsFeatureRequest &setInvalidGeometryCheck( InvalidGeometryCheck check );
352 
358  InvalidGeometryCheck invalidGeometryCheck() const { return mInvalidGeometryFilter; }
359 
367 #ifndef SIP_RUN
368  QgsFeatureRequest &setInvalidGeometryCallback( const std::function< void( const QgsFeature & )> &callback );
369 #else
370  QgsFeatureRequest &setInvalidGeometryCallback( SIP_PYCALLABLE / AllowNone / );
371  % MethodCode
372  Py_BEGIN_ALLOW_THREADS
373 
374  sipCpp->setInvalidGeometryCallback( [a0]( const QgsFeature &arg )
375  {
376  SIP_BLOCK_THREADS
377  Py_XDECREF( sipCallMethod( NULL, a0, "D", &arg, sipType_QgsFeature, NULL ) );
378  SIP_UNBLOCK_THREADS
379  } );
380 
381  sipRes = sipCpp;
382 
383  Py_END_ALLOW_THREADS
384  % End
385 #endif
386 
394  std::function< void( const QgsFeature & ) > invalidGeometryCallback() const { return mInvalidGeometryCallback; } SIP_SKIP
395 
402  QgsFeatureRequest &setFilterExpression( const QString &expression );
403 
409  QgsExpression *filterExpression() const { return mFilterExpression.get(); }
410 
417  QgsFeatureRequest &combineFilterExpression( const QString &expression );
418 
425  QgsExpressionContext *expressionContext() { return &mExpressionContext; }
426 
433  QgsFeatureRequest &setExpressionContext( const QgsExpressionContext &context );
434 
443  QgsFeatureRequest &disableFilter() { mFilter = FilterNone; mFilterExpression.reset(); return *this; }
444 
456  QgsFeatureRequest &addOrderBy( const QString &expression, bool ascending = true );
457 
467  QgsFeatureRequest &addOrderBy( const QString &expression, bool ascending, bool nullsfirst );
468 
474  OrderBy orderBy() const;
475 
481  QgsFeatureRequest &setOrderBy( const OrderBy &orderBy );
482 
489  QgsFeatureRequest &setLimit( long limit );
490 
496  long limit() const { return mLimit; }
497 
499  QgsFeatureRequest &setFlags( QgsFeatureRequest::Flags flags );
500  const Flags &flags() const { return mFlags; }
501 
509  QgsFeatureRequest &setSubsetOfAttributes( const QgsAttributeList &attrs );
510 
517  QgsFeatureRequest &setNoAttributes();
518 
523  QgsAttributeList subsetOfAttributes() const { return mAttrs; }
524 
526  QgsFeatureRequest &setSubsetOfAttributes( const QStringList &attrNames, const QgsFields &fields );
527 
529  QgsFeatureRequest &setSubsetOfAttributes( const QSet<QString> &attrNames, const QgsFields &fields );
530 
535  QgsFeatureRequest &setSimplifyMethod( const QgsSimplifyMethod &simplifyMethod );
536 
541  const QgsSimplifyMethod &simplifyMethod() const { return mSimplifyMethod; }
542 
551  QgsCoordinateReferenceSystem destinationCrs() const;
552 
560  QgsCoordinateTransformContext transformContext() const;
561 
585  QgsFeatureRequest &setDestinationCrs( const QgsCoordinateReferenceSystem &crs, const QgsCoordinateTransformContext &context );
586 
595 #ifndef SIP_RUN
596  QgsFeatureRequest &setTransformErrorCallback( const std::function< void( const QgsFeature & )> &callback );
597 #else
598  QgsFeatureRequest &setTransformErrorCallback( SIP_PYCALLABLE / AllowNone / );
599  % MethodCode
600  Py_BEGIN_ALLOW_THREADS
601 
602  sipCpp->setTransformErrorCallback( [a0]( const QgsFeature &arg )
603  {
604  SIP_BLOCK_THREADS
605  Py_XDECREF( sipCallMethod( NULL, a0, "D", &arg, sipType_QgsFeature, NULL ) );
606  SIP_UNBLOCK_THREADS
607  } );
608 
609  sipRes = sipCpp;
610 
611  Py_END_ALLOW_THREADS
612  % End
613 #endif
614 
623  std::function< void( const QgsFeature & ) > transformErrorCallback() const { return mTransformErrorCallback; } SIP_SKIP
624 
625 
635  bool acceptFeature( const QgsFeature &feature );
636 
646  Q_DECL_DEPRECATED int connectionTimeout() const SIP_DEPRECATED;
647 
657  Q_DECL_DEPRECATED QgsFeatureRequest &setConnectionTimeout( int connectionTimeout ) SIP_DEPRECATED;
658 
667  int timeout() const;
668 
677  QgsFeatureRequest &setTimeout( int timeout );
678 
691  bool requestMayBeNested() const;
692 
705  QgsFeatureRequest &setRequestMayBeNested( bool requestMayBeNested );
706 
707  protected:
708  FilterType mFilter = FilterNone;
710  QgsFeatureId mFilterFid = -1;
712  std::unique_ptr< QgsExpression > mFilterExpression;
714  Flags mFlags;
717  long mLimit = -1;
719  InvalidGeometryCheck mInvalidGeometryFilter = GeometryNoCheck;
720  std::function< void( const QgsFeature & ) > mInvalidGeometryCallback;
721  std::function< void( const QgsFeature & ) > mTransformErrorCallback;
724  int mTimeout = -1;
725  int mRequestMayBeNested = false;
726 };
727 
728 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsFeatureRequest::Flags )
729 
730 
731 class QgsFeatureIterator;
733 
739 class CORE_EXPORT QgsAbstractFeatureSource
740 {
741  public:
742  virtual ~QgsAbstractFeatureSource();
743 
749  virtual QgsFeatureIterator getFeatures( const QgsFeatureRequest &request = QgsFeatureRequest() ) = 0;
750 
751  protected:
752  void iteratorOpened( QgsAbstractFeatureIterator *it );
753  void iteratorClosed( QgsAbstractFeatureIterator *it );
754 
755  QSet< QgsAbstractFeatureIterator * > mActiveIterators;
756 
757  template<typename> friend class QgsAbstractFeatureIteratorFromSource;
758 };
759 
760 #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:34
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
Definition: qgsfeatureid.h:25
FilterType filterType() const
Returns 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
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: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
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.