QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
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;
712  QgsRectangle mFilterRect;
713  QgsFeatureId mFilterFid = -1;
714  QgsFeatureIds mFilterFids;
715  std::unique_ptr< QgsExpression > mFilterExpression;
716  QgsExpressionContext mExpressionContext;
717  Flags mFlags = Flags();
719  QgsSimplifyMethod mSimplifyMethod;
720  long mLimit = -1;
721  OrderBy mOrderBy;
722  InvalidGeometryCheck mInvalidGeometryFilter = GeometryNoCheck;
723  std::function< void( const QgsFeature & ) > mInvalidGeometryCallback;
724  std::function< void( const QgsFeature & ) > mTransformErrorCallback;
726  QgsCoordinateTransformContext mTransformContext;
727  int mTimeout = -1;
728  int mRequestMayBeNested = false;
729 };
730 
732 
733 
734 class QgsFeatureIterator;
736 
742 class CORE_EXPORT QgsAbstractFeatureSource
743 {
744  public:
745  virtual ~QgsAbstractFeatureSource();
746 
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
QgsFeatureRequest::Flag
Flag
Definition: qgsfeaturerequest.h:79
QgsFeatureRequest::invalidGeometryCallback
std::function< void(const QgsFeature &) > invalidGeometryCallback() const
Returns the callback function to use when encountering an invalid geometry and invalidGeometryCheck()...
Definition: qgsfeaturerequest.h:397
QgsExpressionContext
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Definition: qgsexpressioncontext.h:370
QgsFeatureRequest::OrderByClause
The OrderByClause class represents an order by clause for a QgsFeatureRequest.
Definition: qgsfeaturerequest.h:130
QgsFeatureRequest::FilterFid
@ FilterFid
Filter using feature ID.
Definition: qgsfeaturerequest.h:93
QgsCoordinateTransformContext
Contains information about the context in which a coordinate transform is executed.
Definition: qgscoordinatetransformcontext.h:58
QgsSimplifyMethod
This class contains information about how to simplify geometries fetched from a QgsFeatureIterator.
Definition: qgssimplifymethod.h:29
QgsFeatureRequest::filterFids
const QgsFeatureIds & filterFids() const
Gets feature IDs that should be fetched.
Definition: qgsfeaturerequest.h:345
qgsrectangle.h
QgsFeatureRequest::OrderBy::OrderBy
CORE_EXPORT OrderBy()
Create a new empty order by.
qgsexpression.h
QgsFeatureRequest::filterFid
QgsFeatureId filterFid() const
Gets the feature ID that should be fetched.
Definition: qgsfeaturerequest.h:340
QgsFeatureRequest::flags
const Flags & flags() const
Definition: qgsfeaturerequest.h:503
QgsFeatureRequest::filterRect
const QgsRectangle & filterRect() const
Returns the rectangle from which features will be taken.
Definition: qgsfeaturerequest.h:335
crs
const QgsCoordinateReferenceSystem & crs
Definition: qgswfsgetfeature.cpp:51
QgsFeatureRequest::filterType
FilterType filterType() const
Returns the filter type which is currently set on this request.
Definition: qgsfeaturerequest.h:312
QgsFields
Container of fields for a vector layer.
Definition: qgsfields.h:45
qgsfeature.h
QgsAbstractFeatureSource::mActiveIterators
QSet< QgsAbstractFeatureIterator * > mActiveIterators
Definition: qgsfeaturerequest.h:758
QgsFeatureRequest::InvalidGeometryCheck
InvalidGeometryCheck
Handling of features with invalid geometries.
Definition: qgsfeaturerequest.h:100
QgsAttributeList
QList< int > QgsAttributeList
Definition: qgsfield.h:26
QgsRectangle
A rectangle specified with double values.
Definition: qgsrectangle.h:42
QgsFeatureRequest::disableFilter
QgsFeatureRequest & disableFilter()
Disables filter conditions.
Definition: qgsfeaturerequest.h:446
QgsAbstractFeatureIteratorFromSource
Helper template that cares of two things: 1.
Definition: qgsfeatureiterator.h:235
QgsFeatureRequest::expressionContext
QgsExpressionContext * expressionContext()
Returns the expression context used to evaluate filter expressions.
Definition: qgsfeaturerequest.h:428
geos::unique_ptr
std::unique_ptr< GEOSGeometry, GeosDeleter > unique_ptr
Scoped GEOS pointer.
Definition: qgsgeos.h:79
SIP_DEPRECATED
#define SIP_DEPRECATED
Definition: qgis_sip.h:106
QgsFeatureRequest::ALL_ATTRIBUTES
static const QString ALL_ATTRIBUTES
A special attribute that if set matches all attributes.
Definition: qgsfeaturerequest.h:282
QgsFeatureRequest
This class wraps a request for features to a vector layer (or directly its vector data provider).
Definition: qgsfeaturerequest.h:76
qgsexpressioncontext.h
SIP_SKIP
#define SIP_SKIP
Definition: qgis_sip.h:126
QgsFeatureRequest::filterExpression
QgsExpression * filterExpression() const
Returns the filter expression if set.
Definition: qgsfeaturerequest.h:412
qgis_sip.h
QgsAbstractFeatureSource
Base class that can be used for any class that is capable of returning features.
Definition: qgsfeaturerequest.h:743
Q_DECLARE_OPERATORS_FOR_FLAGS
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.
QgsFeatureRequest::FilterExpression
@ FilterExpression
Filter using expression.
Definition: qgsfeaturerequest.h:94
QgsFeatureIds
QSet< QgsFeatureId > QgsFeatureIds
Definition: qgsfeatureid.h:37
QgsCoordinateReferenceSystem
This class represents a coordinate reference system (CRS).
Definition: qgscoordinatereferencesystem.h:206
QgsFeatureRequest::FilterType
FilterType
Types of filters.
Definition: qgsfeaturerequest.h:91
QgsFeatureRequest::OrderBy
Represents a list of OrderByClauses, with the most important first and the least important last.
Definition: qgsfeaturerequest.h:230
QgsFeatureRequest::invalidGeometryCheck
InvalidGeometryCheck invalidGeometryCheck() const
Returns the invalid geometry checking behavior.
Definition: qgsfeaturerequest.h:361
QgsFeatureRequest::subsetOfAttributes
QgsAttributeList subsetOfAttributes() const
Returns the subset of attributes which at least need to be fetched.
Definition: qgsfeaturerequest.h:526
QgsFeatureRequest::simplifyMethod
const QgsSimplifyMethod & simplifyMethod() const
Gets simplification method for geometries that will be fetched.
Definition: qgsfeaturerequest.h:544
QgsFeatureRequest::FilterNone
@ FilterNone
No filter is applied.
Definition: qgsfeaturerequest.h:92
QgsAbstractFeatureSource::getFeatures
virtual QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest())=0
Gets an iterator for features matching the specified request.
QgsFeature
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:56
QgsFeatureRequest::transformErrorCallback
std::function< void(const QgsFeature &) > transformErrorCallback() const
Returns the callback function to use when encountering a transform error when iterating features and ...
Definition: qgsfeaturerequest.h:626
QgsExpression
Class for parsing and evaluation of expressions (formerly called "search strings").
Definition: qgsexpression.h:105
QgsFeatureIterator
Wrapper for iterator of features from vector data provider or vector layer.
Definition: qgsfeatureiterator.h:265
QgsAbstractFeatureIterator
Internal feature iterator to be implemented within data providers.
Definition: qgsfeatureiterator.h:29
qgssimplifymethod.h
QgsFeatureId
qint64 QgsFeatureId
64 bit feature ids negative numbers are used for uncommitted/newly added features
Definition: qgsfeatureid.h:28
QgsFeatureRequest::limit
long limit() const
Returns the maximum number of features to request, or -1 if no limit set.
Definition: qgsfeaturerequest.h:499