QGIS API Documentation  3.21.0-Master (564c892cfd)
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 
76 class CORE_EXPORT QgsFeatureRequest
77 {
78  public:
79  enum Flag
80  {
81  NoFlags = 0,
82  NoGeometry = 1,
83  SubsetOfAttributes = 2,
84  ExactIntersect = 4,
85  IgnoreStaticNodesDuringExpressionCompilation = 8,
86  EmbeddedSymbols = 16,
87  };
88  Q_DECLARE_FLAGS( Flags, Flag )
89 
90 
94  {
98  FilterFids
99  };
100 
103  {
104  GeometryNoCheck = 0,
105  GeometrySkipInvalid = 1,
106  GeometryAbortOnInvalid = 2,
107  };
108 
132  class CORE_EXPORT OrderByClause
133  {
134  public:
135 
144  OrderByClause( const QString &expression, bool ascending = true );
145 
153  OrderByClause( const QString &expression, bool ascending, bool nullsfirst );
154 
163  OrderByClause( const QgsExpression &expression, bool ascending = true );
164 
172  OrderByClause( const QgsExpression &expression, bool ascending, bool nullsfirst );
173 
178  QgsExpression expression() const;
179 
187  bool prepare( QgsExpressionContext *context );
188 
193  bool ascending() const;
194 
198  void setAscending( bool ascending );
199 
204  bool nullsFirst() const;
205 
209  void setNullsFirst( bool nullsFirst );
210 
214  QString dump() const;
215 
216  // friend inline int qHash(const OrderByClause &a) { return qHash(a.mExpression.expression()) ^ qHash(a.mAscending) ^ qHash( a.mNullsFirst); }
217 
218  private:
219  QgsExpression mExpression;
220  bool mAscending;
221  bool mNullsFirst;
222  };
223 
224 
232  class OrderBy : public QList<QgsFeatureRequest::OrderByClause>
233  {
234  public:
235 
239  CORE_EXPORT OrderBy();
240 
244  CORE_EXPORT OrderBy( const QList<QgsFeatureRequest::OrderByClause> &other );
245 
252  QList<QgsFeatureRequest::OrderByClause> CORE_EXPORT list() const;
253 
257  void CORE_EXPORT save( QDomElement &elem ) const;
258 
262  void CORE_EXPORT load( const QDomElement &elem );
263 
268  QSet<QString> CORE_EXPORT usedAttributes() const;
269 
274  QSet<int> CORE_EXPORT usedAttributeIndices( const QgsFields &fields ) const;
275 
279  QString CORE_EXPORT dump() const;
280  };
281 
285  static const QString ALL_ATTRIBUTES;
286 
290  explicit QgsFeatureRequest( QgsFeatureId fid );
292  explicit QgsFeatureRequest( const QgsFeatureIds &fids );
293 
301  explicit QgsFeatureRequest( const QgsRectangle &rectangle );
302 
304  explicit QgsFeatureRequest( const QgsExpression &expr, const QgsExpressionContext &context = QgsExpressionContext() );
308  QgsFeatureRequest &operator=( const QgsFeatureRequest &rh );
309 
315  FilterType filterType() const { return mFilter; }
316 
326  QgsFeatureRequest &setFilterRect( const QgsRectangle &rectangle );
327 
338  const QgsRectangle &filterRect() const { return mFilterRect; }
339 
341  QgsFeatureRequest &setFilterFid( QgsFeatureId fid );
343  QgsFeatureId filterFid() const { return mFilterFid; }
344 
346  QgsFeatureRequest &setFilterFids( const QgsFeatureIds &fids );
348  const QgsFeatureIds &filterFids() const { return mFilterFids; }
349 
357  QgsFeatureRequest &setInvalidGeometryCheck( InvalidGeometryCheck check );
358 
364  InvalidGeometryCheck invalidGeometryCheck() const { return mInvalidGeometryFilter; }
365 
373 #ifndef SIP_RUN
374  QgsFeatureRequest &setInvalidGeometryCallback( const std::function< void( const QgsFeature & )> &callback );
375 #else
376  QgsFeatureRequest &setInvalidGeometryCallback( SIP_PYCALLABLE / AllowNone / );
377  % MethodCode
378  Py_BEGIN_ALLOW_THREADS
379 
380  sipCpp->setInvalidGeometryCallback( [a0]( const QgsFeature &arg )
381  {
382  SIP_BLOCK_THREADS
383  Py_XDECREF( sipCallMethod( NULL, a0, "D", &arg, sipType_QgsFeature, NULL ) );
384  SIP_UNBLOCK_THREADS
385  } );
386 
387  sipRes = sipCpp;
388 
389  Py_END_ALLOW_THREADS
390  % End
391 #endif
392 
400  std::function< void( const QgsFeature & ) > invalidGeometryCallback() const { return mInvalidGeometryCallback; } SIP_SKIP
401 
408  QgsFeatureRequest &setFilterExpression( const QString &expression );
409 
415  QgsExpression *filterExpression() const { return mFilterExpression.get(); }
416 
423  QgsFeatureRequest &combineFilterExpression( const QString &expression );
424 
431  QgsExpressionContext *expressionContext() { return &mExpressionContext; }
432 
439  QgsFeatureRequest &setExpressionContext( const QgsExpressionContext &context );
440 
449  QgsFeatureRequest &disableFilter() { mFilter = FilterNone; mFilterExpression.reset(); return *this; }
450 
462  QgsFeatureRequest &addOrderBy( const QString &expression, bool ascending = true );
463 
473  QgsFeatureRequest &addOrderBy( const QString &expression, bool ascending, bool nullsfirst );
474 
480  OrderBy orderBy() const;
481 
487  QgsFeatureRequest &setOrderBy( const OrderBy &orderBy );
488 
495  QgsFeatureRequest &setLimit( long long limit );
496 
502 #ifndef SIP_RUN
503  long long limit() const { return mLimit; }
504 #else
505  long long limit() const;
506 #endif
507 
509  QgsFeatureRequest &setFlags( QgsFeatureRequest::Flags flags );
510  const Flags &flags() const { return mFlags; }
511 
519  QgsFeatureRequest &setSubsetOfAttributes( const QgsAttributeList &attrs );
520 
527  QgsFeatureRequest &setNoAttributes();
528 
533  QgsAttributeList subsetOfAttributes() const { return mAttrs; }
534 
536  QgsFeatureRequest &setSubsetOfAttributes( const QStringList &attrNames, const QgsFields &fields );
537 
539  QgsFeatureRequest &setSubsetOfAttributes( const QSet<QString> &attrNames, const QgsFields &fields );
540 
545  QgsFeatureRequest &setSimplifyMethod( const QgsSimplifyMethod &simplifyMethod );
546 
551  const QgsSimplifyMethod &simplifyMethod() const { return mSimplifyMethod; }
552 
561  QgsCoordinateReferenceSystem destinationCrs() const;
562 
570  QgsCoordinateTransformContext transformContext() const;
571 
595  QgsFeatureRequest &setDestinationCrs( const QgsCoordinateReferenceSystem &crs, const QgsCoordinateTransformContext &context );
596 
605 #ifndef SIP_RUN
606  QgsFeatureRequest &setTransformErrorCallback( const std::function< void( const QgsFeature & )> &callback );
607 #else
608  QgsFeatureRequest &setTransformErrorCallback( SIP_PYCALLABLE / AllowNone / );
609  % MethodCode
610  Py_BEGIN_ALLOW_THREADS
611 
612  sipCpp->setTransformErrorCallback( [a0]( const QgsFeature &arg )
613  {
614  SIP_BLOCK_THREADS
615  Py_XDECREF( sipCallMethod( NULL, a0, "D", &arg, sipType_QgsFeature, NULL ) );
616  SIP_UNBLOCK_THREADS
617  } );
618 
619  sipRes = sipCpp;
620 
621  Py_END_ALLOW_THREADS
622  % End
623 #endif
624 
633  std::function< void( const QgsFeature & ) > transformErrorCallback() const { return mTransformErrorCallback; } SIP_SKIP
634 
635 
645  bool acceptFeature( const QgsFeature &feature );
646 
656  Q_DECL_DEPRECATED int connectionTimeout() const SIP_DEPRECATED;
657 
667  Q_DECL_DEPRECATED QgsFeatureRequest &setConnectionTimeout( int connectionTimeout ) SIP_DEPRECATED;
668 
677  int timeout() const;
678 
687  QgsFeatureRequest &setTimeout( int timeout );
688 
701  bool requestMayBeNested() const;
702 
715  QgsFeatureRequest &setRequestMayBeNested( bool requestMayBeNested );
716 
717  protected:
718  FilterType mFilter = FilterNone;
719  QgsRectangle mFilterRect;
720  QgsFeatureId mFilterFid = -1;
721  QgsFeatureIds mFilterFids;
722  std::unique_ptr< QgsExpression > mFilterExpression;
723  QgsExpressionContext mExpressionContext;
724  Flags mFlags = Flags();
726  QgsSimplifyMethod mSimplifyMethod;
727  long long mLimit = -1;
728  OrderBy mOrderBy;
729  InvalidGeometryCheck mInvalidGeometryFilter = GeometryNoCheck;
730  std::function< void( const QgsFeature & ) > mInvalidGeometryCallback;
731  std::function< void( const QgsFeature & ) > mTransformErrorCallback;
733  QgsCoordinateTransformContext mTransformContext;
734  int mTimeout = -1;
735  int mRequestMayBeNested = false;
736 };
737 
739 
740 
741 class QgsFeatureIterator;
743 
749 class CORE_EXPORT QgsAbstractFeatureSource
750 {
751  public:
752  virtual ~QgsAbstractFeatureSource();
753 
754 
755  // IMPORTANT -- do NOT remove the /TransferBack/ annotation here -- while it looks completely wrong, it's
756  // required for Python data providers to work correctly! Argh!
757 
764 
765  // IMPORTANT -- do NOT remove the /TransferBack/ annotation here -- while it looks completely wrong, it's
766  // required for Python data providers to work correctly! Argh!
767 
768  protected:
769  void iteratorOpened( QgsAbstractFeatureIterator *it );
770  void iteratorClosed( QgsAbstractFeatureIterator *it );
771 
772  QSet< QgsAbstractFeatureIterator * > mActiveIterators;
773 
774  template<typename> friend class QgsAbstractFeatureIteratorFromSource;
775 };
776 
777 #endif // QGSFEATUREREQUEST_H
Helper template that cares of two things: 1.
Internal feature iterator to be implemented within data providers.
Base class that can be used for any class that is capable of returning features.
QSet< QgsAbstractFeatureIterator * > mActiveIterators
virtual QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest())=0
Gets an iterator for features matching the specified request.
This class represents a coordinate reference system (CRS).
Contains information about the context in which a coordinate transform is executed.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Class for parsing and evaluation of expressions (formerly called "search strings").
Wrapper for iterator of features from vector data provider or vector layer.
The OrderByClause class represents an order by clause for a QgsFeatureRequest.
Represents a list of OrderByClauses, with the most important first and the least important last.
CORE_EXPORT OrderBy()
Create a new empty order by.
This class wraps a request for features to a vector layer (or directly its vector data provider).
std::function< void(const QgsFeature &) > invalidGeometryCallback() const
Returns the callback function to use when encountering an invalid geometry and invalidGeometryCheck()...
const QgsSimplifyMethod & simplifyMethod() const
Gets simplification method for geometries that will be fetched.
InvalidGeometryCheck
Handling of features with invalid geometries.
InvalidGeometryCheck invalidGeometryCheck() const
Returns the invalid geometry checking behavior.
QgsExpression * filterExpression() const
Returns the filter expression if set.
long long limit() const
Returns the maximum number of features to request, or -1 if no limit set.
static const QString ALL_ATTRIBUTES
A special attribute that if set matches all attributes.
QgsExpressionContext * expressionContext()
Returns the expression context used to evaluate filter expressions.
QgsAttributeList subsetOfAttributes() const
Returns the subset of attributes which at least need to be fetched.
QgsFeatureRequest & disableFilter()
Disables filter conditions.
const QgsRectangle & filterRect() const
Returns the rectangle from which features will be taken.
FilterType filterType() const
Returns the filter type which is currently set on this request.
const QgsFeatureIds & filterFids() const
Gets feature IDs that should be fetched.
FilterType
Types of filters.
@ FilterFid
Filter using feature ID.
@ FilterNone
No filter is applied.
@ FilterExpression
Filter using expression.
const Flags & flags() const
std::function< void(const QgsFeature &) > transformErrorCallback() const
Returns the callback function to use when encountering a transform error when iterating features and ...
QgsFeatureId filterFid() const
Gets the feature ID that should be fetched.
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition: qgsfeature.h:56
Container of fields for a vector layer.
Definition: qgsfields.h:45
A rectangle specified with double values.
Definition: qgsrectangle.h:42
This class contains information about how to simplify geometries fetched from a QgsFeatureIterator.
std::unique_ptr< GEOSGeometry, GeosDeleter > unique_ptr
Scoped GEOS pointer.
Definition: qgsgeos.h:79
#define SIP_DEPRECATED
Definition: qgis_sip.h:106
#define SIP_SKIP
Definition: qgis_sip.h:126
#define SIP_TRANSFERBACK
Definition: qgis_sip.h:48
QSet< QgsFeatureId > QgsFeatureIds
Definition: qgsfeatureid.h:37
qint64 QgsFeatureId
64 bit feature ids negative numbers are used for uncommitted/newly added features
Definition: qgsfeatureid.h:28
QList< int > QgsAttributeList
Definition: qgsfield.h:26
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.
const QgsCoordinateReferenceSystem & crs