QGIS API Documentation  3.14.0-Pi (9f7028fd23)
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 
90  enum FilterType
91  {
92  FilterNone,
93  FilterFid,
94  FilterExpression,
95  FilterFids
96  };
97 
99  enum InvalidGeometryCheck
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;
718  QgsAttributeList mAttrs;
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 
731 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsFeatureRequest::Flags )
732 
733 
736 
742 class CORE_EXPORT QgsAbstractFeatureSource
743 {
744  public:
745  virtual ~QgsAbstractFeatureSource();
746 
752  virtual QgsFeatureIterator getFeatures( const QgsFeatureRequest &request = QgsFeatureRequest() ) = 0;
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:91
QgsExpressionContext
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Definition: qgsexpressioncontext.h:369
QgsFeatureRequest::OrderByClause
Definition: qgsfeaturerequest.h:142
QgsCoordinateTransformContext
Definition: qgscoordinatetransformcontext.h:57
QgsSimplifyMethod
Definition: qgssimplifymethod.h:28
qgsrectangle.h
qgsexpression.h
crs
const QgsCoordinateReferenceSystem & crs
Definition: qgswfsgetfeature.cpp:105
QgsFields
Definition: qgsfields.h:44
qgsfeature.h
QgsAbstractFeatureSource::mActiveIterators
QSet< QgsAbstractFeatureIterator * > mActiveIterators
Definition: qgsfeaturerequest.h:758
QgsFeatureRequest::InvalidGeometryCheck
InvalidGeometryCheck
Handling of features with invalid geometries.
Definition: qgsfeaturerequest.h:112
QgsAttributeList
QList< int > QgsAttributeList
Definition: qgsfield.h:26
QgsRectangle
Definition: qgsrectangle.h:41
QgsAbstractFeatureIteratorFromSource
Definition: qgsfeatureiterator.h:233
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
Definition: qgsfeaturerequest.h:75
qgsexpressioncontext.h
SIP_SKIP
#define SIP_SKIP
Definition: qgis_sip.h:126
qgis_sip.h
QgsAbstractFeatureSource
Definition: qgsfeaturerequest.h:742
QgsFeatureIds
QSet< QgsFeatureId > QgsFeatureIds
Definition: qgsfeatureid.h:34
QgsCoordinateReferenceSystem
Definition: qgscoordinatereferencesystem.h:206
QgsFeatureRequest::FilterType
FilterType
Types of filters.
Definition: qgsfeaturerequest.h:103
QgsFeatureRequest::OrderBy
Definition: qgsfeaturerequest.h:242
QgsFeature
Definition: qgsfeature.h:55
QgsExpression
Definition: qgsexpression.h:113
QgsFeatureIterator
Definition: qgsfeatureiterator.h:263
QgsAbstractFeatureIterator
Definition: qgsfeatureiterator.h:28
qgssimplifymethod.h
QgsFeatureId
qint64 QgsFeatureId
Definition: qgsfeatureid.h:25