QGIS API Documentation  3.3.0-Master (ae397bf)
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 
499  QgsFeatureRequest &setSubsetOfAttributes( const QgsAttributeList &attrs );
500 
505  QgsAttributeList subsetOfAttributes() const { return mAttrs; }
506 
508  QgsFeatureRequest &setSubsetOfAttributes( const QStringList &attrNames, const QgsFields &fields );
509 
511  QgsFeatureRequest &setSubsetOfAttributes( const QSet<QString> &attrNames, const QgsFields &fields );
512 
517  QgsFeatureRequest &setSimplifyMethod( const QgsSimplifyMethod &simplifyMethod );
518 
523  const QgsSimplifyMethod &simplifyMethod() const { return mSimplifyMethod; }
524 
533  QgsCoordinateReferenceSystem destinationCrs() const;
534 
542  QgsCoordinateTransformContext transformContext() const;
543 
567  QgsFeatureRequest &setDestinationCrs( const QgsCoordinateReferenceSystem &crs, const QgsCoordinateTransformContext &context );
568 
577 #ifndef SIP_RUN
578  QgsFeatureRequest &setTransformErrorCallback( const std::function< void( const QgsFeature & )> &callback );
579 #else
580  QgsFeatureRequest &setTransformErrorCallback( SIP_PYCALLABLE / AllowNone / );
581  % MethodCode
582  Py_BEGIN_ALLOW_THREADS
583 
584  sipCpp->setTransformErrorCallback( [a0]( const QgsFeature &arg )
585  {
586  SIP_BLOCK_THREADS
587  Py_XDECREF( sipCallMethod( NULL, a0, "D", &arg, sipType_QgsFeature, NULL ) );
588  SIP_UNBLOCK_THREADS
589  } );
590 
591  sipRes = sipCpp;
592 
593  Py_END_ALLOW_THREADS
594  % End
595 #endif
596 
605  std::function< void( const QgsFeature & ) > transformErrorCallback() const { return mTransformErrorCallback; } SIP_SKIP
606 
607 
617  bool acceptFeature( const QgsFeature &feature );
618 
628  Q_DECL_DEPRECATED int connectionTimeout() const SIP_DEPRECATED;
629 
639  Q_DECL_DEPRECATED QgsFeatureRequest &setConnectionTimeout( int connectionTimeout ) SIP_DEPRECATED;
640 
649  int timeout() const;
650 
659  QgsFeatureRequest &setTimeout( int timeout );
660 
673  bool requestMayBeNested() const;
674 
687  QgsFeatureRequest &setRequestMayBeNested( bool requestMayBeNested );
688 
689  protected:
690  FilterType mFilter = FilterNone;
692  QgsFeatureId mFilterFid = -1;
694  std::unique_ptr< QgsExpression > mFilterExpression;
696  Flags mFlags;
699  long mLimit = -1;
701  InvalidGeometryCheck mInvalidGeometryFilter = GeometryNoCheck;
702  std::function< void( const QgsFeature & ) > mInvalidGeometryCallback;
703  std::function< void( const QgsFeature & ) > mTransformErrorCallback;
706  int mTimeout = -1;
707  int mRequestMayBeNested = false;
708 };
709 
710 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsFeatureRequest::Flags )
711 
712 
713 class QgsFeatureIterator;
715 
721 class CORE_EXPORT QgsAbstractFeatureSource
722 {
723  public:
724  virtual ~QgsAbstractFeatureSource();
725 
731  virtual QgsFeatureIterator getFeatures( const QgsFeatureRequest &request = QgsFeatureRequest() ) = 0;
732 
733  protected:
734  void iteratorOpened( QgsAbstractFeatureIterator *it );
735  void iteratorClosed( QgsAbstractFeatureIterator *it );
736 
737  QSet< QgsAbstractFeatureIterator * > mActiveIterators;
738 
739  template<typename> friend class QgsAbstractFeatureIteratorFromSource;
740 };
741 
742 #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:54
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.