QGIS API Documentation  3.5.0-Master (bc98f86)
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 
261  QSet<QString> CORE_EXPORT usedAttributes() const;
262 
266  QString CORE_EXPORT dump() const;
267  };
268 
272  static const QString ALL_ATTRIBUTES;
273 
277  explicit QgsFeatureRequest( QgsFeatureId fid );
279  explicit QgsFeatureRequest( const QgsFeatureIds &fids );
280 
288  explicit QgsFeatureRequest( const QgsRectangle &rectangle );
289 
291  explicit QgsFeatureRequest( const QgsExpression &expr, const QgsExpressionContext &context = QgsExpressionContext() );
295  QgsFeatureRequest &operator=( const QgsFeatureRequest &rh );
296 
302  FilterType filterType() const { return mFilter; }
303 
313  QgsFeatureRequest &setFilterRect( const QgsRectangle &rectangle );
314 
325  const QgsRectangle &filterRect() const { return mFilterRect; }
326 
328  QgsFeatureRequest &setFilterFid( QgsFeatureId fid );
330  QgsFeatureId filterFid() const { return mFilterFid; }
331 
333  QgsFeatureRequest &setFilterFids( const QgsFeatureIds &fids );
335  const QgsFeatureIds &filterFids() const { return mFilterFids; }
336 
344  QgsFeatureRequest &setInvalidGeometryCheck( InvalidGeometryCheck check );
345 
351  InvalidGeometryCheck invalidGeometryCheck() const { return mInvalidGeometryFilter; }
352 
360 #ifndef SIP_RUN
361  QgsFeatureRequest &setInvalidGeometryCallback( const std::function< void( const QgsFeature & )> &callback );
362 #else
363  QgsFeatureRequest &setInvalidGeometryCallback( SIP_PYCALLABLE / AllowNone / );
364  % MethodCode
365  Py_BEGIN_ALLOW_THREADS
366 
367  sipCpp->setInvalidGeometryCallback( [a0]( const QgsFeature &arg )
368  {
369  SIP_BLOCK_THREADS
370  Py_XDECREF( sipCallMethod( NULL, a0, "D", &arg, sipType_QgsFeature, NULL ) );
371  SIP_UNBLOCK_THREADS
372  } );
373 
374  sipRes = sipCpp;
375 
376  Py_END_ALLOW_THREADS
377  % End
378 #endif
379 
387  std::function< void( const QgsFeature & ) > invalidGeometryCallback() const { return mInvalidGeometryCallback; } SIP_SKIP
388 
395  QgsFeatureRequest &setFilterExpression( const QString &expression );
396 
402  QgsExpression *filterExpression() const { return mFilterExpression.get(); }
403 
410  QgsFeatureRequest &combineFilterExpression( const QString &expression );
411 
418  QgsExpressionContext *expressionContext() { return &mExpressionContext; }
419 
426  QgsFeatureRequest &setExpressionContext( const QgsExpressionContext &context );
427 
436  QgsFeatureRequest &disableFilter() { mFilter = FilterNone; mFilterExpression.reset(); return *this; }
437 
449  QgsFeatureRequest &addOrderBy( const QString &expression, bool ascending = true );
450 
460  QgsFeatureRequest &addOrderBy( const QString &expression, bool ascending, bool nullsfirst );
461 
467  OrderBy orderBy() const;
468 
474  QgsFeatureRequest &setOrderBy( const OrderBy &orderBy );
475 
482  QgsFeatureRequest &setLimit( long limit );
483 
489  long limit() const { return mLimit; }
490 
492  QgsFeatureRequest &setFlags( QgsFeatureRequest::Flags flags );
493  const Flags &flags() const { return mFlags; }
494 
502  QgsFeatureRequest &setSubsetOfAttributes( const QgsAttributeList &attrs );
503 
510  QgsFeatureRequest &setNoAttributes();
511 
516  QgsAttributeList subsetOfAttributes() const { return mAttrs; }
517 
519  QgsFeatureRequest &setSubsetOfAttributes( const QStringList &attrNames, const QgsFields &fields );
520 
522  QgsFeatureRequest &setSubsetOfAttributes( const QSet<QString> &attrNames, const QgsFields &fields );
523 
528  QgsFeatureRequest &setSimplifyMethod( const QgsSimplifyMethod &simplifyMethod );
529 
534  const QgsSimplifyMethod &simplifyMethod() const { return mSimplifyMethod; }
535 
544  QgsCoordinateReferenceSystem destinationCrs() const;
545 
553  QgsCoordinateTransformContext transformContext() const;
554 
578  QgsFeatureRequest &setDestinationCrs( const QgsCoordinateReferenceSystem &crs, const QgsCoordinateTransformContext &context );
579 
588 #ifndef SIP_RUN
589  QgsFeatureRequest &setTransformErrorCallback( const std::function< void( const QgsFeature & )> &callback );
590 #else
591  QgsFeatureRequest &setTransformErrorCallback( SIP_PYCALLABLE / AllowNone / );
592  % MethodCode
593  Py_BEGIN_ALLOW_THREADS
594 
595  sipCpp->setTransformErrorCallback( [a0]( const QgsFeature &arg )
596  {
597  SIP_BLOCK_THREADS
598  Py_XDECREF( sipCallMethod( NULL, a0, "D", &arg, sipType_QgsFeature, NULL ) );
599  SIP_UNBLOCK_THREADS
600  } );
601 
602  sipRes = sipCpp;
603 
604  Py_END_ALLOW_THREADS
605  % End
606 #endif
607 
616  std::function< void( const QgsFeature & ) > transformErrorCallback() const { return mTransformErrorCallback; } SIP_SKIP
617 
618 
628  bool acceptFeature( const QgsFeature &feature );
629 
639  Q_DECL_DEPRECATED int connectionTimeout() const SIP_DEPRECATED;
640 
650  Q_DECL_DEPRECATED QgsFeatureRequest &setConnectionTimeout( int connectionTimeout ) SIP_DEPRECATED;
651 
660  int timeout() const;
661 
670  QgsFeatureRequest &setTimeout( int timeout );
671 
684  bool requestMayBeNested() const;
685 
698  QgsFeatureRequest &setRequestMayBeNested( bool requestMayBeNested );
699 
700  protected:
701  FilterType mFilter = FilterNone;
703  QgsFeatureId mFilterFid = -1;
705  std::unique_ptr< QgsExpression > mFilterExpression;
707  Flags mFlags;
710  long mLimit = -1;
712  InvalidGeometryCheck mInvalidGeometryFilter = GeometryNoCheck;
713  std::function< void( const QgsFeature & ) > mInvalidGeometryCallback;
714  std::function< void( const QgsFeature & ) > mTransformErrorCallback;
717  int mTimeout = -1;
718  int mRequestMayBeNested = false;
719 };
720 
721 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsFeatureRequest::Flags )
722 
723 
724 class QgsFeatureIterator;
726 
732 class CORE_EXPORT QgsAbstractFeatureSource
733 {
734  public:
735  virtual ~QgsAbstractFeatureSource();
736 
742  virtual QgsFeatureIterator getFeatures( const QgsFeatureRequest &request = QgsFeatureRequest() ) = 0;
743 
744  protected:
745  void iteratorOpened( QgsAbstractFeatureIterator *it );
746  void iteratorClosed( QgsAbstractFeatureIterator *it );
747 
748  QSet< QgsAbstractFeatureIterator * > mActiveIterators;
749 
750  template<typename> friend class QgsAbstractFeatureIteratorFromSource;
751 };
752 
753 #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:40
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: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
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:99
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.